更新了40题,还剩24题
00x
L1-001 Hello World (5 分)
GPLT 最难的一题!!!
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
L1-002 打印沙漏 (20 分)
因为图形上下对称,所以可以先计算出上半图形的高度、宽度,这题主要就是考察逻辑思维。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, height = 1, width = 1;
char ch;
cin >> n >> ch;
--n; // 先扣去最中间的一个字符
while (true) {
if (n - 2 * (width + 2) >= 0) {
++height;
width += 2;
n -= 2 * width;
} else
break;
}
for (int i = 0; i < height; ++i) {
for (int j = 0; j < i; ++j) cout << ' ';
for (int j = 0; j < 2 * height - 1 - 2 * i; ++j) cout << ch;
cout << '\n';
}
for (int i = 0; i < height - 1; ++i) {
for (int j = 0; j < height - 2 - i; ++j) cout << ' ';
for (int j = 0; j < 2 * (i + 1) + 1; ++j) cout << ch;
cout << '\n';
}
cout << n;
return 0;
}
L1-003 个位数统计 (15 分)
虽然题目描述需要输入正整数,但是输入正整数需要一次一次取模才能得到各位,直接以字符串的方式输入就很简单。
#include <bits/stdc++.h>
using namespace std;
int main() {
int cnt[10] = {
0};
string num;
cin >> num;
for (int i = 0; i < num.size(); ++i) ++cnt[num[i] - '0'];
for (int i = 0; i < 10; ++i)
if (cnt[i] > 0) cout << i << ':' << cnt[i] << '\n';
return 0;
}
L1-004 计算摄氏温度 (5 分)
没有什么难点,L1 很多题都像这样。
#include <bits/stdc++.h>
using namespace std;
int main() {
int F;
cin >> F;
int C = 5 * (F - 32) / 9;
cout << "Celsius = " << C;
return 0;
}
L1-005 考试座位号 (15 分)
把试机座位号当作下标,然后准考证号和考试座位号分别存入 a, b 数组,最后输出就好。(准考证号 16 位,int 装不下)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL x, a[1005];
int n, m, y, z, b[1005];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%lld%d%d", &x, &y, &z);
a[y] = x;
b[y] = z;
}
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
scanf("%d", &y);
printf("%lld %d\n", a[y], b[y]);
}
return 0;
}
L1-006 连续因子 (20 分)
用循环模拟出可能的连续因子序列。需要注意几个优化的地方,for (int i = 2; i <= sqrt(n); ++i)
,很好理解,大于 sqrt(n)
的数再随便乘一下就大于 n 了,肯定就不是因子,所以这里只走到 sqrt(n)
。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, tmp;
int start, len;
int main() {
scanf("%lld", &n);
for (int i = 2; i <= sqrt(n); ++i) {
tmp = 1;
for (int j = i; tmp * j <= n; ++j) {
tmp *= j;
if (n % tmp) break; // 如果tmp不是n的因子,再乘任何数也不是
if (j - i + 1 > len) {
start = i;
len = j - i + 1;
}
}
}
if (!start) {
start = n;
len = 1;
}
printf("%d\n%d", len, start);
for (int i = 1; i < len; ++i) printf("*%d", start + i);
return 0;
}
L1-007 念数字 (10 分)
只要注意行尾不能有空格就好了。
#include <bits/stdc++.h>
using namespace std;
string a[] = {
"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int main() {
string s;
cin >> s;
if (s[0] == '-')
cout << "fu";
else
cout << a[s[0] - '0'];
for (int i = 1; i < s.size(); ++i)
cout << ' ' << a[s[i] - '0'];
return 0;
}
L1-008 求整数段和 (10 分)
只能说 GPLT 的题太细节了,不难的一题但是正确率很低,因为很容易出现刚好 5 个数时,最后连换两行的情况。
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, sum = 0;
scanf("%d%d", &a, &b);
for (int i = a; i <= b; ++i) {
sum += i;
printf("%5d", i);
if ((i - a + 1) % 5 == 0)
printf("\n");
else if (i == b)
printf("\n");
}
printf("Sum = %d", sum);
return 0;
}
L1-009 N 个数求和 (20 分)
分数求和,难点就是通分,需要求最大公约数,可以用欧几里得算法(gcd)求得。最后还有一点需要注意,测试数据是在长整型范围内的。
#include <bits/stdc++.h>
typedef long long LL