要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E".“E"代替"A”。因此,“China"应译为"Glmre”。请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a1 = 'C',a2='h',a3='i',a4='n',a5='a';
a1 = a1 + 4;
a2 = a2 + 4;
a3 = a3 + 4;
a4 = a4 + 4;
a5 = a5 + 4;
printf("%c%c%c%c%c",a1,a2,a3,a4,a5);
return 0;
}
已知某班有男同学x位,女同学y位,x位男生平均分是87分,y位女生的平均分是85,问全体同学平均分是多少分?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c = (87 * a + 85 * b)/(a + b);
printf("%d",c);
return 0;
}
一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至今要喝多少桶水才能解渴。
pi取double型,值为3.14159
#include <stdio.h>
#include <stdlib.h>
int main()
{
int h,r,c,d;
double a,b,pi = 3.14159;
scanf("%d %d",&h,&r);
a = pi * r * r * h;
b = 20000 / a;
c = b;
if (b > c)
b = b + 1;
else b = b;
d = b;
printf("%d",d);
return 0;
}
对于半径为 r 的球,其体积的计算公式为V=(4/3)πr^3,这里取 π=3.14。现给定 r,即球半径,类型为double,求球的体积V,保留到小数点后两位。
#include <stdio.h>
#include <stdlib.h>
int main()
{
double r,v,pi;
scanf("%lf",&r);
pi = 3.14;
v =4 * pi * r * r * r/3;/*很奇怪 如果这里是用(4/3)* pi * r * r * r的话,运行结果是200.96?*/
printf("%.2lf",v);
return 0;
}
甲流并不可怕,在中国,它的死亡率并不是很高。请根据截止2009年12月22日各省报告的甲流确诊数和死亡数,计算甲流在各省的死亡率。
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a,b,c,d;
scanf("%lf %lf",&a,&b);
c = (b / a)*100;
printf("%.3lf%%",c);
return 0;
}
对于多项式f(x)=ax3+bx2+cx+d和给定的a,b,c,d,x,计算f(x)值,保留到小数点后7位。
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a,b,c,d,e,x;
scanf("%lf %lf %lf %lf %lf",&x,&a,&b,&c,&d);
e = a*x*x*x+b*x*x+c*x+d;
printf("%.7lf",e);
return 0;
}
读入一个字符、一个整数、一个单精度浮点数、一个双精度浮点数,然后按顺序输出它们,并且要求在它们之间用一个空格分隔。输出浮点数时保留6位小数。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
int b;
float c;
double d;
scanf("%c",&a);
scanf("%d",&b);
scanf("%f",&c);
scanf("%lf",&d);
printf("%c %d %.6f %.6lf",a,b,c,d);
return 0;
}
给定一个整数n,判断其正负。如果n>0,输出positive;如果n= 0 ,输出zero;如果n<0,输出negative。
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a;
scanf("%lf",&a);
if (a > 0)
printf("positive\n");
else if (a < 0)
printf("negative\n");
else
printf("zero\n");
return 0;
}
读入一个整数a,如果这个数大于1并且小于100,则输出yes,否则输出no。
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a;
scanf("%lf",&a);
if (a > 1 && a < 100)
printf("yes\n");
else printf("no\n");
return 0;
}
输入两个正整数A和B,求A*B的值。注意乘积的范围和数据类型的选择。
输入一行,包含两个正整数A和B,中间用单个空格隔开。1<=A,B <= 50000。
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long a,b,c,d,e;
scanf("%lld %lld",&a,&b);
c = a * b;
printf("%lld",c);
return 0;
}
已知线段的两个端点坐标A(xa,ya),B(xb,yb),求线段AB的长度,保留到小数点后3位。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double a,b,c,d,x1,y1,x2,y2;
scanf("%lf %lf\n",&x1,&y1);
scanf("%lf %lf\n",&x2,&y2);
a = (x1 - x2);
b = (y1 - y2);
c = a * a + b * b;
d = sqrt(c);
printf("%.3lf\n",d);
return 0;
}
平面上有一个三角形,它的三个顶点坐标分别为(xa,ya),(xb,yb),(xc,yc),那么请问这个三角形的面积是多少,精简到小数点后两位。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double a,b,c,a1,b1,c1,d,e,p,a2,b2,c2,x1,y1,x2,y2,x3,y3;
scanf("%lf %lf\n",&x1,&y1);
scanf("%lf %lf\n",&x2,&y2);
scanf("%lf %lf\n",&x3,&y3);
a = (x1 - x2);
b = (y1 - y2);
c = sqrt(a * a + b * b);
a1 = (x1 - x3);
b1 = (y1 - y3);
c1 = sqrt(a1 * a1 + b1 * b1);
a2 = (x2 - x3);
b2 = (y2 - y3);
c2 = sqrt(a2 * a2 + b2 * b2);
p = (c + c1 +c2)/2;
e = p*(p-c)*(p-c1)*(p-c2);
d = sqrt(e);
printf("%.2lf\n",d);
return 0;
}
利用for循环,输入两个数a,b,分别计算a~b中偶数的和、奇数的和。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,i,sum1 = 0,sum2 = 0;
scanf("%d %d",&a,&b);
for (i = a;i <= b;i++)
{
if(i%2 == 0)
sum1 =sum1 + i;
}
for (i = a;i <= b;i++)
{
if(i%2 == 1)
sum2 =sum2 + i;
}
printf("%d %d\n",sum1,sum2);
return 0;
}
判断一个正整数是否是两位数(即大于等于10且小于等于99)。若该正整数是两位数,则输出1,否则输出0。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
scanf("%d\n",&a);
if (a >= 10 && a < 100)
printf("1\n");
else
printf("0\n");
return 0;
}
输入一个整数,即字符的ASCII码,保证存在对应的可见字符,输出相对应的字符。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
scanf("%d\n",&a);
printf("%c\n",a);
return 0;
}
读入一个整数a,如果a为偶数在屏幕输出yes。如果a为奇数则输出no。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
scanf("%d",&i);
if(i%2 == 0)
printf("yes\n");
else
printf("no\n");
return 0;
}
输入温度t的值,判断是否适合晨练。若25<=t<=30,则适合晨练,否则不适合。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
scanf("%d",&i);
if(i <= 30 && i >= 25)
printf("ok\n");
else
printf("no\n");
return 0;
}
下面这个是什么题来着……?
include <stdio.h>
#include <stdlib.h>
int main()
{
int n,x,y,a;
scanf("%d %d %d",&n,&x,&y);
if (y/x <= n){
if (y%x == 0){
a = y / x;
}
else
a = y / x + 1;
}
else a = n;
printf("%d\n",n - a);
return 0;
}