U E D R , A S I H C RSS

Smith Numbers/조현태

느낀점 및 설명

음.. 크기가 크기인 만큼 시간이 약~~간 걸리긴 하지만..
뭐..풀어준다는..*^^*
문제가 있다면 문제에서 요구하는 10^9까지는 무리...
속도를 위해서 저번에 사용했던 소스를 또 우려먹긴 했지만..
10^9이라니.. 약 1TB.. 농담이 아니자낫~>ㅃ<;;
뭐.. 이런이유로.. 속도를 문제로 조건을 버려버린..불량감자소스가 되어버렸다고나..
어쨋든 문제에서 필요한건 풀어내니 문제는 없다고 본다~ 나름대로 빨리 풀어낸다고.ㅎ

소스

~cpp 
#include <stdio.h>
#include <iostream>

unsigned int Sum_jari(unsigned int);
unsigned int Creat_base_and_process(unsigned int number);
unsigned int Get_right(unsigned int, unsigned int*);

const int MAX_NUMBER=10000000;

void main()
{
	int number_simulation;
	printf("테스트를 시행할 횟수를 입력하세요.\n>>");
	scanf("%d",&number_simulation);
	for (;number_simulation>0;--number_simulation)
	{
		unsigned int minimum_number;
		printf ("\n숫자를 입력하세요.\n>>");
		scanf("%d",&minimum_number);
		printf("결과 : %d\n",Creat_base_and_process(minimum_number+1));
	}
}

unsigned int Get_right(unsigned int number, unsigned int* log_number)
{
	unsigned int sum=0;
	while (number>1)
	{
		if (1==log_number[number])
		{
			sum+=number;
			break;
		}
		sum+=Sum_jari(log_number[number]);
		number/=log_number[number];
	}
	return sum;
}

unsigned int Sum_jari(unsigned int number)
{
	unsigned int sum=0;
	while (number>0)
	{
		sum+=number%10;
		number/=10;
	}
	return sum;
}

unsigned int Creat_base_and_process(unsigned int number)
{
	unsigned int *log_number=(unsigned int*)malloc((MAX_NUMBER+2)*sizeof(unsigned int));  
	unsigned int gab;  
	unsigned int sum=0;
	log_number[2]=1;  
	log_number[3]=0;  
	for (register unsigned int i=4; i<=MAX_NUMBER;i+=2)  
	{  
		log_number[i]=1;  
		log_number[i+1]=0;  
	}  
	for (register unsigned int i=3; i<=MAX_NUMBER; ++i)  
	{  
		if (0==log_number[i])  
		{  
			log_number[i]=1;  
			gab=i+i;  
			for(register unsigned int j=i+gab; j<=MAX_NUMBER; j+=gab)  
				log_number[j]=i;  
		}
	}
	unsigned int left=Sum_jari(number),right=Get_right(number,log_number);
	while (left!=right)
	{
		if (MAX_NUMBER==number)
		{
			free(log_number); 
			return -1;
		}
		++number;
		left=Sum_jari(number);
		right=Get_right(number,log_number);
	}
	free(log_number); 
	return number;
}

저에게 할말

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:04
Processing time 0.0128 sec