求《计算机程序设计基础:C语言程序设计》课后集题的所有答案

2024-12-22 15:34:52
推荐回答(1个)
回答1:

1./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{

int score;
printf("请输入你的分数:");
scanf("%d",&score);
if(score<0||score>100)
printf("输入有误!");
else if(score>=60&&score<70)
printf("及格");
else if(score>=70&&score<80)
printf("中等");
else if(score>=80&&score<90)
printf("良好");
else if(score>=90&&score<=100)
printf("优秀");
else
printf("不及格");

}
2./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a,b;char c;
printf("请输入运算式:");
scanf("%d%c%d",&a,&c,&b);
switch(c)
{
case '+':printf("%d%c%d=%d",a,c,b,a+b);break;
case '-':printf("%d%c%d=%d",a,c,b,a-b);break;
case '*':printf("%d%c%d=%d",a,c,b,a*b);break;
case '/':
if(b!=0)
printf("%d%c%d=%d",a,c,b,a/b);
else
printf("分母不能为零");
break;
case '%':
if(b!=0)
printf("%d%c%d=%d",a,c,b,a%b);
else
printf("分母不能为零");
break;
default:printf("运算符输入有误!");
}
}
3./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int i,j;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
printf("%d*%d=%d ",i,j,i*j);
}
printf("\n");
}
}
4./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a,i,k;
printf("请输入一个整数:");
scanf("%d",&a);
if(a==1)
printf("1既不是质数也不是合数");
else if(a>1)
{
for(i=2;i<=a/2;i++)
{
if(a%i==0)
k++;
}
if(k==a/2-1)
printf("你输入的数是质数!");
else
printf("你输入的是合数!");
}
else
printf("你输入的数有误!");
}
5./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a,i;
printf("请输入一个数:");
scanf("%d",&a);
printf("%d=",a);
for(i=2;i {
while(a!=1,i!=a)
{
if(a%i==0)
{
printf("%d*",i);
a=a/i;
}
else
{
break;
}
}
}
printf("%d",a);
}
6./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int n,i,j,k;
printf("请输入菱形大小:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i+1;j++)
{
printf(" ");
}
for(k=1;k<=2*i-1;k++)
{
printf("*");
}
printf("\n");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(" ");
}
for(k=1;k<=2*(n-i+1)-1;k++)
{
printf("*");
}
printf("\n");
}
}
7./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int i,j,k;
for(i=1;i<=26;i++)
{
for(j=1;j<=26-i+1;j++)
{
printf(" ");
}
for(k=1;k<=2*i-1;k++)
{
if(k==i&&i!=1)
printf("%c",65+i);
else
printf("%c",64+i);
}
printf("\n");
}
}
8./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a[15],i;
a[0]=1;
a[1]=1;
for(i=2;i<15;i++)
{
a[i]=a[i-1]+a[i-2];
}
for(i=0;i<15;i++)
{
printf("%d\t",a[i]);
}
}
9./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int hao;//接受候选人的代号
int a,b,c,d;//用于统计候选人所得票数
char name[20];//记住票数最多的人
int max;//记票数
printf("候选人如下:\n");
printf("1.张三\n2.李四\n3.王麻子\n4.赵六\n");
hao=-1;//随便给hao赋一个非零值确保能进入for语句循环
a=b=c=d=0;//为零初始值
for(;hao!=0;)
{ printf("请输入候选人代号:\n");
scanf("%d",&hao);
switch(hao)
{
case 1:a++;break;
case 2:b++;break;
case 3:c++;break;
case 4:d++;break;
case 0:break;
default:printf("请正确输入!");
}
}
printf("投票结果如下:\n");
printf("张三:%d票\n",a);
printf("李四:%d票\n",b);
printf("王麻子:%d票\n",c);
printf("赵六:%d票\n",d);
/*if(a=b=c=d)
{
printf("请再次投票!");
{
while
{*/
if(a>b)
{ max=a;strcpy(name,"张三");}
else
{ max=b;strcpy(name,"李四");}
if(c>max)
{ max=c;strcpy(name,"王麻子");}
if(d>max)
{ max=d;strcpy(name,"赵六");}
if(a=b=c=d)
{ strcpy(name,"请再次投票!");}
printf("%s",name);
printf("恭喜%s被选!!!",name);
}
10./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int i,j,k,n;
static int a[50][50];
printf("input line:");
scanf("%d",&n);
a[0][0]=1;
for(i=0;i<=n;i++)
{
for(j=0;j printf(" ");
for(j=0;j<=i;j++)
{
if(i==j&&j==0)
a[i][j]==1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
printf("%2d ",a[i][j]);
}
printf("\n");
}

}
11./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a[10];
int i,j,temp;
for(i=0;i<=9;i++)
{
printf("请输入%d个数:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<10-1;i++)
{
for(i=0;i<10-1;i++)
{
for(j=0;j<10-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}

}
12./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int i,j,temp,max,min;
int a[10];
for(i=0;i<10;i++)
{
printf("input %d number:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
printf("\n");
min=a[0];max=a[0];
for(i=0;i<10;i++)
{

max=max>a[i]?max:a[i];

min=min }
printf("max=%d\t,min=%d\n",max,min);
for(i=0;i<5;i++)
{
temp=a[i];
a[i]=a[9-i];
a[9-i]=temp;
}
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
}
13./* Note:Your choice is C IDE */
#include "stdio.h"
#include "time.h"
#include "stdlib.h"
void main()
{
int k,n,i;
srand(time(NULL));
printf("%d",rand()%+1);

printf("1.石头\n2.剪刀\n3.布\n");
do{
a:printf("你出的拳是:");
scanf("%d",&k);
if(k==1)
{ if(rand()%+1==1)
{printf("重划");goto a;}
else if(rand()%+1==2)
printf("恭喜您,赢!");
else if(rand()%+1==3);
printf("很遗憾,您败了!");
}
if(k==2)
{if(rand()%+1==1)
printf("很遗憾,您败了!");
else if(rand()%+1==2)
{printf("重划");goto a;}
else(rand()%+1==3);
printf("恭喜您,赢!");
}

if(k==3)
{if(rand()%+1==2)
printf("恭喜您,赢!");
else if(rand()%+1==1)
printf("很遗憾,您败了!");
else (rand()%+1==3);
{printf("重划");goto a;}
}
if(k>3)
printf("error");
}while(k<0||k>4);}
14./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int i,j,k,count1=0,count2=0,count3=0,count=0;
for(i=0;i<=9;i++)
{
for(j=0;j<=9;j++)
{
for(k=0;k<=9;k++)
{
count++;
//printf("%d%d%d ",i,j,k);
if(i==j||i==k||j==k)
count1++;

if(i==j&&i==k&&j==k)
count2++;

if(i!=j&&i!=k&&j!=k)
count3++;

}
}
}
printf("总数为:%d",count);
printf("两位数相同的有:%d ",count1);
printf("三位数相同的有:%d ",count2);
printf("三位数不同的有:%d ",count3);
}
15/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a[6]={55,180,285,195,300};
int i,sum=0;
int b,c;
printf("1.珠海\n2.广州\n3.南昌\n4.上海\n5.北京\n6.黑龙江\n");
printf("请选择出发站:");
scanf("%d",&b);
printf("请选择终点站:");
scanf("%d",&c);
for(i=b;i {
sum+=a[i-1];
}
printf("费用为:%d",sum);
}
17* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int yeas,month,day,sum=0,i;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
a:printf("请输入年份:");
scanf("%d",&yeas);
if(yeas<1900)
goto a;
b:printf("请输入月份:");
scanf("%d",&month);
if(month<0||month>12)
goto b;
c:printf("请输入日:");
scanf("%d",&day);
if(day<0||day>31)
goto c;

if(yeas%100==0&&yeas%4==0||yeas%400==0)
{
a[1]=29;
}
for(i=0;i {
sum+=a[month-2];
}
sum=sum+day;
printf("这一天是一年中的第%d天!",sum);
}
18./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int i,n,m,k,l=5;
int a[15]={2,9,15,23,34,49};
for(i=0;i<=l;i++)
{
printf("%d ",a[i]);
}
printf("\n");
do
{
printf("请任意的输入一个数:");
scanf("%d",&n);
for(i=0;i<=l;i++)
{
if(n { k=i;break;}
else
{k=l+1;}
}
for(i=l+1;i>=k+1;i--)
{
a[i]=a[i-1];
}
a[k]=n;
l++;
printf("插入后的数组:\n");
for(i=0;i<=l;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}while(l<15);
printf("数据已满!");
}
19.* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a=8,b=5,c=3,d=2;
int n=1999;
int max;
int i;
//max=a>b?a:b>c?b:c>d?c:d;
for(i=1;i<=n;i++)
{
max=a>b?a:b>c?b:c>d?c:d;
if(max==a)
{
a-=3;
b+=1;
c+=1;
d+=1;
}
else if(max==b)
{
b-=3;
a+=1;
c+=1;
d+=1;
}
else if(max==c)
{
c-=3;
a+=1;
b+=1;
d+=1;
}
else
{
a+=1;
b+=1;
c+=1;
d-=3;
}
}
printf("%d\n",max);
printf("%d,%d,%d,%d",a,b,c,d);

}
20./* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
char a[40];
int k,i,count1=0,count2=0,count3=0,count4=0,count=0;
printf("请输入一串字符:\n");
gets(a);
k=strlen(a);
for(i=0;i<=k;i++)
{
if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
count1++;
if(a[i]<='9'&&a[i]>'0')
count2++;
if(a[i]=='_')
count3++;
if(a[i]==' ')
count4++;

}
printf("字母个数:%d\t数字个数: %d \t下划线个数:%d\t空格个数: %d\t其他: %d",count1,count2,count3,count4,count);
}