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<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> , 1N100, 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: [email protected]
 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\n",ans);
70     }
71
72
73  #ifndef ONLINE_JUDGE
74   fclose(stdin);
75   #endif
76     return 0;
77 }

时间: 2024-10-11 03:31:52

uva 1583 B - 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】12169-Disgruntled Judge(暴力or欧几里得)

可能由于后台数据的原因,这道题直接暴力枚举a,b进行判断也能过,不过跑的时间长,效率太差了. 14021006 12169 Disgruntled Judge Accepted C++ 0.876 2014-08-11 08:46:28 不说了,比较无脑. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #incl

UVA - 10162 Last Digit

Description  Problem B.Last Digit  Background Give you a integer number N (1<=n<=2*10100). Pleasecompute S=11+22+33+-+NN   Give the last digit of S to me. Input Input file consists of several Ns, each N a line. It is ended with N=0. Output For each

uva 10162 - Last Digit(数论)

题目链接:uva 10162 - Last Digit 题目大意:给定n,求s的个位的数值是多少. 解题思路:对于ii,重复周期为20,这样就有 1 4 7 6 5 6 3 6 9 0 1 6 3 6 5 6 7 4 9 0 但是这个周期的值是不为0的,总的话是100为一个大周期. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxt =

uva 10825 - Anagram and Multiplication(暴力)

题目链接:uva 10825 - Anagram and Multiplication 题目大意:给出m和n,要求找一个m位的n进制数,要求说该数乘以2~m中的任意一个数的结果是原先数各个位上数值的一个排序. 解题思路:枚举最后一位数,然后用这个数去乘以2~m并对n取模,然后得到的数一定就是这个数的组成,暴力搜索一下并判断. #include <cstdio> #include <cstring> #include <algorithm> using namespace

uva 11256 - Repetitive Multiple(gcd+暴力)

题目链接:uva 11256 - Repetitive Multiple 题目大意:给定一个数n,要求找到最小的k,使得k?n为题目中定义的重复数字. 解题思路:枚举k?n的循环节长度,比如当前枚举为2,那么一次判断u=1001,1001001,1001001001 ...,取d = gcd(n,u), 那么k = u / d, a = n / d (因为n?k=u?a)并且保证a的长度为2,所以k和a要同时扩大相应倍数.枚举过程中为何k. #include <cstdio> #include

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.