第一个就是一个分段函数,if esle if else语句就搞定了
(1)
#include <stdio.h>
int main()
{
int x, y;
scanf("%d", &x); //从键盘读取x的值
if (x < 1)
y = x;
else if (x >= 1 && x < 10)
y = 3 - x / 5;
else
y = 3 * x - 11;
printf("%d", y);
return 0;
}
(2)
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c;
double p,s;
scanf("%lf %lf %lf", &a, &b, &c);//输入3个实数,以空格隔开
if (a + b > c && a + c > b && b + c > a)
{
//可以构成三角形
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
printf("三角形的面积为:%lf", s);
}
else
printf("无法构成三角形");
return 0;
}
(3)
#include <stdio.h>
int main()
{
int days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//12个月份的天数
int y, m, d,i;
int sum = 0;
printf("请输入年 月 日:");//输入年月日,以空格隔开
scanf("%d %d %d", &y, &m, &d);
//判断是否是闰年
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
days[2] = 29;//闰年2月是29天
for (i = 1; i < m; i++)
sum += days[i];
sum += d;
printf("%d年%d月%d日是今年的第%d天", y, m, d, sum);
return 0;
}
(4)
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c;
double q,x1,x2;
printf("请输入a b c的值:");
scanf("%lf %lf %lf",&a,&b,&c);
q = b * b - 4 * a * c;
if (q > 0)
{
printf("方程有2个实根:");
x1 = (-b + sqrt(q)) / (2 * a);
x2 = (-b - sqrt(q)) / (2 * a);
printf("x1=%lf ,x2=%lf", x1, x2);
}
else if (q == 0)
{
printf("方程有1个实根:");
x1 = -b / (2 * a);
printf("x1=x2=%lf", x1);
}
else
{
printf("方程没有实根,虚数解为:");
x1 = -b / (2 * a);
x2 = sqrt(-q) / (2 * a);
printf("x1=%lf+%lfi , x2=%lf-%lfi", x1, x2, x1, x2);
}
return 0;
}
(5)
#include <stdio.h>
int main()
{
int n;
int t, m,s=0;
printf("请输入一个数:");
scanf("%d", &n);
t = n;
while (t)
{
m = t % 10;
s = s + m * m * m;
t /= 10;
}
if (s == n)
printf("%d是水仙花数", n);
else
printf("%d不是水仙花数", n);
return 0;
}