例题 3-5 生成元 digit generator

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define maxn 100005
 4 int ans[maxn];           //类似于 比较大的数组还是开导外面比较好一点,防止报错.
 5 int main()
 6 {
 7     int x,y,m,T,n;
 8     memset(ans,0,sizeof(ans));  //数组归零.
 9     for(m=1;m<maxn;m++)         //从  1  开始  遍历到  maxn.
10     {
11         x=y=m;                  //  将  x和y 都赋值为    m.
12         while(x>0)              //x代表这   一个个的位数.
13         {
14             y=y+x%10;          //处理  加起来...
15             x=x/10;
16         }
17         if(ans[y]==0||m<ans[y])       //  这个就用来确定   赋值的为  最小值  //后面那个   就是画蛇添足了
18             ans[y]=m;
19     }
20     scanf("%d",&T);
21     while(T--)
22     {
23         scanf("%d",&n);
24         printf("%d\n",ans[n]);
25     }
26     return 0;
27 }

感觉第十七行 那里的     m<maxn[y]                  ........纯属画蛇添足..

时间: 2024-08-25 16:13:44

例题 3-5 生成元 digit generator的相关文章

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

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,

UVA1583 UVALive3355 Digit Generator

Regionals 2005 >> Asia - Seoul 问题链接:UVA1583 UVALive3355 Digit Generator.基础训练级的题,用C语言编写. 看题意,请点击上述链接. 查表法最为合理,即使试探法可能性太多,而且求最小的生成元比较困难. 不打表估计时间上也会超时. 程序中,打表功能封装到函数maketable(),使得主函数变得简洁.另外,打表时需要注意数组下标溢出问题.还需要注意,求的是最小生成元. AC通过的C语言程序如下: /* UVA1583 UVALi

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 

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

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.

【紫书】例题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

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