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 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

要求是根据输入的数找到其最小生成元,我一开始的思路是枚举,但量太多,想用判断来缩小枚举范围。设输入数num,当num=99999时的各个位数的最大和为45,也就是说只要在[num-45,num]这个区间进行枚举,就可以比较轻松地得到答案。以下用C写的答案:

#include<stdio.h>
int main()
{int ans=0,turns,num,i;
scanf("%d",&turns);while(turns>0){
scanf("%d",&num);
for(i=num-45;i<num;i++)
{if((i+i%10000/1000+i%1000/100+i%100/10+i%10)==num)
{ans=i; break;}}
if(!ans)printf("No answers\n");
else printf("%d\n",ans);turns--;ans=0;
}
return 0;
}
				
时间: 2024-10-31 09:04:45

Digit Generator, ACM/ICPC Seoul 2005, UVa1583的相关文章

最小生成元 (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的生成元

生成元(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

【紫书】例题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. [代码实现] 方法1 1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n = 0; 9 while ( scanf ("%d", &a

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

#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int t, n, a, b, ans, l;int main(){ scanf("%d", &t);//这句话是为了确定一个最大的范围,比如说10000 while (t--) { scanf("%d", &n); ans = 0; for (int i = n - 50;

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

生成元:如果 x 加上 x 各个数字之和得到y,则说x是y的生成元. n(1<=n<=100000),求最小生成元,无解输出0. 例如:n=216 , 解是:198 198+1+9+8=216 解题思路:打表 循环将从1到10005(大点也可以)进行提前写好. 例如: 1  1+1=2,-->  arr[2]=1 13 13+1+3=17,-->arr[17]=13 34  34+3+4=41, -->arr[41]=34 打完表后,直接将给的数作为下标,输出即可. #inc

得分(Score, ACM/ICPC Seoul 2005,UVa 1585)

#include<cstdio>#include<cstdlib>#include<cstring>int main(){ char s[80];//输入OOXXOXXOOO,最终得分计算为1+2+0+0+1+0+0+1+2+3=10 int m = 0, sum = 0, i = 0; scanf("%s", s); for (i = 0; i < strlen(s); i++) { if (s[i] == 'X') m = 0; if (s

得分(Score,ACM/ICPC Seoul 2005,UVa 1585)

#include<stdio.h> int main(void) { char b; int t,cou,sum; scanf("%d",&t); getchar(); while(t--) { cou=sum=0; while((b=getchar())!='\n') { if(b=='O')sum+=++cou; else cou=0; } printf("%d\n",sum); } return 0; }

环状序列(CircularSequence,ACM/ICPC Seoul 2004,UVa1584)

Question 例题3-5 环状序列(CircularSequence,ACM/ICPC Seoul 2004,UVa1584) 长度为n的环状串有n种表示方法,分别为从某个位置开始顺时针得到,在这些排列中字典顺序最小的称"最小表示". 如CTCC的最小表示为CCCT,CGAGTCAGCT的最小表示为AGCTCGAGTC. 提示:对于两个字符串,从第一的字符开始比较,当某一个位置的字符不同时,该位置字符较小的串,字典序小,如果一个字符串没有更多的字符,但是另一个字符串还没结束,则较短

分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf("%s", s); double sum = 0; for (int i = 0; i < strlen(s); i++) { if (s[i] == 'C') sum += (s[i + 1] - 48) * 12.01; if (s[i] == 'H') { if (s[i +