资源描述:
《100道经典c语言编程试题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、100道经典c语言编程试题1.输入两个正整数,m和n,求其最大公约数和最小公倍数。#includevoidmain(){inthcf(int,int);/*函数声明*/intlcd(int,int,int);/*函数声明*/intu,v,h,l;printf("Pleaseinputtwonumbers:");scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d",h);l=lcd(u,v,h);printf("L.C.D=%d",l);}inthcf(intu,intv)
2、{intt,r;if(v>u){t=u;u=v;v=t;}while((r=u%v)!=0){u=v;v=r;}return(v);}intlcd(intu,intv,inth){return(u*v/h);}2.输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。#includeintletter,digit,space,others;voidmain(){voidcount(char[]);chartext[80];printf("Pleaseinputstring:");gets(text);printf("
3、string:");puts(text);letter=0;digit=0;space=0;others=0;count(text);printf("letter:%d,digit:%d,space:%d,others:%d",letter,digit,space,others);}voidcount(charstr[]){inti;for(i=0;str[i]!=' ';i++)if((str[i]>='a'&&str[i]<='z')
4、
5、(str[i]>='A'&&str[i]<='Z'))letter++;elseif(str[i
6、]>='0'&&str[i]<='9')digit++;elseif(str[i]==32)space++;elseothers++;}3.输入一个正整数求出它是几位数;输出原数和位数。#includeintdigit;voidmain(){voidcount(char[]);chartext[80];printf("Pleaseinputnumbers:");gets(text);printf("Numbers:");puts(text);digit=0;count(text);printf("digit:%d",
7、digit);}voidcount(charstr[]){inti;for(i=0;str[i]!=' ';i++)if(str[i]>='0'&&str[i]<='9')digit++;}4.输入一个正整数,输出原数并逆序打印出各位数字。#includevoidinvertLongInt(long);voidmain(){unsignedlongiNumber;printf("Pleaseinputanumber:");scanf("%ld",&iNumber);printf("Theinputnumberis:%ld
8、n",iNumber);printf("Theinversenumberis:");invertLongInt(iNumber);}voidinvertLongInt(longx){if(x>=0&&x<=9)printf("%d",x);else{printf("%d",x%10);invertLongInt(x/10);}}5.从键盘上输入若干学生的一门课成绩,统计并输出最高成绩和最低成绩及相应的序号,当输入负数时结束输入。6.从键盘上输入若干学生的一门课成绩,计算出平均分,当输入负数时结束输入。将结果输出。7.求1!+2!+3!+……+
9、20!,将结果输出。#includevoidmain(){floats=0,t=1;intn;for(n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+3!+……+20!=%e",s);}8.打印以下图案:****************#includevoidmain(){inti,j;printf("Thepictureis:");staticcharpicture[4][7]={{'','','','*'},{'','','*','*','*'},{'','*',
10、'*','*','*','*'},{'*','*','*','*','*','*','*'}};for(i=0;i<=3;i++){fo