Digit Generator(水)

题目链接: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 }
				
时间: 2024-10-11 21:19:54

Digit Generator(水)的相关文章

UVa 1583 Digit Generator(数)

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 

Problem 005——Digit Generator

1583 - Digit Generator 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,

UVa 1225 Digit Counting --- 水题

UVa 1225 题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字 解题思路:用一个cnt数组记录0-9这10个数字出现的次数,先将cnt初始化为0,接着让i从1枚举到n, 对每个i,处理以活的i的每一个位置上的数,并在相应的cnt下标上+1 最后输出cnt数组即可 /* UVa 1225 Digit Counting --- 水题 */ #include <cstdio> #include <cstring

uva 1583 B - Digit Generator (暴力)

B - Digit Generator Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description For a positive integer N<tex2html_verbatim_mark> , the digit-sum of N<tex2html_verbatim_mark> is defined as the sum of N&l

UVA1583 UVALive3355 Digit Generator

Regionals 2005 >> Asia - Seoul 问题链接:UVA1583 UVALive3355 Digit Generator.基础训练级的题,用C语言编写. 看题意,请点击上述链接. 查表法最为合理,即使试探法可能性太多,而且求最小的生成元比较困难. 不打表估计时间上也会超时. 程序中,打表功能封装到函数maketable(),使得主函数变得简洁.另外,打表时需要注意数组下标溢出问题.还需要注意,求的是最小生成元. AC通过的C语言程序如下: /* UVA1583 UVALi

最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583)

Question 例题3-5 最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583) 如果x+x的各个数字之和得到y,就是说x是y的生成元.给出n(1<=n<=100000), 求最小生成元.无解输出0.例如,n=216,121,2005时的解分别是198,0,1979. Think 方法一:假设所求生成元记为m,不难发现m<n.换句话说,只需枚举所有的m<n,看看有木有哪个数是n的生成元.此举效率不高,因为每次计算一个n的生成元

UVa1583 Digit Generator

#include <stdio.h> int main(){    int T, N, i, k, digitsum, generator;    scanf("%d", &T);    while (--T >= 0)    {        scanf("%d", &N);        for (i = 45; i > 0; --i)        {            generator = N - i;     

生成元(Digit Generator,ACM/ICPC Seoul 2005, UVa1583)

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

UVa 1583 Digit Generator(数学)

 题意 如果a加上a所有数位上的数等于b时 a称为b的generator  求给定数的最小generator 给的数n是小于100,000的  考虑到所有数位和最大的数99,999的数位和也才45  因此我们只需要从n-45到n枚举就行了 #include<cstdio> #include<cstring> using namespace std; int t, n, a, b, ans, l; int main() { scanf ("%d", &