uva 1583 B

B - Digit Generator
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

Description

Download as PDF
 

For a positive integer N<tex2html_verbatim_mark> , the digit-sum of N<tex2html_verbatim_mark> is defined as the sum of N<tex2html_verbatim_mark> itself and its digits. When M<tex2html_verbatim_mark> is the digitsum of N<tex2html_verbatim_mark> , we call N<tex2html_verbatim_mark> agenerator of M<tex2html_verbatim_mark> .

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<tex2html_verbatim_mark> test cases. The number of test cases T<tex2html_verbatim_mark> is given in the first line of the input. Each test case takes one line containing an integer N<tex2html_verbatim_mark> , 1$ le$N$ le$100, 000<tex2html_verbatim_mark> .

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<tex2html_verbatim_mark> for each test case. If N<tex2html_verbatim_mark> has multiple generators, print the smallest. If N<tex2html_verbatim_mark> 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

一开始从1开始枚举,tle了
稍微想一下就会发现..最多6位数..所以目标数字与n最多差6*9==54
所以直接从n-54枚举到n即可.
 1 /*************************************************************************
 2     > File Name: code/uva/1583.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年09月15日 星期二 16时47分20秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define y1 hust111qqz
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 #define lr dying111qqz
26 using namespace std;
27 #define For(i, n) for (int i=0;i<int(n);++i)  
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 int n ;
32 bool judge(int x,int y)
33 {
34     int r;
35     int sum = 0;
36     int tmp = y-x;
37     while (x)
38     {
39            
40     r = x % 10;
41     x = x/10;
42     sum = sum + r;
43     }
44     
45     if (sum==tmp) return true;
46     return false;
47 }
48 int main()
49 {
50   #ifndef  ONLINE_JUDGE 
51     freopen("in.txt","r",stdin);  
52   #endif
53     int T;
54     cin>>T;
55     while (T--)
56     {
57     scanf("%d",&n);
58 
59     int ans = 0 ;
60     int st = n-54;
61     for ( int i = st ; i <= n ;i++)
62     {
63         if (judge(i,n))
64         {
65         ans =  i;
66         break;
67         }
68     }
69     printf("%d
",ans);
70     }
71   
72   
73  #ifndef ONLINE_JUDGE  
74   fclose(stdin);
75   #endif
76     return 0;
77 }
View Code
原文地址:https://www.cnblogs.com/111qqz/p/4810883.html