国家二级C语言机试考试题.docx
- 文档编号:24454109
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:38
- 大小:21.71KB
国家二级C语言机试考试题.docx
《国家二级C语言机试考试题.docx》由会员分享,可在线阅读,更多相关《国家二级C语言机试考试题.docx(38页珍藏版)》请在冰豆网上搜索。
国家二级C语言机试考试题
试题1
改错
计算大于100的前10个素数之和。
程序如下:
#include
voidmain<>
{
inti,j,n,s;
n=0;
s=1;/*$ERROR1$*/
for
{
for
if
{
continue;/*$ERROR2$*/
}
if
{
s+=i;
n++;
if =10>/*$ERROR3$*/ break; } } printf<"s=%d\n",s>; } 参考答案: 1.s=0; 2.break; 3.if 程序填空 输入一个大于1的正整数,将其分解成质因数的乘积〔质因数即素数因子,并按下例中的格式输出。 请填空。 例如: #include voidmain<> { longi,n; printf<"Inputn: ">; scanf<"%ld",___>;/*$BLANK1$*/ printf<"Output: %d=",n>; i=2; while { if { printf<"%ld*",i>; n=___;/*$BLANK2$*/ } else i++; } printf<"%ld\n",___>;/*$BLANK3$*/ } 参考答案: 1.&n 2.n/i 3.i 程序设计 [白马百担问题] 有100匹马驮100担货,大马驮4担,中马驮3担,5匹下马驮1担。 问三种马各有几匹? 注意: 1.大、中、小马都必须有; 2.问题的解只有一种。 输出格式: Big=30,mid=40,small=30 参考程序一: voidmain<> { inta,b,c; for for PRINT; } 参考程序二: voidmain<> { inta,b,c; for if PRINT; } 试题2 改错 从键盘输入一个不多于4位的十进制正整数,求出它是几位数。 如输入528,则结果为n=3. 程序如下: #include voidmain<> { intx,n; scanf<"%d",x>;/*$ERROR1$*/ if printf<"DataError! \n">; else { if elseif elseif n=4;/*$ERROR3$*/ printf<"n=%d\n",n>; } } 参考答案: 1.scanf<"%d",&x>; 2.if 3.elsen=4; 程序填空 输出一下图形<字符"*"之间没有空格,第一行之前有一个空格>。 请填空。 * *** ***** ******* ********* 程序如下: #include voidmain<> { inti,j; for/*$BLANK1$*/ { for printf<"">; for printf<"*">; printf<"\n">; } } 参考答案: 1.5 2.i 3.2*i-1 程序设计 计算并输出Fibonacci数列中小于3000的最大项。 说明: Fibonacci数列的前两项均为1,从第三项开始每一项都是其前两项上网和。 数列如下: 1,1,2,3,5,8,13,21... 输出格式: f=2345678 参考程序一 voidmain<> { longf1,f2,f,i; f1=f2=1; for {f=f1+f2; if f1=f2; f2=f; } f=f2; PRINT } 参考程序二 voidmain<> { longf1,f2,f,i; f1=f2=1;f=2; for {f=f1+f2; f1=f2; f2=f; } f=f1; } PRINT } 参考程序三 voidmain<> { longt[40]={1,1},f; inti; for t[i]=t[i-1]-t[i-2]; for if f=t[i-1]; PRINT } 参考程序四 voidmain<> { longf1,f2,f; f1=f2=1; for { f1=f2; f2=f; } f=f2; PRINT } 试题3 改错 在屏幕上输出以下图形〔说明: 第一行之前有3个空格,星号之间无空格。 * *** ***** ******* ***** *** * 程序如下: #include #include voidmain<> { inti,j; for { j=1;/*$ERROR1$*/ while { printf<"">; j++; } j=0; while { printf<"">; i++;/*$ERROR3$*/ printf<"\n">; } } 参考答案: 1.j=0; 2.while 3.j++; 程序填空 以下程序的功能是: 输出所有的"水仙花数",并输出"水仙花数"的个数。 所谓"水仙花数"是指一个三位数,其各位数字的立方和等于该数本身。 例如153是一个"水仙花数",因为153=1*1*1+5*5*5+3*3*3。 请填空。 程序如下: #include voidmain<> { inti,a,b,c,n; n=____;/*$BLANK1*/ for { a=i%10; b=i/10%10; c=____;/*$BLANK2$*/ { printf<"%6d",i>; ___;/*$BLANK3$*/ } } printf<"\nn=%d\n",n>; } 参考答案 1.0 2.i/100 3.n++或++n 程序设计 求1900~2010之间各位数不是7的所有素数之和。 输出格式: s=23456 参考程序一: voidmain<> { longs=0; inti,j; for { for ifbreak; if =7>s+=I; } PRINT } 参考程序二: voidmain<> { longs=0; inti,j; for { if =7> { for ifbreak; if } } PRINT } 试题4 改错 计算100~300之间满足下列条件的数之和。 (1)能被3整除; (2)个位数是7. 程序如下: #include voidmain<> { IntI,sum; Sum=1;/*$ERROR1$*/ For If If { Sum=I;/*$ERROR2$*/ } Print<"%d",sum>;/*$ERROR3$*/ } 参考答案: 1.sum=0; 2.sum=sum+i;或sum+=i; 3.printf<"%d",sum>; 程序填空 求满足条件1+2+3+…+n>=1000的最小n的值。 请填空。 #include Voidmain<> { IntI,n,s: S=_____;/*$BLANK1$*/ I=1; While { s=_____;/*$BLANK2$*/ i++; } n=_____;/*$BLANK3$*/ printf<"n=%d\n",n>; } 参考答案: 1.0 2.s+i 3.i-1 程序设计 编写程序,计算各位数字之和等于15的三位数之和。 输出格式: S=1234 参考程序一: Voidmain<> { Longs=0; Inta,b,c; For For Print } 参考程序二: Voidmain<> { longs=0; inti; for ifs+=i; Print } 试题5 改错 输出一维数组中最大元素及其下标值。 #include voidmain<> { inta[10]={-3,1,-5,4,9,0,-8,7,-6,2}; inti,max,addr; max=a[0]; addr=1;/*$ERROR1$*/ i=1; while/*$ERROR2$*/ { if { max=a[i]; i=addr;/*$ERROR3$*/ } i++; } printf<"max=%d,address=%d\n",max,addr>; } 参考答案: 1.addr=0; 2.while 3.addr=i; 程序填空 从键盘输入一个学生成绩,输出对应的等级〔100分为A,90~99为B,80~89为C,70~79为D,60~69为E,小于60为F,如果输入成绩小于0分或大于100分,输出提示信息"InputError! "。 #include voidmain<> { intg; printf<"Enteramark: ">; scanf<"%d",___>;/*$BLANK1$*/ if printf<"InputError! \n">; else { printf<"g=%d: ",g>; switch<___>/*$BLANK2$*/ { case10: printf<"A\n">;break; case9: printf<"B\n">;break; case8: printf<"C\n">;break; case7: printf<"D\n">;break; case6: printf<"E\n">;break; ___: printf<"F\n">;/*$BLANK3$*/ } } } 参考答案: 1.&g 2.g/10 3.default 程序设计 计算900到2010之间所有能被11整除的奇数之和。 〔要求用循环实现 输出格式: sum=2345678 参考程序一: voidmain<> { longsum=0; inti; for ifsum+=I; PRINT } 参考程序二: voidmain<> { longsum=0; inti; for ifsum+=i; PRINT } 参考程序三: voidmain<> { longsum=0; inti; for ifsum+=i; PRINT } 参考程序四: voidmain<> { longsum=0; inti; for sum+=i; PRINT } 试题6 改错 计算: 1-2/<1+2>+3/<1+2+3>-4/<1+2+3+4>+…+n/<1+2+3+…+n>的值。 〔假设本体中你的值从键盘输入,且大小在10以内。 #include voidmain<> { inti,j,n,a; doubles=0; printf<"pleaseentern=">; scanf<"%d",n>;/*$ERROR1$*/ for { a=1;/*$ERROR2$*/ for a=a+j; if/*$ERROR3$*/ s=s+1.0*i/a; else s=s-1.0*i/a; } printf<"%f\n",s>; }参考答案: 1.scanf<"%d",&n>; 2.a=0; 3.if =0> 程序填空 #include #defineN10 structstudent { charnum[20]; intscore; }; voidmain<> { structstudentst[N]= {{"0906050401",99},{"0906050402",76}, {"0906050501",80},{"0906050502",68}, {"0906050503",76},{"0906050504",64}, {"0906050506",98},{"0906050508",79}, {"0906050509",64},{"0906050510",98}}; inti; int___;/*$BLANK1$*/ min=st[0].score; for if min=st[i].score; for/*$BLANK3$*/ if printf<"%s\t\t%d\n",st[i].num,st[i].score>; } 参考答案: 1.min 2.st[i].score 3.N 程序设计 根据下式求s的值〔要求用循环实现: s=1/2-2/3+3/4-4/5+5/6-/6/7+…+99/100 输出格式: s=0.1234 参考程序一: voidmain<> { doubles; inti;flag=1; s=0; for { s=s+1.0*i/*flag; flag=-flag; } PRINT } 参考程序二: voidmain<> { doubles; inti; s=0; for if s=s+1.0*i/; else s=s-1.0*i/; PRINT } 试题7 改错 将数组中的数从小到大排序后输出,要求: 所有数据输出在一行,且每个数据占5列。 #include voidmain<> { inta[11]={7,9,-6,10,50,32,24,-15,1,88,29}; inti,j,k; for for { { k=a[i+1];/*$ERROR2$*/ a[i]=a[i+1]; a[i+1]=k; } } for printf<"%5d",&a[i]>/*$ERROR3$*/ } 参考答案: 1.ifa[i+1]> 2.k=a[i]; 3.printf<"%5d",a[i]>; 程序填空 9个评委给某个选手打分〔分数存于数组a中,去掉一个最高分和一个最低分,求该选手的平均分。 #include voidmain<> { inti; floata[9],max,min,ave; printf<"Inputarray: \n">; for scanf<"%f",___>;/*$BLANK1$*/ max=min=ave=___;/*$BLANK2$*/ for { ave+=a[i]; if max=a[i]; elseif ___;/*$BLANK3$*/ } ave= printf<"Mark=%.3f\n",ave>; } 参考答案: 1.&a[i] 2.a[0] 3.min=a[i] 程序设计 根据下式求s的值〔要求用循环实现: s=1+1/<1+2>+1/<1+2+3>+1/<1+2+3+4>+…1/<1+2+3+…+n> 说明: 本题以n=30为例。 参考程序一: voidmain<> {doubles; inti,j,h; for { h=0; for h=h+j; s=s+1.0/h; } PRINT } 参考程序二: voidmain<> { doubles; inti; for s+=2.0/i/; PRINT } 试题8 改错 计算1+<1+2>+<1+2+3>+<1+2+3+4>+...+<1+2+3+…+n>的值。 #include voidmain<> { longs,t; inti,j,n; scanf<"%d",n>;/*$ERROR1$*/ s=1;/*$ERROR2$*/ for { t=0; j=1; while { t+=j; j++; } s=s+t; } printf<"s=%ld\n",s>; } 参考答案: 1.scanf<"%d",&n>; 2.s=0; 3.while 程序填空 从键盘输出10个数存于数组a中,求最大数和此大数〔假设10个数各不相同。 #include voidmain<> { inti,max1,max2,a[10]; printf<"Inputarray: \n">; for scanf<"%d",___>;/*$BLANK1$*/ ifa[1]> { max1=a[0];max2=a[1]; } else { max1=a[1];max2=a[0]; } for if { max2=___;/*$BLANK2$*/ max1=a[i]; } else if max2=___;/*$BLANK3$*/ printf<"max1=%d,max2=%d\n",max1,max2>; } 参考答案: 1.&a[i] 2.max1 3.a[i] 程序设计 某果农有一车苹果,第一天卖掉2/3,并吃了2个,第二天卖掉了剩下2/3,并吃了2个,第三天到第七天都是如此,到第八天只剩下五个苹果。 求此车共装有多少苹果〔要求用循环实现。 输出格式: s=23456 参考程序: voidmain<> { inti,s=5; for s= PRINT } 试题9 改错 将字符串str1和str2合并成一个新字符串str,然后输出字符串str。 #include #include voidmain<> { charstr1[30],str2[30],str[80]; inti=0,j=0; printf<"Enterthefirststring: ">; gets printf<"Enterthesecondstring: ">; gets while { str[i]=str1[i]; i++; } while { str[i++]=str2[j]; i++;/*$ERROR1$*/ } str[i]="\0";/*$ERROR2$*/ printf<"str: %c\n",str>;/*$ERROR3$*/ } 参考答案: 1.j++; 2.str[i]=’\0’; 3.printf<"str: %s\n",str>; 程序填空 调用swap〔函数交换两个变量的值。 #include voidswap;;;;;;;;*3;;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 国家 二级 语言 考试题
![提示](https://static.bdocx.com/images/bang_tan.gif)