算法竞赛入门经典 习题 2-10 排列(permutation)
2017-07-04 12:47
393 查看
习题 2-10
用1,2,3。....,9组成3个三位数abc。def和ghi,每一个数字恰好使用一次,要求abc:def:ghi=1:2:3。输出全部解。
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { int abc, def, ghi; int a[10], count = 0; memset(a, 0, sizeof(a)); // printf("n\n"); for(abc = 123; abc <= 329; abc++) { def = 2*abc; ghi = 3*abc; a[abc/100] = a[abc/10%10] = a[abc%10] = 1; a[def/100] = a[def/10%10] = a[def%10] = 1; a[ghi/100] = a[ghi/10%10] = a[ghi%10] = 1; int i; for( i = 1; i <= 9; i++) count += a[i]; if(count == 9) printf("%d %d %d\n", abc, def, ghi); count = 0; memset(a, 0, sizeof(a)); } system("PAUSE"); return 0; }
总结:1 将全部可能出现的数字作为一个一维数组的下标,最后推断之和是否为9,假设小于9,必有重合。反之每一个数字仅仅有一个
2 推断过后。count和数组要清零。
相关文章推荐
- 算法竞赛入门经典 习题 2-10 排列(permutation)
- 《算法竞赛入门经典》习题2-10 排列(permutation)
- 算法竞赛入门经典 习题2-10 排列(permutation)
- 算法竞赛入门经典(第二版) 习题2-6 排列(permutation)
- 习题2-10 排列permutation
- 算法竞赛入门经典 习题2-10 排列(permutation)
- 习题 2-10 排列(permutation)
- 算法竞赛入门经典 习题2-10 排列(permutation)
- 算法竞赛入门经典习题2-10排列
- 算法竞赛入门经典习题2-6 排列(permutation)
- 【紫书】习题2-6 排列(permutation)
- 习题2-6 排列(permutation)(java版本)
- 排列 permutation 算法竞赛入门经典第二版 习题2-6
- 算法竞赛入门经典(第2版)习题4-10 洪水!UVa815
- 算法竞赛入门经典-课后练习-2-10-排列
- 算法竞赛入门经典第四章习题4-10 Flooded! UVA - 815
- 算法竞赛入门经典习题2-10
- 算法竞赛入门经典 第二版 习题4-10 洪水 Flooded uva815
- <<算法竞赛入门经典>> 习题2-10
- 习题2-6 排列(permutation)