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, 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 , 1N100, 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

Sample Output

198
0
1979

CODE#include"stdio.h"int main(){    int N;    scanf("%d",&N);    while(N--)    {        int x;        scanf("%d",&x);        int i,j,k,l,m,n;        int t=x,sum=0;        for(i=1; t/10!=0; i++)             t=t/10;        for(k=x-i*9; k<=x; k++)        {            t=k;            for(j=1; j<=i; j++)            {                sum+=t%10;                t/=10;            }            if(k+sum==x)            {                printf("%d\n",k);                break;            }            sum=0;            if(k==x)                printf("%d\n",0);        }    }    return 0;}

My Way这道题挺简单的,我一遍成功。首先是输入N控制输入的样例数目,然后在进行下一步的计算。对于这个题,最开始的想法就是穷举。但是这样非常的浪费内存,在循环的嵌套中又容易超时,于是我不得不想另外的方法。于是我在想,既然求这个数的生成元,那要看看这个数有几位就可以。如果这个数有4位,那么它的生成元最多加4*9=36就能成为这个数。那么我们只需用一个变量,来储存该数减去36的数,并以此为循环始值。这样就大大的减少了循环运算的次数和时间。并且,在后面计算该数每一位上的数字时也能用到这个位数。如此,通过这个程序,就很轻松的解决掉了这个问题。不过,我不能因为一次的顺利就得意,真正的考验还没有开始呢!加油!!!
时间: 2024-10-16 17:00:10

Problem 005——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 

素数筛法--SPOJ Problem 2 Prime Generator

质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单,看它是否能被2到sqrt(N)之间的整数整除即可. def isPrime(n): if n%2==0: return False for i in xrange(3,int(math.sqrt(n)+1),2): if n%i==0: return False return True 不过要找出1

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(水)

题目链接: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.

最小生成元 (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", &