紫书 习题 2-6 排列(permutation)

题目:用1,2,。。。,9组成3个三位数 且 三个三位数的比为1:2:3 而且要求数字不能重复 每行一个解

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <cstring>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     int a[3], b[10];
12     int i, j;
13     for(a[0]=100;a[0]<=333;a[0]++)
14     {
15         a[1] = a[0] * 2;
16         a[2] = a[0] * 3;
17         /*下面的循环是把三个三位数的个位全部放入一个数组
18         */
19         for(i=0, j=0;i<3;i++)
20         {
21             b[j] = a[i] / 100;
22             b[j+1] = a[i] /10 % 10;
23             b[j+2] = a[i] % 10;
24             j = j+ 3;
25         }
26         for(i=0;i<10;i++) //检验b数组中有没有重复数字
27         {
28             for(j=i+1;j<10;j++)
29             {
30                 if(b[i]==b[j])
31                     goto A;
32             }
33         }
34         printf("%d %d %d\n", a[0], a[1], a[2]);
35         A:continue;
36     }
37     return 0;
38 }

 

posted @ 2017-02-21 10:36  码农CHQ  阅读(208)  评论(0编辑  收藏  举报