(工具:C)
1.从键盘输入一个圆半径,计算并输出圆周长、圆面积和球体积。
如: 输入 1.5<回车>
输出 圆周长:9.42,圆面积:7.06,球体积:14.13
#include <stdio.h>
float main(void)
{
float r,l,s,v;
printf("输入一个圆半径:\n");
scanf("%f",&r);
l = 2 * 3.14 * r;
s = 3.14 * r * r;
v = 4 / 3 * 3.14 * r * r * r;
printf("圆周长:%f,圆面积:%f,球体积:%f\n",l,s,v);
return 0;
}
2.从键盘输入任意三个整数,将它们按从大到小的顺序排序后重新输出。
#include <stdio.h>
int main(void)
{
int a,b,c,t;
printf("Please enter three integers:\n");
scanf("%d %d %d",&a,&b,&c);
if (a<b){
t = a;
a = b;
b = t;
}
if (a<c){
t = a;
a = c;
c = t;
}
if (b<c){
t = b;
b = c;
c = t;
}
printf("%d %d %d\n",a,b,c);
return 0;
}
3.从键盘输入一元二次方程ax2+bx+c=0 的三个系数a、b、c,若方程有两个相异实根则输出两个根;若方程有相等实根则输出根;若方程无实根则输出“无实根”。
#include <stdio.h>
#include <math.h>
float main()
{
float a,b,c,x1,x2,delta=0;
printf("Please enter a b c\n"); //请输入a b c
scanf("%f,%f,%f",&a,&b,&c);
if(a!=0)
{
delta=b*b-4*a*c;
if(delta<0)
printf("No real roots.\n"); //无实根
if(delta>0)
{
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
printf("x1=%f,x2=%f\n",x1,x2); //输出x1 x2
}
else
{
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
printf("x=%f\n",x1);
}
}
else
printf("[a] cannot be equal to 0.\n"); //若输入a=0则报错
return 0;
}
4.从键盘输入一个年份和月份, 判断并输出该年该月有多少天。
如: 输入 2008 2<回车>
输出 2008年2月有29天
#include <stdio.h>
int main(void)
{
int a,b;
printf("Please enter the number of your year and month:\n");
scanf("%d %d",&a,&b);
switch(b){
case 1:case 3:case 5:case 7:case 8:case 10:case 12: printf("31\n");
break;
case 4:case 6:case 9:case 11: printf("30\n");
break;
case 2:
if ((a%4==0&&a%100!=0)||(a%400==0)) printf("29\n");//讨论闰年情况
else printf("28\n");
break;
default: printf("Error\n");//输入异常月份数字反馈错误
break;
}
return 0;
}
(个人答案,如有错误或更正建议欢迎联系作者)