《算法竞赛入门经典2ndEdition 》例题3-5 生成元(Digit Generator, Uva1583)
2015-11-29 14:58
411 查看
这题还是比较简单的,然而更加觉得自己真心蒟蒻,可以在保存时就只保存最小的,然而我是保存了所有的,在输出时才比较。
#include <cstdio> #include <cstring> using namespace std; const int maxn = 100000; int g[maxn + 20][maxn/100];//g[i][j] is a generator of i,(j > 0) // g[i][0] is the number of generators of i. int main() { //freopen("New Text Document.txt","r",stdin); //freopen("Output.txt","w",stdout); int t; memset(g, 0, sizeof(g)); scanf("%d", &t); for(int i = 1; i <= maxn; i++) { int x = i, y = 0; while(x != 0) { y += x%10; x /= 10; } if(i + y <= maxn) g[i+y][++g[i+y][0]] = i; } for(int i = 1; i <= t; i++) { int n; scanf("%d",&n); int min = maxn*2; for(int i = 1; i <= g [0]; i++) if(g [i] < min) min = g [i]; if(min == maxn*2) printf("0\n"); else printf("%d\n",min); } //getchar();getchar(); return 0; }
下面这个是刘汝佳标程,里面的
while(T--)感觉很巧妙,之前我都是在循环体里面进行
T--然后在循环条件是判断
T >= 0,另外的确不需要保存所有的生成元,上面我的那个程序在这点上的确做的不好。
// UVa1583(LA3355) Digit Generator // Rujia Liu #include<stdio.h> #include<string.h> #define maxn 100005 int ans[maxn]; int main() { int T, n; memset(ans, 0, sizeof(ans)); for(int m = 1; m < maxn; m++) { int x = m, y = m; while(x > 0) { y += x % 10; x /= 10; } if(ans[y] == 0 || m < ans[y]) ans[y] = m; } scanf("%d", &T); while(T--) { scanf("%d", &n); printf("%d\n", ans ); } return 0; }
相关文章推荐
- 《算法竞赛入门经典》解题索引
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- UVA11367 加满油 解题报告