当先锋百科网

首页 1 2 3 4 5 6 7

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1128

题目描述:

Self Numbers

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5733    Accepted Submission(s): 2521


Problem Description
In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 
33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ...

The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97. 


Write a program to output all positive self-numbers less than or equal 1000000 in increasing order, one per line. 
 

Sample Output
  
1 3 5 7 9 20 31 42 53 64 | | <-- a lot more numbers | 9903 9914 9925 9927 9938 9949 9960 9971 9982 9993 | | |

题意:

计算不能 组成d(n)的 数字。

题解:

从1到1000000,把能组成d(n)的数字 标记出来,剩下的就是不能组成的数字了。

代码:

/*
we should set the array size beyond the standard more  such as +50 or +500
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define M 1000000
using namespace std;
int flag[1000000+500]={0};
int digit[7+5]={0};//the number of the bit
/*d(n)*/
int d1(int num)
{
	int sum=num;
	int bit=0;
	while(num>0)
	{
		bit=num%10;
		num/=10;
		sum+=bit;
	}
	return(sum);
}
/*d(n)*/
int d(int num)
{
	int sum=num;
	int i=0;
	for(i=0;i<=7-1;i++)
	{
		sum+=digit[i];
	}
	return(sum);//sum > 1000000  so will lead to the flag[d{i}] overflow
}
/*big data plus one*/
int PlusDigit()
{
	int i=0;
	digit[0]++;
	for(i=0;i<=7-1;i++)
	{
		if(digit[i]>=10)
		{
			int c=digit[i]/10;
			digit[i+1]+=c;
			digit[i]=digit[i]%10;
		}
	}
	return(0);
}
/*for test*/
int test()
{
	return(0);
}
/*main process*/
int MainProc()
{
	int i=0;
	for(i=1,digit[0]=1;i<=M;i++)//907019
	{
		flag[d(i)]=1;
		if(!flag[i])
		{
			printf("%d\n",i);
		}
		PlusDigit();
	}
	return(0);
}
int main()
{
	MainProc();
	return(0);
}