题目链接:http://acm.tju.edu.cn/toj/showp2502.html2502. Digit Generator
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 2426 Accepted Runs: 1579
For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N, we call N a generator of M.
For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256.
Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.
You are to write a program to find the smallest generator of the given integer.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case takes one line containing an integer N, 1 ≤ N ≤ 100,000.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line is to contain a generator of N for each test case. If N has multiple generators, print the smallest. If N does not have any generators, print 0.
The following shows sample input and output for three test cases.
Sample Input
3 216 121 2005
Output for the Sample Input
198 0 1979数组应用水题直接给代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 using namespace std; 5 int main() 6 { 7 char num[11]; 8 int T; 9 scanf("%d",&T); 10 while(T--) 11 { 12 scanf("%s",num); 13 int tm = 9*strlen(num); 14 int sum =num[0]-‘0‘; 15 for(int i =1 ;i < strlen(num) ;i++) 16 { 17 sum*=10; 18 sum+=(num[i]-‘0‘); 19 } 20 int tt = sum - tm; 21 int i; 22 for( i = tt ; i < sum ; i++) 23 { 24 int ga = i; 25 int flag = 0; 26 while(ga > 0) 27 { 28 flag+=ga%10; 29 ga = ga/10; 30 } 31 if((i+flag)==sum) 32 { 33 printf("%d\n",i); 34 break; 35 } 36 } 37 if(i==sum) puts("0"); 38 } 39 return 0 ; 40 }