C语言上机试题:10
1.题目:编一C程序,它能读入一个正整数n(n < 20),再读入二个n*n的矩阵(矩阵元素为整数,输入时相邻的整数用空格隔开),分别判断每个矩阵是否为对称矩阵,若是,相应输出为Yes,否则为No。(注:程序命名为e10_1.exe)
程序设计:
main()
{int n,a[20][20],b[20][20],i,j;
char fa='y',fb='y';
printf("Please enter n(<20)=");
scanf("%d",&n);
printf("\n");
printf("Please enter a[%d][%d]:\n",n,n);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Please enter b[%d][%d]:\n",n,n);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&b[i][j]);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if (a[i][j]!=a[j][i])
{fa='n';
break;
}
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if (b[i][j]!=b[j][i])
{fb='n';
break;
}
if (fa=='y') printf("a[%d][%d] yes\n",n,n);
else printf("a[%d][%d] no\n",n,n);
if (fb=='y') printf("b[%d][%d] yes\n",n,n);
else printf("b[%d][%d] no\n",n,n);
}
2.题目:设 y(n) = 1 n<= 1时
y(n) = y(n-1)*2+y(n-2) n>1时
编一C程序,它能对读入的任意n(n>=0且n<50),能计算并输出y(n)的值。(注:程序命名为e10_2.exe)
程序设计:
int y(n1)
int n1;
{if (n1<=1) return (1);
else return (y(n1-1)*2+y(n1-2));
}
main()
{int n;
printf("Please enter n(>=0 and <50)=");
scanf("%d",&n);
printf("\n");
printf("y(%d)=%d\n",n,y(n));
}