59.填表格
将1、2、3、4、5和6 填入下表中,要使得每一列右边的数字比左边的数字大,每一行下面的数字比上面的数字大。按此要求,可有几种填写方法?
. . .
. . .
*问题分析与算法设计
按题目的要求进行分析,数字1一定是放在第一行第一列的格中,数字6一定是放在第二行第三列的格中。在实现时可用一个一维数组表示,前三个元素表示第一行,后三个元素表示第二行。先根据原题初始化数组,再根据题目中填 写数字的要求进行试探。
*程序与程序注释
#include<stdio.h>
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
正文
59.填表格2005-09-10 15:20:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/xiangyu/1709.html
阅读(4909) | 评论(2)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论