c语言上机题.docx
《c语言上机题.docx》由会员分享,可在线阅读,更多相关《c语言上机题.docx(40页珍藏版)》请在冰豆网上搜索。
c语言上机题
试卷1改错题
输入大于等于2015的10个素数,如2017,2027,2029,2039,……等。
#include
voidmain
()
{
inti,j,flag,n;
n=0;
i=2015;
while(n<=10)/*$ERROR1$*/改为while(n<10)
{
flag=0;
for(j=2;j
if(i/j==0)/*$ERROR2$*/改为if(i%j==0)
{
flag=1;
break;
}
if(flag>=0)/*$ERROR3$*/改为if(flag==0)
{
n=n+1;
printf("%d\n",i);
}
i=i+2;
}
}
试卷1填空题
输入100以内能被3整除且个位数为6的所有整数之和。
#include
voidmain()
{
inti,j;
ints=0;/*$BLANK1$*/
for(i=0;i<10或i<=9;i++)/*$BLANK2$*/
{
j=i*10+6;
if(j%3==0)/*$BLANK3$*/
s=s+j;
}
printf("%d\n",s);
}
试卷1程序题
【兑换零钱】
计算将200元钱换成1元,5元和10元的零钱(每种零钱都要求有),一共有多少种换法。
参考程序1
#include
voidPRINT(intcount)
{
FILE*out;
printf("count=%d\n",count);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"count=%d",count);
fclose(out);
}
voidmain()
{intcount=0;
Inta,b,c;
for(a=1;a<200;a++)
for(b=1;b<40;b++)
for(c=1;c<20;c++)
if(a+5*b+10*c==200)
count++;
PRINT(count);
}
参考程序2
#include
voidPRINT(intcount)
{
FILE*out;
printf("count=%d\n",count);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"count=%d",count);
fclose(out);
}
voidmain()
{intcount=0;
Inta,b,c;
for(a=1;a<200;a++)
for(b=5;b<200;b+=5)
for(c=10;c<200;c+=10)
if(a+b+c==200)
count++;
PRINT(count);
}
参考程序3
#include
voidPRINT(intcount)
{
FILE*out;
printf("count=%d\n",count);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"count=%d",count);
fclose(out);
}
voidmain()
{intcount=0;
Intt10,t5;
for(t10=10;t10<200;t10+=10)
for(t5=5;t5<200-t10;t5+=5)
if(200-t10-t5>0)count++;
PRINT(count);
}
试卷2改错题
在屏幕上输入以下图形(每行前没有空格,星号之间也没有空格)
*****
****
***
**
*
**
***
****
*****
#include
voidmain()
{
inti,j;
i=1;/*$ERROR1$*/改为i=0;
while(i<5)
{
j=0;
while(j<5-i)
{printf("*");
j++;
}
printf("\n");
i++;
}
i=4;
while(i>0)
{
j=0;
while(j<5-i)/*$ERROR2$*/改为while(j<=5-i)
{printf("*");
j++;
}
printf("\n");
i++;/*$ERROR3$*/改为i--;
}
}
试卷2填空题
某部门有三位职工,要求输入职工的工资信息,计算每位职工的实发工资和部门实发工资总额(total)
说明:
(1)描述职工工资信息的数据包括编号(num)、姓名(name)、基本工资(bwage)\奖金(bonus)、保险(ins)和实发工资(rwage);
(2)实发工资=基本工资+奖金—保险;
(3)职工编号长度小于10位且为整数,姓名不包括空格。
#include
#defineN3
structemployee
{
longnum;
charname[15];
longbwage,bonus,ins,rwage;
};
voidmain()
{
sructemployeeNK1$*/
inti;
longtotal;
total=0;/*$BLANK2$*/
printf("Pleaseinputdata:
\n");
printf("\nnumnamebwagebonusins:
\n");
for(i=0;i{
scanf("%ld%s",&worker[i].num,worker[i].name);
scanf("%ld%ld%ld",&worker[i].bwage,&worker[i].bonus,&worker[i].ins);
}
for(i=0;i{
Worker[i].rwage=worker[i].bonus-worker[i].ins;/$BLANK3$*/
total=total+worker[i].rwage;
}
printf("output:
\n");
printf("\nnumnamerealwage\n");
for(i=0;i{
printf("%-9ld%-14s%-ld\n",worker[i].num,worker[i].name,worker[i].rwage);
}
printf("\ntotalwage=%ld\n",total);
}
试卷2程序题
某果农有一车水果,第一天卖掉三分之而后吃了两个,第二天卖掉了剩下的三分之二又吃了两个,第三天到第七天都如此,到第八天一看只剩了五个苹果。
求此车共装有多少个苹果(要求用循环实现)。
参考程序1
#include
voidPRINT(ints)
{
FILE*out;
printf("s=%d\n",s);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"s=%d",s);
fclose(out);
}
voidmain()
{
inti;s=5;
for(i=1;i<8;I++)
s=(s+2)*3;
PRINT(s);
}
试卷3改错题
计算所有三位正整数中各个数位上数字之和是13的数的总和。
如将139、148、157等这样的数求和。
#include
voidmain()
{
inti,j,s;
longsum;
sum=1;/*$ERROR1$*/改为sum=0;
for(i=100;i<=999;i++)
{
j=i;
s=0;
while(j>1)/*$ERROR2$*/改为while(j<0)
{
s=s+j/10;/*$ERROR3$*/改为s=s+j%10;
j=j/10;
}
if(s==13)sum+=i;
}
printf("%ld\n",sum);
}
试卷3填空题
从键盘上输入两个正整数,并输入他们的最大公约数。
#include
intgcd(intm,intn)
{
intr,t;
if(m{
t=m;
m=n;
n=t;/*$BLANK1$*/
}
while(n>0)
{
r=m%n;
m=n;
n=r;
}
return(_m);/*$BLANK2$*/
}
voidmain()
{
intm,n,result;
printf("Peaseinputtwonumber:
\n");
scanf("%d%d",&m,&n);/*$BLANK3$*/
result=gcd(m,n);
printf("result=%d\n",result);
}
试卷3程序题
求100~999之间的回文素数之和。
说明:
(1)回文素数是一个素数,且从左向右和从右向左读是相同的,如:
101,131,181
(2)要求使用循环实现。
参考程序1
#include
voidPRINT(longs)
{
FILE*out;
printf("s=%ld\n",s);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"s=%ld",s);
fclose(out);
}
voidmain()
{
longs=0;
inta,b,I,t;
for(a=1;a<=9;a++)
for(b=0;b<=9;b++)
{
t=101*a+10*b;
for(i=2;i<=t/2;i++)
if(t%i==0)break;
if(i>t/2)s+=t;
}
PRINT(s);
}
参考程序2
#include
voidPRINT(longs)
{
FILE*out;
printf("s=%ld\n",s);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"s=%ld",s);
fclose(out);
}
voidmain()
{
longs=0;
inta,b,I,t;
for(i=100;i<1000;i++)
{
a=i\100;
b=i\10%10;
c=i%10;
if(a==c)
{for(t=2;t<=i/2;t++)
if(t>i/2)s+=I;
}
}
PRINT(s);
}
试卷4改错题
把字符串s中所有数字字符按以下规律改写:
(1)0,1,2,3,4,5,6,7,8分别改写成1,2,3,4,5,6,7,8,9
(2)9改成0
(3)其他字符保持不变
#include
#include
voidmain()
{
chars[80];
inti;
getc(s);/*$ERROR1$*/改为gets(s);
for(i=0;s[i]!
='\0';i++)
{
if(s[i]='9')/*$ERROR2$*/改为if(s[i]==’9’)
s[i]='0';
elseif(s[i]>='0'&&s[i]<='8')
s[i]=s[i]+1;
}
printf("%c\n",s);/*$ERROR3$*/改为printf(“%s\n”,s);
}
试卷4填空题
定义求n!
的递归函数f(),并调用函数f()求1!
+2!
+3!
+4!
+5!
的值。
#include
longf(intn)
{
if(n==1||n==0)
return
(1);
else
return(n*f(_n-1);/*$BLANK1$*/
}
voidmain()
{
inti;
longs;
s=_0_;/*$BLANK2$*/
for(i=1;i<=5;i++)
s+=f(i);
printf("1!
+2!
+3!
+4!
+5!
=%ld\n",_s_);/*$BLANK3$*/
}
试卷4程序题
计算10000以内9的偶数倍数之和,即:
sum=18+36+54+……9990其中18,36,54^9990等分别是9的2,4,6……1110倍。
参考程序1
#include
voidPRINT(longsum)
{
FILE*out;
printf("sum=%ld\n",sum);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"sum=%ld",sum);
fclose(out);
}
voidmain()
{
inti;
intsum=0;
for(i=2;i<=1110;i+=2)
sum*=9;
PRINT(sum);
}
参考程序2
#include
voidPRINT(longsum)
{
FILE*out;
printf("sum=%ld\n",sum);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"sum=%ld",sum);
fclose(out);
}
voidmain()
{
intI;
intsum=0;
for(i=18;i<10000;i+=18)
sum+=i;
PRINT(sum);
}
参考程序3
#include
voidPRINT(longsum)
{
FILE*out;
printf("sum=%ld\n",sum);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"sum=%ld",sum);
fclose(out);
}
voidmain()
{
intI;
intsum=0;
for(i=1;i<10000;i++)
if(i%2==0&&i%9==0)sum+=I;
PRINT(sum);
}
试卷5改错题
若有车的车牌号和罚分记录数据,统计罚分达到或超过12分的车辆总数并输入这些车辆的信息(说明:
id域代表车牌号,score域代表罚分)。
#include
structcar
{charid[12];
intscore;
};
voidmain()
{
inti,n=0;
carc[6]={{"A-QQ111",8},/*$ERROR1$*/改为structcarc[6]={{“A-QQ111”,8},
{"A-TT222",12},
{"B-WJ333",12},
{"C-LC444",5},
{"F-PC555",6},
{"B-MY666",12}};
printf("id\tscore\n");
i=0;
while(i<6)
{
if(score>=12)/*$ERROR2$*/改为if(c[i].score>=12)
{
printf("%s\t%d\n",c[i].id,c[i].score);
n--;/*$ERROR3$*/改为n++;
}
i++;
}
printf("n=%d\n",n);
}
*试卷5填空题
输入所有的“水仙花数”,并输入“水仙花数”的个数。
所谓的“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身。
例如153=1*1*1+5*5*5+3*3*3。
#include
voidmain()
{
inti,a,b,c,n;
n=___0;/*$BLANK1$*/
for(i=101;i<=999;i++)
{
a=i%10;
b=i/10%10;
c=___;/*$BLANK2$*/改为C=i/100
if(a*a*a+b*b*b+c*c*c==i)
{
printf("%6d",i);
___n++;/*$BLANK3$*/
}
}
printf("\nn=%d\n",n);
}
试卷5程序题
【三色球问题】
若一个口袋中放有12个球,其中有3个红色的,3个白色的和6个黑色的,从中任取8个球,求共有多少种不同的颜色搭配。
如:
2个白色球和6个黑色球;1个红色球、3个白色球和4个黑色球;等都是满足条件的颜色搭配。
参考程序1
#include
voidPRINT(intn)
{
FILE*out;
printf("count=%d\n",n);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"count=%dp",n);
fclose(out);
}
voidmain()
{
intn=0;
inta,b,c;
for(a=0;a<=3;a++)
for(b=0;b<=3;b++)
for(c=0;c<=6;c++)
if(a+b+c==8)n++;
PRINT(n);
}
参考程序2
#include
voidPRINT(intn)
{
FILE*out;
printf("count=%d\n",n);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"count=%dp",n);
fclose(out);
}
voidmain()
{
intn=0;
inta,b,c;
for(a=0;a<=3;a++)
for(b=0;b<=3;b++)
{c=8-a-b;
If(c<=6)n++;
}
PRINT(n);
}
试卷6改错题
在屏幕上输入以下图形(数字之间没有空格)。
0
123
45678
9012345
678901234
#include
voidmain()
{
chara[9]="";
inti,j,k,n=0;
for(i=0;i<5;i++)
{
for(j=0;j
{
k=4-i+j;
n=n/10;/*$ERROR1$*/改为:
n=n%10;
a[k]='0'+n;
n++;
}
for(j=0;j<9;j++)
printf("%c",a+j);/*$ERROR2$*/改为:
printf(“%c”,a[j]);
printf("/n");/*$ERROR3$*/改为:
printf(“\n”);
}
}
试卷6填空题
输入100~300之间个位数码为3的全部素数之和。
#include
voidmain()
{
inti,j,s;
s=0;/*$BLANK1$*/
for(i=101;i<300;i+=2)
{
for(j=2;j<=i/2;j++)
if(i%j==0)
break;/*$BLANK2$*/
if((j>i/2)&&(i%10==3))
s+=i;/*$BLANK3$*/
}
printf("s=%d\n",s);
}
试卷6程序题
已知数列F(n)
当n=1时F(n)=5
当n=(F(n-1)+10/F(n-1)/2
求该数列第五项即F(5)的值。
说明:
(1)结果保留五位小数;
(2)要求使用循环实现。
#include
voidPRINT(doublef)
{
FILE*out;
printf("F(5)=%.5f\n",f);
if((out=fopen("C:
\\24000110\\RESULT.DAT","w+"))!
=NULL)
fprintf(out,"F(5)=%.5f",f);
fclose(out);
}
voidmain()
{
doublef=5;
inti;
for(i=2;i<=5;i++)
f=(f+10/f)/2;
PRINT(f);
}
试卷7改错题
将字符串str1和字符串str2合并成一个新字符串str,然后输出字符串str。
#include
#include
voidmain()
{
charstr1[100],str2[30];
inti,j;
printf("Enterthefirststring:
");
gets(str1);
printf("Enterthesecondstring:
");
gets(str2);
for(i=0;str1[i]!
='\0';i++);
j=0;
while(str2[j]!
='\0')
{
str1[i++]=str2[j];
i++;/*$ERROR1$*/改为:
j++;
}
str1[i]="\0";/*$ERROR2$*/改为:
str[j]=’\0’;
printf("%c\n",str1);/*$ERROR3$*/改为:
printf(“%s\n”,str);
}
试卷7填空题
输入10个数据,计算并输入其中所有正数的平均值。
#include
#defineN10
voidmain()
{
doubleave=0,a[N],*p;
int