C语言题库.docx
- 文档编号:12102827
- 上传时间:2023-04-17
- 格式:DOCX
- 页数:24
- 大小:20.94KB
C语言题库.docx
《C语言题库.docx》由会员分享,可在线阅读,更多相关《C语言题库.docx(24页珍藏版)》请在冰豆网上搜索。
C语言题库
●∙ 1、 输出小于 2012的最大的 10 个素数,如 2011,2003,1999,1997,1993..... 等。
#include
void main()
{
int i,j,flag,n;
n=0;
i=2012;
while(n<=10) /*$ERROR1$*/
{
flag=0;
for(j=2;j
if(i/j==0) /*$ERROR2$*/
{
flag=1;
break;
}
if(flag>=0) /*$ERROR3$*/
{
n=n+1;
printf("%d\n",i);
}
i=i-1;
}
}
v∙ ERROR S :
while(n<10) if(i%j==0) if(flag==0)
●∙ 下列程序功能是:
将在字符串t 中而未在字符串 s 中的字符顺序存放到一个新的字符串 u 中。
例如:
当 s= “12345 ”, t= “ 2468 ”时, u 中的字符为:
“ 68 ”。
#include
#include
void main()
{
char s[80]="12345",t[80]="2468",u[80];
int i,j,p,len1,len2;
p=____; /*$BLANK1$*/
len1=strlen(t);
len2=strlen(s);
for(i=0;i { for(j=0;j if(t[i]==s[j]) break; if (j>=len2) { u[p]=____; /*$BLANK2$*/ p++; } } u[p]=____; /*$BLANK3$*/ printf("%s\n",u); } v∙ BLANK S: 0;t[i];'\0' ●∙ 【三色球问题】若一个口袋放有12 个球,其中有 3 个红色的, 3 个白色的和 6 个黑色的,从中任取 8 个球,求共有多少中不同的颜色搭配。 如: 2 个白色球和 6 个黑色球; 1 个红色球、 3 个白色球和 4 个黑色球; 3 个红色球、 2 个白色球和 3 个黑色球;等都是满足条件的颜色搭配。 #include void PRINT(int n) { FILE *out; printf("count=%d\n",n); if((out=fopen("C: \\24000101\\RESULT.DAT","w+"))! =NULL) fprintf(out,"count=%dp",n); fclose(out); } void main() { int r,w,b; int n=0; for (r=0;r<=3;r++) for (w=0;w<=3;w++) for (b=0;b<=6;b++) if (r+w+b==8) n++; PRINT(n); } ●∙ 2、在屏幕上输出一下图形(说明: 数字之间无空格) 1 21 321 4321 54321 #include void main() { int i,j; i=1; while(i<6) { for(j=1;j<6-i;j++) printf(" "); j=1; /*$ERROR1$*/ while(j>=1) { printf("%d",i); /*$ERROR2$*/ --j; } printf("/n"); /*$ERROR3$*/ i++; } } v∙ ERROR S : j=i;printf("%d",j);printf("\n"); ●∙ 下列程序的功能是: 计算并输出下列级数的前N 项之和 SN ,直到 SN 大于 q 为止, q 的值通过形参传入 SN= 2/1+3/2+4/3+.....+N+1/N 例如,若q 的值为 6.0 ,则函数值为 6.083333 。 #include float sum(float q) { int N=1; float SN=0; while(SN__q) /*$BLANK1$*/ { SN=SN+1.0*(N+1)/N; ____; /*$BLANK2$*/ } return SN; } void main() { float q; printf("Input q: "); scanf("%f",&q); printf("%f\n",_____); /*$BLANK3$*/ } v∙ BLANK S: <=; N++ 或 ++N ; sum(q) ●∙ 计算100-2011 之间同时下列条件的数之和(要求使用循环实现)。 (1) 能被3 整除; (2) 至少有一位数字是5 。 输出格式: sum=2345678 #include void PRINT(long sum) { FILE *out; printf("sum=%ld\n",sum); if((out=fopen("C: \\24000101\\RESULT.DAT","w+"))! =NULL) fprintf(out,"sum=%lds",sum); fclose(out); } void main() { long sum=0;int i,n,flag; for(i=100;i<2011;i++) { flag=0; n=i; do{ if(n%10==5) { flag=1; break; } else n=n/10; }while(n>0); if(flag&&i%3==0) sum+=i; } PRINT(sum); } ●∙ 3、输出 5 名学生 4 门课的成绩,输出每门课的最高分。 #include #define N 4 #define M 5 void main() { int i, j; float score, firstscore,max; for(i=1;i<=M;i++) {printf("\n Please input score of course no %d: ",i); scanf("%f", firstscore ); /*$ERROR1$*/ max = &firstscore; /*$ERROR2$*/ for(j=2;j<=N; j++) { scanf("%f" , &score); if(score>max) score =max; /*$ERROR3$*/ } printf("\n The max score of course no %d is: %f", i, max); } } v∙ ERRORS: scanf("%f",&firstscore);max=firstscore;max=score; ●∙ 下列程序中函数fun 的功能是: 计算正整数 n 的所有因子( 1 和 n 除外)之和作为函数值返回。 例如: n=120 时,函数值为 239 。 #include int fun(int n) {int i,sum=0; for(i=2;i<_____;i++) /*$BLANK1$*/ if(_____) sum=sum+i; /*$BLANK2$*/ return sum; } void main() {int n; printf("Input n: "); scanf("%d",&n); printf("%d\n",_____); /*$BLANK3$*/ } v∙ BLANKS: n; n%i==0 ;fun(n) ●∙ 计算2011-9999 之间同时满足以下条件的所有自然数之和。 条件如下: (1 )该数能被 11 整除;(2 )该数加 1 后能被 4 整除。 输出格式: s=12345 #include void PRINT(long s) {FILE *out; printf("s=%ld\n",s); if((out=fopen("C: \\24000101\\RESULT.DAT","w+"))! =NULL) fprintf(out,"s=%ld",s); fclose(out); } void main() { long s=0; int i; for(i=2011;i<=9999;i++) if(i%11==0&&(i+1)%4==0) s=s+i; PRINT(s); } ●∙ 4、输出一维数组中的最大元素及其标值。 #include int getMaxFromArray(int a[],int n) {int i,max; max=1; /*$ERROR1$*/ for(i=1;i { if(a[i] > a[max]) max=i; } return i; /*$ERROR2$*/ } void main() {int a[10]={13,1,-5,4,9,0,-8,7,-6,2}; int max; max = getMaxFromArray(a); /*$ERROR3$*/ printf("max=%d,address=%d\n",a[max],max); } v∙ ERROR S: max=0;return max;max=getMaxFromArray(a,10); ●∙ 下列程序功能是: 读入一行英文文本,将其中每个单词的第一个字母改成大写,然后输出此文本行(这里的“单词”是指由空格隔开的字符串) 例如,若输入: I am a student to take the examination. 则应输出: I Am A Student To The Examination. #include void main() {char str[80],*ch; int flag=1; printf("Please input a string: \n"); gets(str); ch=____; /*$BLANK1$*/ while(*ch) {if (*ch==' ') flag=1; else {if (flag==____ && *ch>='a' && *ch<='z') /*$BLANK2$*/ *ch=*ch-32; flag=0; } ____ ; /*$BLANK3$*/ } printf("%s\n",str); } v∙ BLANK S: str;1;ch++; ●∙ 已知数列F ( n ): (1 )当 n=1 或 n=2 时 F ( n ) =1 (2 )当 n>2 时 F ( n ) =F ( n-2 ) +F ( n-1 ) %10000 求该数列前40 项的和(要求使用循环实现)。 输出格式: s=123456 #include void PRINT(long s) {FILE *out; printf("s=%ld\n",s); if((out=fopen("C: \\24000101\\RESULT.DAT","w+"))! =NULL) fprintf(out,"s=%ld",s); fclose(out); } void main() {long s=2; int f1,f2,f; int i; f1=f2=1; for(i=3;i<=40;i++) { f=(f1+f2)%10000; s+=f; f1=f2; f2=f; } PRINT(s); } ●∙ 5、计算: 1+ ( 1+2 ) + ( 1+2+3 ) + ( 1+2+3+4 ) +...+(1+2+3+...+n) 的值。 #include int sum(int n) {int i,s; s=1; /*$ERROR1$*/ for(i=1;i<=n;i++) s=s+i; return s; } void main() {int i,n; long s; printf("\nPlease input n: "); scanf("%d",n); /*$ERROR2$*/ s=0; for(i=1;i<=n;i++) s = sum(i); /*$ERROR3$*/ printf("\nThe result is %ld\n",s); } v∙ ERROR S: s=0;scanf("%d",&n);s=s+sum(i) ●∙ 下列程序中函数fun 的功能是: 找出 100 至 999 之间三位数字之和等于 9 的所有整数,把这些整数放在 s 所指数组中,个数通过 n 传回。 #include void fun(int s[],int *n) {int a,b,c,i; for(i=100;i<=999;i++) {a=______; /*$BLANK1$*/ b=i/10%10; c=i%10; if(a+b+c==9) { s[*n]=i; ______; /*$BLANK2$*/ } } } void main() {int s[50],n=0,i; fun(_____); /*$BLANK3$*/ for(i=0;i printf("%d ",s[i]); printf("\n"); } v∙ BLANK S: i/100;(*n)++;s,&n;●∙ 根据下列计算s 的值。 s=1/(2*2)+2/(3*3)+3/(4*4)+...+18/(19*19) 说明: (1 )结果保留四位整数;(2 )要求使用循环实现。 输出格式: s=12.4567 #include void PRINT(double s) {FILE *out; printf("s=%.4f\n",s); if((out=fopen("C: \\24000101\\RESULT.DAT","w+"))! =NULL) fprintf(out,"s=%.4f",s); fclose(out); } void main() {int i; double s=0; for (i=1;i<=18;i++) s=s+1.0*i/(i+1)/(i+1); PRINT(s); }●∙ 6、若有车的车牌号和罚分记录数据,统计罚分达到或超过 12 分的车辆总数并输出这些车辆的信息(说明: id域代表车牌号, score 域代表罚分)。 #include struct car { char id[12]; int score; }; void main() { int i,n=0; car c[6]={ {"A-QQ111",8}, /*$ERROR1$*/ {"A-TT222",12}, {"B-WJ333",12}, {"C-LC444",5}, {"F-PC555",6}, {"B-MY666",12}}; printf("id\tscore\n"); i=0; while(i<6) {f(score>=12) /*$ERROR2$*/ {printf("%s\t%d\n",c[i].id,c[i].score); n--; /*$ERROR3$*/ } i++; } printf("n=%d\n",n); } v∙ ERROR: structcar c[6]={{"A-QQ111",8};if(c[i].score>=12);n++ ●∙ 下列程序功能是: 统计字符串s 中各元音字母的个数。 说明: (1 )元音字母即: A 、 E 、 I 、 O 、 U ; (2 )字母不分大、小写。 例如,若输入字符串: “THIs is a book ”,则输出应该是: 1 0 2 2 0 #include #include void main() {char s[80]; int i=0,a[5]={0}; printf("Please input a string: \n"); gets(____); /*$BLANK1$*/ while(s[i]! =_____) /*$BLANK2$*/ { switch(toupper(s[i])) {case 'A': a[0]++;break; case 'E': a[1]++;break; case 'I': a[2]++;break; case 'O': a[3]++;break; case 'U': a[4]++;break;} ______; /*$BLANK3$*/ } for(i=0;i<5;i++) printf("%d ",a[i]); printf("\n"); } v∙ BLANKS: s;'\0';i++; ●∙ 根据下式求s 的值。 s=2011/1+2010/2+2009/3+...+3/2009+2/2010+1/2011 说明: (1 )结果保留一位小数;(2 )要求使用循环实现。 输出格式: s=2.1 #include void PRINT(double s) { FILE *out; printf("s=%.1f\n",s); if((out=fopen("C: \\24000101\\RESULT.DAT","w+"))! =NULL) fprintf(out,"s=%.5f",s); fclose(out); } void main() { double s=0; int i; for(i=1;i<=2011;i++) s=s+1.0*i/(2012-i); PRINT(s); } ●∙ 7、已知数列: 第一项和第二项都是 1 ,从第三项开始每项都是其前两项之和。 求第 n 项( n 从键盘输入,用递推实现)。 #include long f(int n) {if(n=1 || n=2) /*$ERROR1$*/ return 1; else return f(n-1) - f(n-2); /*$ERROR2$*/ } void main() {long m; int n; printf("\nPlease input n: \n"); scanf("%d",n); /*$ERROR3$*/ if(n>0) { m=f(n); printf("%ld\n",m); } else printf("Data error\n"); } v∙ ERRORS: if(n==1||n==2);reture f(n-1)+f(n-2);scanf("%d",&n); ●∙ 下列程序中函数fun 的功能是: 删除字符串中的数字字符。 例如输入字符串: 48CTYP9E6 ,则输出: CTYPE 。 #include #include void fun(char s[]) {int i,j; for(i=0,j=0;s[i]! =_____;i++) /*$BLANK1$*/ if(s[i]<'0' ||s[i]>'9') s[j++]=s[i]; s[j]=____; /*$BLANK2$*/ } void main() {char str[80]; printf("Input a string: \n"); gets(_____); /*$BLANK3$*/ fun(str); puts(str); } v∙ BLANKS: '\0';'\0';str; ●∙ 【3n+1 问题】一个正整数 n ,若其为奇数,则变换成 3n+1 ,若其为偶数,则变换成 n/2, 经过若干次变换,总能变成 1 。 例如,当正整数n 等于 3 时,有: 3->10->5->16->8->4->2->1 编程计算n 等于 1235 时,最少需要经过多少次变换才能变成 1 。 输出格式: count=2345 #include void PRINT(int count) {FILE *out; printf("count=%d\n",count); if((out=fopen("C: \\24000101\\RESULT.DAT","w+"))! =NULL) fprintf(out,"count=%ds",count); fclose(out); } void main() {int count=0; int n=1235; for(count=0;n! =1;count++) if(n%2==0) n=n/2; else n=3*n+1; PRINT(count); } ●∙ 8、将一个数组的数据逆序输出。 #include int sort(int a[],int lenth) {int i,t; if(lenth<0) return -1; for(i=0;i { t=a[0]; /*$ERROR1$*/ a[i]=a[lenth-i-1]; a[lenth-i-1]=t; } return 1; /*$ERROR2$*/ } void
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 题库
![提示](https://static.bdocx.com/images/bang_tan.gif)