7-1 温度转换
#include<stdio.h>
int main() {
int F = 150, C;
C = 5 * (F - 32) / 9;
printf("fahr = 150, celsius = %d", C);
return 0;
}
7-2 计算物体自由下落的距离
#include<stdio.h>
int main() {
int g = 10;
int t = 3;
printf("height = %.2f", g * t * t / 2.0);
return 0;
}
7-3 整数四则运算
#include<stdio.h>
int main()
{
int a,b,c,d,e,f;
scanf("%d %d",&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf("%d + %d = %d\n",a,b,c);
printf("%d - %d = %d\n",a,b,d);
printf("%d * %d = %d\n",a,b,e);
printf("%d / %d = %d",a,b,f);
return 0;
}
7-4 计算分段函数[1]
#include<stdio.h>
int main() {
double x;
scanf("%lf", &x);
if (x != 0) {
printf("f(%.1f) = %.1f", x, 1 / x);
} else {
printf("f(%.1f) = %.1f", 0, 0);
}
return 0;
}
7-5 计算分段函数[2]
#include<stdio.h>
#include<math.h>
int main() {
double x;
scanf("%lf", &x);
if (x >= 0) {
printf("f(%.2f) = %.2f", x, sqrt(x));
} else {
printf("f(%.2f) = %.2f", x, pow(x + 1, 2) + 2 * x + 1 / x);
}
return 0;
}
7-6 求N分之一序列前N项和
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
double sum = 0;
int i = 1;
while (n--) {
sum += 1.0 / i++;
}
printf("sum = %f", sum);
return 0;
}
7-7 求简单交错序列前N项和
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
double sum = 0;
int i = 1;
while (n--) {
sum += 1.0 / i;
if (i > 0) {
i = i + 3;
} else if (i < 0) {
i = i - 3;
}
i = -i;
}
printf("sum = %.3f", sum);
return 0;
}
7-8 求组合数
#include<stdio.h>
double fact(int n) {
double sum = 1;
for (int i = 1; i <= n; i++) {
sum *= i;
}
return sum;
}
int main() {
int m, n;
scanf("%d %d", &m, &n);
printf("result = %.0lf\n", fact(n) / (fact(m) * fact(n - m)));
return 0;
}
7-9 求整数均值
#include<stdio.h>
int main() {
double sum = 0;
int temp;
scanf("%d", &temp);
sum += temp;
scanf("%d", &temp);
sum += temp;
scanf("%d", &temp);
sum += temp;
scanf("%d", &temp);
sum += temp;
printf("Sum = %.0f; Average = %.1f", sum, sum / 4);
return 0;
}
7-10 阶梯电价
#include<stdio.h>
int main() {
double x;
scanf("%lf", &x);
double sum = 0;
if (x <= 50 && x >= 0) {
sum = x * 0.53;
printf("cost = %.2f", sum);
} else if (x > 50) {
sum = 26.5 + (x - 50) * 0.58;
printf("cost = %.2f", sum);
} else {
printf("Invalid Value!");
}
return 0;
}