将1到9 这九个数字分成三个3位数,分求第一个3位数,正好是第二个3位数的二倍,是第三个3位数的三倍。问应当怎样分法。
*问题分析与算法设计
问题中的三个数之间是有数学关系的,实际上只要确定第一个三位数就可以解决问题。
试探第一个三位数之后,计算出另外两个数,将其分别分解成三位数字,进行判断后确定所试探的数是否就是答案。
需要提醒的是:试探的初值可以是123,最大值是333。因为不可能超出该范围。
No. :1
, 1 2 3
, 4 5 6
No. :2
, 1 2 4
, 3 5 6
No. :3
, 1 2 5
, 3 4 6
No. :4
, 1 3 4
, 2 5 6
No. :5
, 1 3 5
, 2 4 6
*问题分析与算法设计
问题中的三个数之间是有数学关系的,实际上只要确定第一个三位数就可以解决问题。
试探第一个三位数之后,计算出另外两个数,将其分别分解成三位数字,进行判断后确定所试探的数是否就是答案。
需要提醒的是:试探的初值可以是123,最大值是333。因为不可能超出该范围。
*程序与程序设计
1 #include <stdio.h>
2 int count;
3 int judl(int s[]);
4 int print(int u[]);
5 int main()
6 {
7 static int a[] = {1, 2, 3, 4, 5, 6};
8 printf("The possible table satisfied above conditions are :\n");
9 for (a[1] = a[0] + 1; a[1] <= 5; ++a[1])
10 for (a[2] = a[1] + 1; a[2] <= 5; ++a[2])
11 for (a[3] = a[0] + 1; a[3] <= 5; ++a[3])
12 for (a[4] = a[1] > a[3] ? a[1] + 1 : a[3] + 1; a[4] <= 5; ++a[4])
13 {
14 if (judl(a))
15 {
16 print(a);
17 }
18 }
19
20 return 0;
21 }
22
23 int judl(int s[])
24 {
25 int i, l;
26 for (l = 1; l < 4; l++)
27 {
28 for (i = l+ 1; i < 5; ++i)
29 {
30 if (s[l] == s[i])
31 {
32 return 0;
33 }
34 }
35 }
36 }
37
38 int print(int u[])
39 {
40 int k;
41 printf("\nNo. :%d", ++count);
42 for (k = 0; k < 6; k++)
43 {
44 if (0 == k%3)
45 {
46 printf("\n, %d ", u[k]);
47 }
48 else
49 {
50 printf("%d ", u[k]);
51 }
52 }
53 }
The possible table satisfied above conditions are :
No. :1
, 1 2 3
, 4 5 6
No. :2
, 1 2 4
, 3 5 6
No. :3
, 1 2 5
, 3 4 6
No. :4
, 1 3 4
, 2 5 6
No. :5
, 1 3 5
, 2 4 6