c语言上机试题

  • 格式:pdf
  • 大小:16.89 KB
  • 文档页数:15

可编辑word文档C上机题1. 设y(n) = 1 n<= 1时;y(n) = -y(n-2)+2*y(n-1) n>1时。编一C程序,它能对读入的任意n(n>=0且n<50),能计算并输出y(n)的值。(注:程序命名为e1_2.exe) #include "stdio.h" int y(n) int n; { if (n<=1) return 1; else return (-y(n-2)+2*y(n-1)); } void main() { int n; printf("please input the n :\n"); scanf("%d",&n); printf("y(%d)=%d \n",n,y(n)); } 2.编一C程序,它能读入一个字符串(串长<100,串中可能有空格符),计算并输出该字符串中十进制数字的个数。(注:程序命名为e1_3.exe) 可编辑word文档#include "stdio.h" #include "string.h" #define MAX 100 void main() { char a[MAX]; int n=0,i,j; printf("please input the char :\n"); gets(a); i=strlen(a); if (i>=100 || i<=0) printf("sorry! there is more than or less than one hundred number \n"); else if (i<100 && i>0) { for(j=0;j='0' && a[j]<='9') n++; printf("the length is :%d \n",n); } } 第三题编一C程序,它能读入一正整数n,(n<10),并输出如下图形:1 可编辑word文档2 2 2 3 3 3 3 3 . . n n . . . . . nn (共2n-1个n) . . 3 3 3 3 3 2 2 2 1 (可执行文件名为e2_1.exe) #include "stdio.h" void print(int a,int printnum,int charnum ,char printchar) { int i,j; for(i=0;i=10 || n<=0) { printf("please input n again (n<10 and n>0):\n"); scanf("%d",&n); } /*i为打印的数字printnum为打印数字的个数printchar为打印的空格charnum为打印的空格个数*/ for(i=1,printnum=2*i-1,charnum=n-i;i<=n;i++,printnum+=2,charnum--) print(i,printnum,charnum,printchar); for(i-=2,printnum-=4,charnum=1;i>=1;i--,printnum-=2,charnum++) print(i,printnum,charnum,printchar); } 4. 编一C程序,它能读入矩阵A的行数L,列数M,矩阵B的列数N,矩阵B的行数是M。L,M,N值都小于20), 以及按行读入矩阵A的L*M个元素(整数)及矩阵B的M*N个元素(整数),计算并输出A与B的矩阵乘积(按行,列输出)。(输入时,相邻的两个整数用空格隔开)。(可执行文件名为e2_2.exe) #include "stdio.h" #define MAX 20 可编辑word文档void main() { int l,m,n,i,k,j,o,a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],tmp=0; printf("please input the L :\n"); scanf("%d",&l); printf("please input the m :\n"); scanf("%d",&m); printf("please input the n :\n"); scanf("%d",&n); if (l>=20 ||l<=0) { printf("please input l again (0=20 ||m<=0) { printf("please input m again (0=20 ||n<=0) { printf("please input n again (0=40 || n<=0) { printf("please input the n again(0