2059 买笔
#include <stdio.h>
int main() {
int x;
int sixYuanPens = 0, fiveYuanPens = 0, fourYuanPens = 0;
int found = 0;
scanf("%d", &x);
for (sixYuanPens = 0; sixYuanPens * 6 <= x; sixYuanPens++) {
for (fiveYuanPens = 0; (sixYuanPens * 6 + fiveYuanPens * 5) <= x; fiveYuanPens++) {
int remainingMoney = x - (sixYuanPens * 6 + fiveYuanPens * 5);
fourYuanPens = remainingMoney / 4;
if ((sixYuanPens * 6 + fiveYuanPens * 5 + fourYuanPens * 4) == x) {
found = 1;
printf("%d %d %d\n", sixYuanPens, fiveYuanPens, fourYuanPens);
break;
}
}
if (found) break;
}
return 0;
}
1051 分段函数
#include <stdio.h>
int main() {
double x, y;
scanf("%lf", &x);
if (x >= 0 && x < 5) {
y = -x + 2.5;
} else if (x >= 5 && x < 10) {
y = 2 - 1.5 * (x - 3) * (x - 3);
} else if (x >= 10 && x < 20) {
y = x / 2 - 1.5;
} else {
printf("x的值不在定义的区间内。\n");
return 1;
}
printf("%.3lf\n", y);
return 0;
}
1050 骑车与走路
#include <stdio.h>
int main() {
double distance; // 距离,单位:米
double bikeTime, walkTime; // 骑车和步行所需时间,单位:秒
double bikeFixedTime = 27 + 23; // 骑车固定时间(找车、开锁、停车、锁车),单位:秒
double bikeSpeed = 3.0; // 骑车速度,单位:米/秒
double walkSpeed = 1.2; // 步行速度,单位:米/秒
scanf("%lf", &distance);
// 计算骑车所需时间
bikeTime = bikeFixedTime + distance / bikeSpeed;
// 计算步行所需时间
walkTime = distance / walkSpeed;
// 比较两者时间并输出结果
if (bikeTime < walkTime) {
printf("Bike\n");
} else if (bikeTime > walkTime) {
printf("Walk\n");
} else {
printf("All\n");
}
return 0;
}
1055 判断闰年
#include <stdio.h>
int main() {
int year;
// 提示用户输入年份
scanf("%d", &year);
// 判断是否是闰年
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("Y\n");
} else {
printf("N\n");
}
return 0;
}
1054 三角形判断
#include <stdio.h>
int main() {
int a, b, c;
// 提示用户输入三条线段的长度
scanf("%d %d %d", &a, &b, &c);
// 检查是否满足三角形的三边关系定理
if (a + b > c && a + c > b && b + c > a) {
printf("yes\n");
} else {
printf("no\n");
}
return 0;
}