生成元(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; i < n; ++i)
{
a = b = i;
while (b)
{
a += b % 10;
b /= 10;
}
if (a == n)
{
ans = i;
break;
}
}
printf("%d\n", ans);
}
return 0;
}

时间: 2024-07-30 13:51:55

生成元(Digit Generator,ACM/ICPC Seoul 2005,UVa 1583)的相关文章

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

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

得分(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; }

分子量(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 +

数数字 (Digit Counting,ACM/ICPC Danang 2007,UVa 1225)

思路: 利用java 特性,将数字从1 一直加到n,全部放到String中,然后依次对strring扫描每一位,使其carr[str.charAt(i)-'0']++; 最后输出carr[i],即可. 13 string=12345678910111213 carr[1]++.carr[2]++.carr[3]++....carr[1]++.carr[1]++.carr[1]++.carr[2]++.carr[1]++.carr[3]++ AC Code: import java.util.Sc