1.代理费计算
#include <stdio.h>
int main() {
float before;
int n = 0;
scanf_s("%f", &before);
if (before > 0 && before < 3000)
n = 1;
else if (before >= 3000 && before <= 6000)
n = 2;
else if (before >= 6000 && before < 10000)
n = 3;
switch (n) {
case 1:printf("the cost is %.2f RMB", before * 0.012 + 18); break;
case 2:printf("the cost is %.2f RMB", before * 0.006 + 36); break;
case 3:printf("the cost is %.2f RMB", before * 0.003 + 54); break;
default:printf("data error!"); break;
}
return 0;
}
2.分段函数求解
#include <stdio.h>
#include <math.h>
int main() {
float a, x, y;
scanf_s("a=%f,x=%f", &a, &x);
if (x == a || x == -a)
y = 0;
else if (x > -a && x < a)
y = sqrt(pow(a, 2) - pow(x, 2));
else
y = x;
printf("a=%.1f,x=%.1f,y=%.2f", a, x, y);
}
3.某点的建筑高度计算
#include <stdio.h>
#include <math.h>
int main() {
float x, y;
float d1, d2, d3, d4;
int h;
scanf_s("%f,%f", &x, &y);
d1 = sqrt(pow(x - 2, 2) + pow(y - 2, 2));
d2 = sqrt(pow(x - 2, 2) + pow(y + 2, 2));
d3 = sqrt(pow(x + 2, 2) + pow(y - 2, 2));
d4 = sqrt(pow(x + 2, 2) + pow(y + 2, 2));
if (d1 <= 1 || d2 <= 1 || d3 <= 1 || d4 <= 1)
h = 10;
else
h = 0;
printf("height is %d", h);
return 0;
}
4.百分制成绩转化为等级
#include <stdio.h>
int main() {
int score, n = 0;
scanf_s("%d", &score);
if (score >= 0 && score < 60)
n = 1;
else if (score < 70)
n = 2;
else if (score < 80)
n = 3;
else if (score < 90)
n = 4;
else if (score <= 100)
n = 5;
switch (n) {
case 1:printf("Level E"); break;
case 2:printf("Level D"); break;
case 3:printf("Level C"); break;
case 4:printf("Level B"); break;
case 5:printf("Level A"); break;
default:printf("Input Error"); break;
}
return 0;
}