二级C南开上机百题填空题.docx
- 文档编号:6815447
- 上传时间:2023-01-10
- 格式:DOCX
- 页数:98
- 大小:42.53KB
二级C南开上机百题填空题.docx
《二级C南开上机百题填空题.docx》由会员分享,可在线阅读,更多相关《二级C南开上机百题填空题.docx(98页珍藏版)》请在冰豆网上搜索。
二级C南开上机百题填空题
全国计算机等级考试二级C语言南开上机百题填空题解答
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
注意:
部分源程序如blank.c给出。
1题题目:
请补充fun函数,fun函数的功能是求10的阶乘。
#include
longfun(intn)
{
if()
return(n*fun());
elseif()
return1;
}
main()
{
intk=10;
printf("%d!
=%ld\n",k,fun(k));
}
2题题目:
请在函数fun的横线上填写若干表达式,使从键盘上输入一个整数n,输出斐波纳契数列。
斐波纳契数列是一种整数数列,其中每数等于前面两数之和,如011235813……
#include
intfun(intn);
main()
{
inti,n=0;
scanf("%d",&n);
for(i=0;i printf("%d",fun(i)); } intfun(intn) { if() return0; else if() return1; else return; } 3题题目: 请补充函数fun(char*s),该函数的功能是把字符串中的内容逆置。 例如: 字符串中原有的字符串为abcde,则调用该函数后,串中的内容为edcba。 #include #include #include #defineN81 voidfun(char*s) { inti=0,t,n=strlen(s); for(;;i++) { t=*(s+i); ; ; } } main() { chara[N]; clrscr(); printf("Enterastring: "); gets(a); printf("Theoriginalstringis: "); puts(a); fun(a); printf("\n"); printf("Thestringaftermodified: "); puts(a); } 4题题目: 请补充函数fun,它的功能是: 计算并输出n(包括n)以内能被3或7整除的所有自然数的倒数之和。 例如,在主函数中从键盘给n输入30后,输出为: s=1.226323。 #include doublefun(intn) { inti; doublesum=0.0; if(n>0&&n<=100) { for(i=1;;i++) if() sum+=; } returnsum; } main() { intn; doubles; printf("\nInputn: "); scanf("%d",&n); s=fun(n); printf("\n\ns=%f\n",s); } 5题题目: 给定程序的功能是求二分之一的圆面积,函数通过形参得到圆的半径,函数返回二分之一的圆面积(注: 圆面积公式为: S=3.14159*r*r,在程序中定义的变量名要与公式的变量相同)。 例如,输入圆的半径值: 19.527,输出为s=598.950017。 #include floatfun(float) { return3.14159*/2.0; } main() { floatx; printf("Enterx: "); scanf("%f",); printf("s=%f\n",fun(x)); } 6题题目: 给定程序的功能是计算并输出下列级数的前n项之和Sn,直到Sn大于q为止,q的值通过形参传入。 Sn=2/1+3/2+4/3+……+(n+1)/n 例如,若q的值为50.0,则函数值为50.416687。 #include doublefun(doubleq) { intn; doubles; n=2; s=2.0; while(sq) { s=s+(double)(n+1)/n; ; } printf("n=%d\n",n); ; } main() { printf("%f\n",fun(50)); } 7题题目: 函数fun的功能是: 统计长整数n的各个位上出现数字1、2、3的次数,并通过外部(全局)变量c1、c2、c3返回主函数。 例如,当n=123114350时,结果应该为: c1=3c2=1c3=2。 #include intc1,c2,c3; voidfun(longn) { c1=c2=c3=0; while(n) { switch() { case1: c1++;; case2: c2++;; case3: c3++; } n/=10; } } main() { longn=123114350L; fun(n); printf("\nTheresult: \n"); printf("n=%ldc1=%dc2=%dc3=%d\n",n,c1,c2,c3); } 8题题目: 请补充main函数,该函数的功能是: 从键盘输入一组整数,使用条件表达式找出最大的整数。 当输入的整数为0时结束。 例如,当输入123540时,最大的数为5。 #include #include #defineN100 main() { intnum[N]; inti=-1; intmax=0; clrscr(); printf("\nInputintegernumber: \n"); do { i++; printf("num[%d]=",i); scanf("%d",); max=num[i]: max; }while(); printf("max=%d\n",max); } 9题题目: 给定程序的功能是分别统计字符串中大写字母和小写字母的个数。 例如,给字符串ss输入: AaaaBBb123CCccccd,则输出结果应为: upper=5,lower=9。 #include voidfun(char*s,int*a,int*b) { while(*s) { if(*s>='A'&&*s<='Z') ; if(*s>='a'&&*s<='z') ; s++; } } main() { chars[100]; intupper=0,lower=0; printf("\nPleaseastring: "); gets(s); fun(s,&upper,&lower); printf("\nupper=%dlower=%d\n",); } 10题题目: 请补充main函数,该函数的功能是: 从键盘输入3个整数,然后找出最大的数并输出。 例如,输入: 12,45,43,输出为45。 #include #include main() { inta,b,c,max; clrscr(); printf("\nInputthreenumbers: \n"); scanf("%d,%d,%d",&a,&b,&c); printf("Thethreenumbersare: %d,%d,%d\n",a,b,c); if(a>b) ; else ; if(max ; printf("max=%d\n",max); } 11题题目: 给定程序中,函数fun的功能是: 把形参s所指字符串中下标为奇数的字符右移到下一个奇数位置,最右边被移出字符串的字符绕回放到第一个奇数位置,下标为偶数的字符不动(注: 字符串的长度大于等于2)。 例如,形参s所指的字符串为: abcdefgh,执行结果为: ahcbedgf。 #include voidfun(char*s) { inti,n,k; charc; n=0; for(i=0;s[i]! ='\0';i++)n++; if(n%2==0) k=n-; else k=n-2; c=; for(i=k-2;i>=1;i=i-2)s[i+2]=s[i]; s[1]=; } main() { chars[80]="abcdefgh"; printf("\nTheoriginalstringis: %s\n",s); fun(s); printf("\nTheresultis: %s\n",s); } 12题题目: 请补充fun函数,该函数的功能是将字符串tt中的大写字母都改为对应的小写字母,其他字符不变。 例如,若输入"AreyoucomefromSichuan? ",则输出"areyoucomefromsichuan? "。 #include #include #include char*fun(chartt[]) { inti; for(i=0;tt[i];i++) { if((tt[i]>='A')&&()) ; } return(); } main() { chartt[81]; printf("\nPleaseenterastring: "); gets(tt); printf("\nTheresultstringis: \n%s",fun(tt)); } 13题题目: 请补充fun函数,该函数的功能是判断一个数是否为素数。 该数是素数时,函数返回字符串: yes! ,否则函数返回字符串: no! ,并在主函数中输出。 #include"conio.h" #include"stdio.h" { inti,m; m=1; for(i=;i if(n%i==0) { m=0; break; } if(m==1&&n>1) return("yes! "); else return("no! "); } main() { intk=0; printf("Input: "); scanf("%d",&k); printf("%s\n",fun(k)); } 14题题目: 请补充fun函数,该函数的功能是: 依次取出字符串中所有小写字母,形成新的字符串,并取代原字符串。 #include #include voidfun(char*s) { inti=0; char*p=s; while() { if(*p>='a'&&*p<='z') { s[i]=*p; ; } p++; } s[i]=; } main() { charstr[80]; printf("\nEnterastring: "); gets(str); printf("\n\nThestringis: \%s\n",str); fun(str); printf("\n\nThestringofchangingis: \%s\n",str); } 15题题目: 题目: 给定程序的功能是判断字符ch是否与串str中的某个字符相同;若相同,什么也不作,若不同,则插在串的最后。 #include #include voidfun(char*str,charch) { while(*str&&*str! =ch)str++; if(*strch) { str[0]=ch; =0; } } main() { chars[81],c; printf("\nPleaseenterastring: \n"); gets(s); printf("\nPleaseenterthecharactertosearch: "); c=getchar(); fun(); printf("\nTheresultis%s\n",s); } 16题题目: 请补充fun函数,该函数的功能是: 判断一个年份是否为闰年。 例如,1900年不是闰年,2004是闰年。 #include #include intfun(intn) { intflag=0; if(n%4==0) { if() flag=1; } if() flag=1; return; } main() { intyear; printf("Inputtheyear: "); scanf("%d",&year); if(fun(year)) printf("%disaleapyear.\n",year); else printf("%disnotaleapyear.\n",year); } 17题题目: 给定程序的功能是将n个人员的考试成绩进行分段统计,考试成绩放在a数组中,各分段的人数存到b数组中: 成绩为60到69的人数存到b[0]中,成绩为70到79的人数存到b[1],成绩为80到89的人数存到b[2],成绩为90到99的人数存到b[3],成绩为100的人数存到b[4],成绩为60分以下的人数存到b[5]。 例如,当a数组中的数据是: 93、85、77、68、59、43、94、75、98。 调用该函数后,b数组中存放的数据应该是: 1、2、1、3、0、2。 #include voidfun(inta[],intb[],intn) { inti; for(i=0;i<6;i++) b[i]=0; for(i=0;i<;i++) if(a[i]<60) b[5]++; b[(a[i]-60)/10]++; } main() { inti,a[100]={93,85,77,68,59,43,94,75,98},b[6]; fun(,9); printf("theresultis: "); for(i=0;i<6;i++) printf("%d",b[i]); printf("\n"); } 18题题目: str为一个字符序列。 请补充fun函数,该函数的功能是: 查找str中值为x的元素,返回找到值为x的元素个数,并把这些值为x的元素下标依次保存在数组bb中。 例如,在"abcdefahij"中查找‘a’,结果为: 2个‘a’,下标依次为0、6。 #include #include #defineN20 intbb[N]; intfun(char*str,charch) { inti=0,n=0; chart=ch; char*p=str; while(*p) { if() ; p++; i++; } return; } main() { charstr[N]; charch; inti,j,n; clrscr(); printf("*******Inputtheoriginalstring*******\n"); gets(str); printf("*******TheOriginalstring*******\n"); puts(str); printf("*******Inputcharacter*******\n"); scanf("%c",&ch); n=fun(str,ch); printf("\nThenumberofcharacteris: %d\n",n); printf("*******Thesuffixofcharacter*******\n"); for(i=0;i printf("%d",bb[i]); } 19题题目: 给定程序的功能是计算score中m个人的平均成绩aver,将低于aver的成绩放在below中,通过函数名返回人数。 例如,当score={10,20,30,40,50,60,70,80,90},m=9时,函数返回的人数应该是4,below={10,20,30,40}。 #include intfun(intscore[],intm,intbelow[]) { inti,j=0; floataver=0.0; for(i=0;i aver+=score[i]; aver/=(float)m; for(i=0;i if(score[i] below[j++]=; returnj; } main() { inti,n,below[9]; intscore[9]={10,20,30,40,50,60,70,80,90}; n=fun(score,9,); printf("\nBelowtheaveragescoreare: "); for(i=0;i printf("%d",); printf("\n"); } 20题题目: 给定程序的功能是求出能整除x且不是偶数的各整数,并放在数组pp中,这些除数的个数由n返回。 例如,若x的值为30,则有4个数符合要求,它们是1,3,5,15。 #include voidfun(intx,intpp[],int*n) { inti,j=0; for(i=1;i<=x;i+=2) if((x%i)==0) pp[j++]=; *n=; } main() { intx,aa[1000],n,i; printf("\nPleaseenteranintegernumber: \n"); scanf("%d",&x); fun(x,); for(i=0;i printf("%d",aa[i]); printf("\n"); } 21题题目: 给定程序中,函数fun的功能是: 将s所指字符串中的所有数字字符移到所有非数字字符之后,并保持数字字符串和非数字字符串原有的先后次序。 例如,形参s所指的字符串为: def35adh3kjsdf7。 执行结果为: defadhkjsdf3537。 #include voidfun(char*s) { inti,j=0,k=0; chart1[80],t2[80]; for(i=0;s[i]! ='\0';i++) if(s[i]>='0'&&s[i]<='9') { t2[j]=s[i]; ; } else t1[k++]=s[i]; t2[j]=0; t1[k]=0; for(i=0;i ; for(i=0;i<;i++) s[k+i]=t2[i]; } main() { chars[80]="ba3a54j7sd567sdffs"; printf("\nTheoriginalstringis: %s\n",s); fun(s); printf("\nTheresultis: %s\n",s); } 22题题目: 给定程序中,函数fun的功能是: 在形参s所指字符串中的每个数字字符之后插入一个*号。 例如,形参s所指的字符串为: def35adh3kjsdf7。 执行结果为: def3*5*adh3*kjsdf7*。 #include voidfun(char*s) { inti,j,n; chart1[80],t2[80]; for(i=0;s[i]! ='\0';i++) if(s[i]>='0's[i]<='9') { n=0; while(s[i+1+n]! =) n++; for(j=i+n+1;j>i;j--) s[j+1]=; s[j+1]='*'; i=i+1; } } main() { chars[80]="ba3a54cd23a"; printf("\nTheoriginalstringis: %s\n",s); fun(s); printf("\nTheresultis: %s\n",s); } 23题
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 二级 南开 上机 填空