C语言上机试题共29页文档.doc
如有侵权,请联系网站删除,仅供学习与交流C语言上机试题【精品文档】第 57 页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)#include "stdio.h"#include "string.h"#define MAX 100void main()char aMAX;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<i;j+) if (aj>='0' && aj<='9') n+; printf("the length is :%d n",n);第三题编一C程序,它能读入一正整数n,(n<10),并输出如下图形: 1 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<charnum;i+) printf("%c ",printchar); for(j=i;j<=i+printnum-1;j+) printf("%d ",a); printf("n");void main() int n,printnum,i,charnum; char printchar=' ' printf("please input the n:n"); scanf("%d",&n); if (n>=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 20void main() int l,m,n,i,k,j,o,aMAXMAX,bMAXMAX,cMAXMAX,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<l<20):n"); scanf("%d",&l); if (m>=20 |m<=0) printf("please input m again (0<m<20):n"); scanf("%d",&m); if (n>=20 |n<=0) printf("please input n again (0<n<20):n"); scanf("%d",&n); printf("please input the number in a%d%d:n",l,m); for(i=0;i<l;i+) for(j=0;j<m;j+) scanf("%d",&aij); printf("please input the number in b%d%d:n",m,n); for(i=0;i<m;i+) for(j=0;j<n;j+) scanf("%d",&bij); for(i=0;i<l;i+) for(k=0;k<n;k+) tmp=0; for(j=0;j<m;j+) tmp+=aij*bjk; cik=tmp; printf("the result is:n"); for(o=0;o<i;o+) for(j=0;j<k;j+) printf("%d ",coj); printf("n"); 5.编一C程序,它能读入n(n<200)个整数(以-9999为结束标记,-9999不算在内,相邻的两个整数用空格隔开),找出第1到第n-1个数中第一个与第n个数相等的那个数,并输出该数读入时的序号(序号从1开始记)。(可执行文件名为e2_3.exe)#include "stdio.h"#define MAX 200void main() int i,j,n,flag=0,tmp,aMAX; printf("please input the number :n"); for(i=0;i<MAX;i+) scanf("%d",&ai); if (ai=-9999) break; n=-i; tmp=ai; for(i=0;i<n && flag=0;i+) if (tmp=ai) j=i; flag=1; break; if (flag=0) printf("Sorry! there is not found!n"); else if(flag=1) printf("the aquality number place is :n"); printf("%d",+j); printf("n");6.编一C程序,它能读入一正整数n(n<40),并输出如下图形: A A A . A A A A . A . . A A A . A 共n行,每行n个A,下一行比上一行右移一列.(注:可执行文件名为e3_1.exe)#include "stdio.h"#define MAX 40void main() int n,i,m,j; char printchar=' ',printnum='A' printf("please input the n:n"); scanf("%d",&n); if (n>=40 | n<=0) printf("please input the n again(0<n<%d) :n",MAX); scanf("%d",&n); for(i=0;i<n;i+) for(j=0;j<=i;j+) printf("%c",printchar); for(m=0;m<n;m+) printf("%c",printnum); printf("n"); 7.编一C程序,它能读入一个正整数,并输出该正整数的17进制形式,依次用A、B、C、D、E、F、G表示10、11、12、13、14、15、16。(注:可执行文件名为e3_2.exe)#include "stdio.h"void main() int n,i=0,j,b17; char a17="0123456789ABCDEFG" printf("please input the n :n"); scanf("%d",&n); do j=n%17; bi+=j; n=n/17; while(n!=0); for(i-;i>=0;i-) j=bi; printf("%c",aj); printf("n");第八题编一C程序,它能读入两个整数m与n,计算并输出m与n的绝对值的最大公约数及最小公倍数。(注:可执行文件名为e3_3.exe)分析:求最大公约数的算法思想:(最小公倍数=两个整数之积/最大公约数) (1) 对于已知两数m,n,使得m>n; (2) m除以n得余数r; (3) 若r=0,则n为求得的最大公约数,算法结束;否则执行(4); (4) mn,nr,再重复执行(2)。#include "stdio.h"void main() int nm,r,n,m,t; printf("please input two numbers:n"); scanf("%d %d",&m,&n); if (m<0) m*=-1; if (n<0) n*=-1; nm=n*m; if (m<n) t=n; n=m; m=t; r=m%n; while (r!=0) m=n; n=r; r=m%n; printf("最大公约数:%dn",n); printf("最小公倍数:%dn",nm/n); 9.编一C程序,它能读入一个13进制数(用字符串表示,用A、B、C分别表示10、11和12),输出该数的值(按十进制数输出)。如输入为3B时,输出为50。(注:可执行文件名为e4_1.exe)#include "stdio.h"#include "stdlib.h"void main(void)char str64,*p;unsigned long x;printf("input the number:n");gets(str);x=strtoul(str,&p,13);printf("the number is %ldn",x);10.任何进制转换成十进制#include "stdio.h"#include "stdlib.h"void main(void)char str64,*p;unsigned long x;printf("input the number:n");gets(str);x=strtoul(str,&p,13); /*只是这里该是几进制就改为几 */printf("the number is %ldn",x);11.题编一C程序,它能依次读入三组d1、r1、d2、r2、d3、r3共18个非负整数(其中0<=r1<d1,0<=r2<d2,0<=r3<d3),对每组6个非负整数依次判断是否有正整数n,满足n%d1=r1且n%d2=r2且n%d3=r3,若有,则输出其中最小者;否则,输出No。(输入整数时,相邻的两个用空格隔开)。(注:可执行文件名为e4_2.exe)#include "stdio.h"void main() int l,i,n,d1,r1,d2,r2,d3,r3; for(i=0;i<=2;i+) l=1; n=1; printf("please input the number (0<=r1=<d1 0<=r2=<d2 0<=r3=<d3):n"); /*别犯低级错误!scanf(“%dn”,&n); 千万别那样写 */ scanf("%d %d %d %d %d %d",&d1,&r1,&d2,&r2,&d3,&r3); while(l!=0)&&(n<=d1*d2*d3) ) if (n%d1=r1) && (n%d2=r2) && (n%d3=r3) printf("%dn",n); l=0; else n+; if (l!=0) printf("NOn"); 12.编一C程序,它能读入10个用空格隔开的整数n1,n2,n10,对于i=1,2,10,执行:若ni是某个整数的5次方,则输出yes,否则输出no。#include "stdio.h"void main() int a10,i,j,m,n=10; printf("please enter 10 numbers:n"); for (i=0;i<n;i+) scanf("%d",&ai); for (i=0;i<10;i+) if (ai>0) if (ai=1) printf("%d yes",ai); else m=0; for (j=0;j<=ai/5;j+) if (j*j*j*j*j=ai) m=1; break; if (m=1) printf("%d yes",ai); if (m=0) printf("%d no",ai); else if (ai<0) n=0; for (j=0;j>=ai;j-) if (j*j*j*j*j=ai) n=1; break; if (n=1) printf("%d yes",ai); if (n=0) printf("%d no",ai); else if (ai=0) printf("%d YES ",ai); printf("n"); 13.编一C程序,它能读如若干浮点数f1、f2、.fn(以-9999.0为结束标记,-9999.0不算在内)并输出在f2、f3、.fn这n-1个数中大于f1的数的个数、等于f1的数的个数和小于f1的数的个数。(注:可执行文件名为e4_3.exe)include "math.h"#include "stdio.h"void main() float f100,f1; int n=0,i,i1=0,i2=0,i3=0; printf("Please enter float number :n"); scanf("%f",&f1); f0=f1; while (fn!=-9999.0) scanf("%f",&f+n); for (i=1;i<=n-1;i+) if (fi>f1) i1+; else if (fi<f1) i3+; else i2+; printf(">f1:%dn",i1); printf("=f1:%dn",i2); printf("<f1:%dn",i3); 14.输入一个整数x和一个正整数n,求x(x-1)(x-2)(x-3).(x-n+1)的值并输出.好象上次还考过呢.用递归,这里只给出算法,没有输入输出,没有溢出处理int Get(int x,int n) if(n = 0) return 1; /最后一步,相当于x*1 else return (x-n+1) * Get(x,n-1); void main() int r; r = Get(-5,3);15.编一C程序,它能读入若干个非零浮点数(相邻的两个数用空格隔开,以0.0为输入结束标记,0.0不算在内),计算并输出这些数的平均值。(注:可执行文件名为e5_1.exe)#include "math.h"#include "stdio.h"void main() float f,s=0.0; int n=0; printf("Please enter float number:n"); scanf("%f",&f); if (fabs(f-0.0)<1e-4) printf("No number!n"); else while (fabs(f-0.0)>1e-4) n+; s+=f; scanf("%f",&f); if (n!=0) printf("%d number of average is:%fn",n,s/n); 16.编一C程序,它能反复读入正整数(相邻的用空格隔开),并判断它是否为素数(质数),若是,则输出“yes”,否则,输出“no”,直到读入1时,程序终止执行。(注:可执行文件名为e5_2.exe)#include "stdio.h"#define MAX 10000void test (int a) int j,testdiv=2; if (a>2) for(j=0;testdiv<=a/2 && j=0;testdiv+) if (a%testdiv=0) j=1; break; if (j=1 | a<=1) printf("NO "); else printf("YES ");void main() int n,i,aMAX; printf("please input the number :n"); for(i=0;i<MAX;i+) scanf("%d",&ai); if (ai=1) break; n=i; printf("the result is :n"); for(i=0;i<n;i+) test(ai); printf("n");17.编一C程序,它能读入21个整数(相邻的两个数用空格隔开),并输出值为中间的整数的值(即恰有10个值小于它,恰有10个值大于它)。(注:可执行文件名为e5_3.exe)#include "stdio.h"void main()int a21,i,tmp,j;printf("please input the number :n");for(i=0;i<21;i+) scanf("%d",&ai);for(i=0;i<21;i+) for(j=i+1;j<21;j+) if (ai<aj) tmp=ai; ai=aj; aj=tmp; printf("the middle number is :n"); printf("%dn",a10);18.编一个程序,它能读入文件和中的所有整数,并把这些数按从小到大的次序写到文件中,文件中的相邻两个整数都用空格或换行符隔开, 相同的数只能写一次,十个数一行)。(注:可执行文件名为e9_2.exe)#include "stdio.h"#include "stdlib.h"int o=0; /*设置一个全局变量使O等于去掉重复数后的总的数据个数 */int clearSort(int a,int quantity) /*将重复的数去掉并且改掉总的数据个数*/int i,j,m;for(i=0;i<quantity-1;i+) for(j=i+1;j<quantity;j+) if(ai=aj) for(m=j+1;m<=quantity;m+) am-1=am; quantity-; j-; o=quantity; return(quantity) ;void main(void)FILE *fp1,*fp2,*fp3;int a100,b100,i,x,n,j,p,tmp; system("cls"); /* 清屏 */if (fp1=fopen("f1.c","wb")=NULL) printf("the file f1.c can not open !n"); exit(0); printf("please input the number in f1.c:n"); x=9999; while (x!=-9999) scanf("%d",&x); if (x!=-9999) fwrite(&x,sizeof(int),1,fp1); fclose(fp1); if (fp2=fopen("f2.c","wb")=NULL) printf("the file f2.c can not open !n"); exit(0); printf("please input the number in f2.c:n"); x=9999; while (x!=-9999) scanf("%d",&x); if (x!=-9999) fwrite(&x,sizeof(int),1,fp2); fclose(fp2); if (fp1=fopen("f1.c","rb")=NULL) printf("the file f1.c can not open !/n"); exit(0); if (fp2=fopen("f2.c","rb")=NULL) printf("can not open the file f2.c!n"); exit(0); if (fp3=fopen("f3.c","wb")=NULL) printf("can not open the file f3.c!n"); exit(0); i=-1; while(!feof(fp1) fread(&a+i,sizeof(int),1,fp1); /*将FP1文件中的数据写入a数组中 */ fclose(fp1); i-; rewind(fp2); while(!feof(fp2) fread(&a+i,sizeof(int),1,fp2); /*紧接着FP1 FP2也将数据写入a数组中*/ fclose(fp2); n=i; for(j=0;j<n;j+) /* 排序*/ for(p=j+1;p<n;p+) if (aj>ap) tmp=aj; aj=ap; ap=tmp; clearSort(a,n); /*调用子函数 */ fwrite(a,sizeof(int),o,fp3); /*将数据写到文件FP3中*/ fclose(fp3); if(fp3=fopen("f3.c","rd")=NULL) printf("can not open f3.c !n"); exit(0); i=0; while(!feof(fp3) /*这里开始输出并且每行十个 */ fread(&bi,sizeof(int),1,fp3); /*将FP3文件中的数据写到B数组中*/ printf("%d ",bi+); if(i%10=0) printf("n"); if (i>=o) break; printf("n"); 第十九题 编一C程序,它能读入一正整数n,并输出如下图形: 1 2 2 2 3 3 3 3 3 . AAAA .AAAA BBBBBBB,. BBBBBBB . . nnnnnnn n . . . . . nnnnnnnnn (共2n-1个n) . BBBBBBBBB.BBBBBBBBBB AAAAAA.AAAAAAAA . . 3 3 3 3 3 2 2 2 1当n>9时输出A,B C.:<=9时输出12345.9#include "stdio.h"void print(int num ,char shu,char row,int rows) /* num是要输出的字符的个数 shu要输出的字符 row 是空格 rows 是空格的个数 */int m,j;for(m=0;m<rows;m+)printf("%c ",row);j=m; for(;j<m+num;j+)printf("%c ",shu); for(m=0;m<rows;m+)printf("%c ",row); /*到这里还没换行呢 */printf("n");void print11(int num ,int shu,char row,int rows) int m,j;for(m=0;m<rows;m+)printf("%c ",row);j=m; for(;j<m+num;j+)printf("%d ",shu); for(m=0;m<rows;m+)printf("%c ",row); printf("n");void main(void)int i,n,printnum,rows,j;char rownum,a52="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",printchar;int printchar1;printf("please input the number :n");scanf("%d",&n); for(i=1,printnum=1,printchar='1',rownum=' ',rows=n-i;i<=n;printnum+=2,i+,rows-,printchar+) if (i<=9)print(printnum,printchar,rownum,rows);else if (i>9) for(j=0;j<n-9;j+,printnum+=2,rows-,i+) print(printnum,aj,rownum,rows); break; for(printnum-=4,i-=2,rows=1;i>=1;printnum-=2,i-,rows+) if (i>9) for(j=n-11;j>=0;j-,printnum-=2,rows+,i-) print(printnum,aj,rownum,rows); if (i<=9) for(printchar1=i,printnum=2*i-1,rows=n-i;i>=0;printnum-=2,printchar1-,i-,rows+)pri