A 诚信参赛
如果你答案错误,请检查:1. 是否是英语输入状态下的标点符号;2. 逗号后面有个空格。
写这类题时,建议直接复制题目需要输出的内容粘贴到代码里。
#include <stdio.h>
int main(void) {
printf("sheng si kan dan, cheng xin xun lian!");
return 0;
}
B 计算罚时
#include <stdio.h>
int main(void) {
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a + b * 20);
return 0;
}
C 踱步
按照题目要求模拟过程即可。
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
int lengthSum = 0;
int x = 0, y = 0;
for (int i = 0; i < n; ++i) {
int direction, length;
scanf("%d %d", &direction, &length);
lengthSum += length;
if (direction == 1) {
x += length;
} else if (direction == 2) {
x -= length;
} else if (direction == 3) {
y -= length;
} else {
y += length;
}
}
printf("%d %d\n", x, y);
printf("%d\n", lengthSum);
return 0;
}
D 长跑评级
用秒表示成绩,方便比较。例如 3'45'' 等于 225 秒。
#include <stdio.h>
int main(void) {
int minute, second;
int failCount = 0;
scanf("%d", &minute);
while (minute != -1) {
scanf("%d", &second);
int totalSeconds = minute * 60 + second;
if (totalSeconds <= 207) {
printf("Outstanding\n");
} else if (totalSeconds <= 222) {
printf("Good\n");
} else if (totalSeconds <= 272) {
printf("Pass\n");
} else {
printf("Fail\n");
++failCount;
}
scanf("%d", &minute);
}
printf("%d", failCount);
return 0;
}
E 找坐标
嵌套 for 循环,遍历每一行和每一列。如果找到 k,立刻输出然后结束程序。
#include <stdio.h>
int main(void) {
int n, k;
scanf("%d %d", &n, &k);
/* r 代表行,c 代表列 */
for (int r = 1; r <= n; ++r) {
for (int c = 1; c <= n; ++c) {
int num;
scanf("%d", &num);
if (num == k) {
printf("%d %d", r, c);
return 0;
}
}
}
return 0;
}
F 连续五天早八
判断是否有 5 个连续的 1 即可。
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
int count = 0;
for (int i = 0; i < n; ++i) {
int haveClass;
scanf("%d", &haveClass);
if (haveClass) {
++count;
} else {
count = 0;
}
if (count == 5) {
printf("Yes");
return 0;
}
}
printf("No");
return 0;
}
G 求序列和
n 最大可以取 16,用 int 可能会溢出,因此用 long long。
这题不能用 double,因为 double 的尾数位只有 52 个二进制位,最多只能精确表示 15 或 16 个十进制有效数字。
深入了解可以搜索 IEEE754 标准相关解读。
#include <stdio.h>
int main(void) {
int a, n;
long long sum = 0;
long long term = 0;
scanf("%d %d", &a, &n);
for (int i = 0; i < n; i++) {
term = term * 10 + a; // 构造每一项
sum += term; // 累加到总和
}
printf("%lld", sum);
return 0;
}
H ReLU 函数
分段函数,用 if 判断 x 与 0 的大小关系。
- 如果 x ≤ 0,f(x) = 0。
- 如果 x > 0,f(x) = x。
#include <stdio.h>
int main(void) {
int x;
scanf("%d", &x);
if (x <= 0) {
printf("0");
} else {
printf("%d", x);
}
return 0;
}