C趣味程序百例(18)填表格 |
|
www.nanhushi.com 佚名 不详 |
59.填表格 将1、2、3、4、5和6 填入下表中,要使得每一列右边的数字比左边的数字大,每一行下面的数字比上面的数字大。按此要求,可有几种填写方法? *问题分析与算法设计 按题目的要求进行分析,数字1一定是放在第一行第一列的格中,数字6一定是放在第二行第三列的格中。在实现时可用一个一维数组表示,前三个元素表示第一行,后三个元素表示第二行。先根据原题初始化数组,再根据题目中填 写数字的要求进行试探。 *程序与程序注释 #include int jud1(int s[]); void print(int u[]); int count; /*计数器*/ void main() { static int a[]={1,2,3,4,5,6}; /*初始化数组*/ printf("The possble table satisfied above conditions are:\n"); for(a[1]=a[0]+1;a[1]<=5;++a[1]) /*a[1]必须大于a[0]*/ for(a[2]=a[1]+1;a[2]<=5;++a[2]) /*a[2]必须大于a[1]*/ for(a[3]=a[0]+1;a[3]<=5;++a[3]) /*第二行的a[3]必须大于a[0]*/ for(a[4]=a[1]>a[3]?a[1]+1:a[3]+1;a[4]<=5;++a[4]) /*第二行的a[4]必须大于左侧a[3]和上边a[1]*/ if(jud1(a)) print(a); /*如果满足题意,打印结果*/ }
int jud1(int s[]) { int i,l; for(l=1;l<4;l++) for(i=l+1;i<5;++i) if(s[l]==s[i]) return 0; /*若数组中的数字有重复的,返回0*/ return 1; /*若数组中的数字没有重复的,返回1*/ }
void print(int u[]) { int k; printf("\nNo.:%d",++count); for(k=0;k<6;k++) if(k%3==0) /*输出数组的前三个元素作为第一行*/ printf("\n%d",u[k]); else /*输出数组的后三个元素作为第二行*/ printf("%d",u[k]); } *运行结果 The possble table satisfied above conditions are: No.1: No.2: No.3: No.4: No.5: 1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 4 5 6 3 5 6 3 4 6 2 5 6 2 4 6
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: C趣味程序百例(18)拉丁方 下一篇文章: C趣味程序百例(18)1~9分成1:2:3的三个3位数 |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|