1-5 #include <stdio.h>
int main ( )
{ printf ("**************************\n\n");
printf(" Very Good!\n\n");
printf ("**************************\n");
return 0;
}
1-6#include <stdio.h>
int main()
{int a,b,c,max;
printf("please input a,b,c:\n");
scanf("%d,%d,%d",&a,&b,&c);
max=a;
if (max<b)
max=b;
if (max<c)
max=c;
printf("The largest number is %d\n",max);
return 0;
}
4-4-1
#include <stdio.h>
int main()
{
int a,b,c;
printf("请输入三个整数:");
scanf("%d,%d,%d",&a,&b,&c);
if (a<b)
if (b<c)
printf("max=%d\n",c);
else
printf("max=%d\n",b);
else if (a<c)
printf("max=%d\n",c);
else
printf("max=%d\n",a);
return 0;
}
4-6.
#include <stdio.h>
int main()
{ int x,y;
printf("输入x:");
scanf("%d",&x);
if(x<1) /* x<1 */
{ y=x;
printf("x=%3d, y=x=%d\n" ,x,y);
}
else if(x<10) /* 1=<x<10 */
{ y=2*x-1;
printf("x=%d, y=2*x-1=%d\n",x,y);
}
else /* x>=10 */
{ y=3*x-11;
printf("x=%d, y=3*x-11=%d\n",x,y);
}
return 0;
}
4-7-1
#include <stdio.h>
int main()
{
int x,y;
printf("enter x:");
scanf("%d",&x);
y=-1;
if(x!=0)
if(x>0)
y=1;
else
y=0;
printf("x=%d,y=%d\n",x,y);
return 0;
}
4-7-2
#include <stdio.h>
int main()
{
int x,y;
printf("please enter x:");
scanf("%d",&x);
y=0;
if(x>=0)
if(x>0) y=1;
else y=-1;
printf("x=%d,y=%d\n",x,y);
return 0;
}
4-8
#include <stdio.h>
int main()
{ float score;
char grade;
printf("请输入学生成绩:");
scanf("%f",&score);
while (score>100||score<0)
{printf("\n 输入有误,请重输");
scanf("%f",&score);
}
switch((int)(score/10))
{case 10:
case 9: grade='A';break;
case 8: grade='B';break;
case 7: grade='C';break;
case 6: grade='D';break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: grade='E';
}
printf("成绩是 %5.1f,相应的等级是%c\n ",score,grade);
return 0;
}
4-11
#include <stdio.h>
int main()
{int t,a,b,c,d;
printf("请输入四个数:");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);
if (a>b)
{ t=a;a=b;b=t;}
if (a>c)
{ t=a;a=c;c=t;}
if (a>d)
{ t=a;a=d;d=t;}
if (b>c)
{ t=b;b=c;c=t;}
if (b>d)
{ t=b;b=d;d=t;}
if (c>d)
{ t=c;c=d;d=t;}
printf("排序结果如下: \n");
printf("%d %d %d %d \n" ,a,b,c,d);
return 0;
}
4-12
#include <stdio.h>
int main()
{
int h=10;
float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;
printf("请输入一个点(x,y):");
scanf("%f,%f",&x,&y);
d1=(x-x4)*(x-x4)+(y-y4)*(y-y4); /*求该点到各中心点距离*/
d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);
d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);
d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);
if (d1>1 && d2>1 && d3>1 && d4>1) h=0; /*判断该点是否在塔外*/
printf("该点高度为 %d\n",h);
return 0;
}
5-3
#include <stdio.h>
int main()
{
int p,r,n,m,temp;
printf("请输入两个正整数n,m:");
scanf("%d,%d,",&n,&m);
if (n<m)
{
c语言程序分析题及答案 temp=n;
n=m;
m=temp;
}
p=n*m;
while(m!=0)
{
r=n%m;
n=m;
m=r;
}
printf("它们的最大公约数为:%d\n",n);
printf("它们的最小公约数为:%d\n",p/n);
return 0;
}
5-4
#include <stdio.h>
int main()
{
char c;
int letters=0,space=0,digit=0,other=0;
printf("请输入一行字符:\n");
while((c=getchar())!='\n')
{
if (c>='a' && c<='z' || c>='A' && c<='Z')
letters++;
else if (c==' ')
space++;
else if (c>='0' && c<='9')
digit++;
else
other++;
}
printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符数:%d\n",letters,space,digit,other);
return 0;
}
5-5
#include <stdio.h>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论