文章目錄
- 00x
- L1-001 Hello World (5 分)
- L1-002 打印沙漏 (20 分)
- L1-003 個位數統計 (15 分)
- L1-004 計算攝氏溫度 (5 分)
- L1-005 考試座位號 (15 分)
- L1-006 連續因子 (20 分)
- L1-007 念數字 (10 分)
- L1-008 求整數段和 (10 分)
- L1-009 N 個數求和 (20 分)
- 01x
- L1-010 比較大小 (10 分)
- L1-011 A-B (20 分)
- L1-012 計算指數 (5 分)
- L1-013 計算階乘和 (10 分)
- L1-014 簡單題 (5 分)
- L1-015 跟奧巴馬一起畫方塊 (15 分)
- L1-016 查驗身份證 (15 分)
- L1-017 到底有多二 (15 分)
- L1-018 大笨鐘 (10 分)
- L1-019 誰先倒 (15 分)
- 02x
- L1-020 帥到沒朋友 (20 分)
- L1-021 重要的話說三遍 (5 分)
- L1-022 奇偶分家 (10 分)
- L1-023 輸出 GPLT (20 分)
- L1-024 後天 (5 分)
- L1-025 正整數 A+B (15 分)
- L1-026 I Love GPLT (5 分)
- L1-027 出租 (20 分)
- L1-028 判斷素數 (10 分)
- L1-029 是不是太胖了 (5分)
- 03x
- L1-030 一幫一 (15分)
- L1-031 到底是不是太胖了 (10分)
- L1-032 Left-pad (20分)
- L1-033 出生年 (15分)
- L1-034 點贊 (20分)
- L1-035 情人節 (15分)
- L1-036 A乘以B (5分)
- L1-037 A除以B (10分)
- L1-038 新世界 (5分)
- L1-039 古風排版 (20 分)
- 05x
更新了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;
using namespace std;
LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
char c;
int n;
LL x, y, tmp;
cin >> n;
cin >> x >> c >> y;
LL sx = x, sy = y;
for (int i = 1; i < n; ++i) {
cin >> x >> c >> y;
tmp = sy / gcd(sy, y) * y;
sx = sx * (tmp / sy) + x * (tmp / y);
sy = tmp;
}
LL num = sx / sy;
sx %= sy;
tmp = gcd(sx, sy);
sx /= tmp;
sy /= tmp;
if (sx == 0)
cout << num;
else if (num == 0)
cout << sx << '/' << sy;
else
cout << abs(num) << ' ' << sx << '/' << sy;
return 0;
}
01x
L1-010 比較大小 (10 分)
簡單的排序。
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[3];
for (int i = 0; i < 3; ++i)
cin >> a[i];
sort(a, a + 3);
cout << a[0] << "->" << a[1] << "->" << a[2];
return 0;
}
L1-011 A-B (20 分)
字符串,查找,刪除。
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
getline(cin, a);
getline(cin, b);
for (int i = 0; i < b.size();) {
if (a.find(b[i]) != -1)
a.erase(a.find(b[i]), 1);
else
++i;
}
cout << a;
return 0;
}
L1-012 計算指數 (5 分)
水題。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << "2^" << n << " = " << pow(2, n);
return 0;
}
L1-013 計算階乘和 (10 分)
水題。
#include <bits/stdc++.h>
using namespace std;
int fac(int x) { return x == 1 ? 1 : fac(x - 1) * x; }
int main() {
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; ++i)
sum += fac(i);
cout << sum;
return 0;
}
L1-014 簡單題 (5 分)
水題。
#include <iostream>
using namespace std;
int main() {
cout << "This is a simple problem.";
return 0;
}
L1-015 跟奧巴馬一起畫方塊 (15 分)
主要就是四捨五入的部分,可以直接用 cmath
裏的 round
函數。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char c;
cin >> n >> c;
int m = round(n * 1.0 / 2);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j)
cout << c;
cout << endl;
}
return 0;
}
L1-016 查驗身份證 (15 分)
水題,就是麻煩了點。
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
int b[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int n;
cin >> n;
string s;
bool flag = false;
while (n--) {
int sum = 0;
cin >> s;
for (int i = 0; i < s.size() - 1; ++i)
sum += (s[i] - '0') * a[i];
sum %= 11;
if (b[sum] != s.back()) {
cout << s << endl;
flag = true;
}
}
if (!flag)
cout << "All passed";
return 0;
}
L1-017 到底有多二 (15 分)
用 cout << fixed << setprecision(2) 或 c 語言的 printf 控制小數點位數。
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int cnt = 0;
double flag = 1;
if (s[0] == '-') {
flag *= 1.5;
s.erase(s.begin(), s.begin() + 1);
}
if ((s.back() - '0') % 2 == 0) flag *= 2;
for (int i = 0; i < s.size(); ++i)
if (s[i] == '2') ++cnt;
cout << fixed << setprecision(2)
<< cnt * flag / s.size() * 100 << '%';
return 0;
}
L1-018 大笨鐘 (10 分)
注意 12 點整不需要敲,過了 12 點就要敲。
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, m;
scanf("%d:%d", &h, &m);
if (h >= 0 && h < 12 || h == 12 && m == 0)
printf("Only %02d:%02d. Too early to Dang.", h, m);
else {
if (m == 0)
h -= 12;
else
h -= 11;
while (h--)
printf("Dang");
}
return 0;
}
L1-019 誰先倒 (15 分)
假如酒量給的是 1,需要喝 2 杯才能倒,所以輸入兩人的酒量後,都需要加 1。另外題目描述第二行輸入一個正整數 N,實際上 N 根本沒用。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, _, a, b, c, d;
int cnt1 = 0, cnt2 = 0;
cin >> n >> m >> _;
++n, ++m;
while (n && m) {
cin >> a >> b >> c >> d;
int tmp = a + c;
if (b == tmp && d != tmp) {
--n;
++cnt1;
} else if (b != tmp && d == tmp) {
--m;
++cnt2;
}
}
if (n)
cout << "B\n" << cnt1;
else
cout << "A\n" << cnt2;
return 0;
}
02x
L1-020 帥到沒朋友 (20 分)
朋友圈人數如果爲 1,不做處理。大於 1 的話,說明有朋友,做個標記,注意輸出格式。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, id;
vector<bool> fri(100000, false), vis(100000, false);
cin >> n;
while (n--) {
cin >> k;
if (k == 1)
cin >> id;
else {
while (k--) {
cin >> id;
fri[id] = true;
}
}
}
cin >> n;
bool flag = false;
while (n--) {
cin >> id;
if (!fri[id] && !vis[id]) {
if (flag) cout << ' ';
cout << setfill('0') << setw(5) << id;
vis[id] = flag = true;
}
}
if (!flag) cout << "No one is handsome";
return 0;
}
L1-021 重要的話說三遍 (5 分)
水題。
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "I'm gonna WIN!" << endl;
cout << "I'm gonna WIN!" << endl;
cout << "I'm gonna WIN!" << endl;
return 0;
}
L1-022 奇偶分家 (10 分)
有個小技巧,用按位與運算判斷奇偶性是比模運算快的。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, a = 0, b = 0;
cin >> n;
while (n--) {
cin >> x;
if (x & 1)
++a;
else
++b;
}
cout << a << ' ' << b;
return 0;
}
L1-023 輸出 GPLT (20 分)
遍歷一遍字符串,分別統計’G’,’P’,’L’,’T’的個數(大寫小寫都算),最後用一個 while 循環控制輸出就好了。
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
int g = 0, p = 0, l = 0, t = 0;
for (int i = 0; i < str.size(); ++i) {
if (str[i] == 'g' || str[i] == 'G')
++g;
else if (str[i] == 'p' || str[i] == 'P')
++p;
else if (str[i] == 'l' || str[i] == 'L')
++l;
else if (str[i] == 't' || str[i] == 'T')
++t;
}
while (g || p || l || t) {
if (g) {
cout << 'G';
--g;
}
if (p) {
cout << 'P';
--p;
}
if (l) {
cout << 'L';
--l;
}
if (t) {
cout << 'T';
--t;
}
}
return 0;
}
L1-024 後天 (5 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
int d;
cin >> d;
cout << (d + 1) % 7 + 1;
return 0;
}
L1-025 正整數 A+B (15 分)
直接整行讀入,然後因爲題目描述第一個空格是兩個數的分隔,所以按第一個空格的位置分隔開,最後就是簡單的字符串轉數字。
#include <bits/stdc++.h>
using namespace std;
int getNum(string &s) {
int num = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] < '0' || s[i] > '9')
return -1;
num = num * 10 + s[i] - '0';
}
return (num >= 1 && num <= 1000) ? num : -1;
}
int main() {
string line, s1, s2;
getline(cin, line);
int space = line.find(' ');
s1 = line.substr(0, space);
s2 = line.substr(space + 1);
int n1 = getNum(s1), n2 = getNum(s2);
if (n1 != -1)
cout << n1 << " + ";
else
cout << "? + ";
if (n2 != -1)
cout << n2 << " = ";
else
cout << "? = ";
if (n1 != -1 && n2 != -1)
cout << n1 + n2;
else
cout << '?';
return 0;
}
L1-026 I Love GPLT (5 分)
真的水啊。
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "I\n \nL\no\nv\ne\n \nG\nP\nL\nT";
return 0;
}
L1-027 出租 (20 分)
vis 數組標記某個數字是否在號碼中出現,然後反向遍歷一遍把出現的數字放入 v1,同時 mp 映射數字在 v1 中的下標,方便接下來的 v2 數組的獲取。
#include <bits/stdc++.h>
using namespace std;
string tel;
bool vis[10];
vector<int> v1, v2;
map<int, int> mp;
int main() {
cin >> tel;
for (int i = 0; i < tel.size(); ++i) vis[tel[i] - '0'] = true;
for (int i = 9; i >= 0; --i) {
if (vis[i]) {
mp[i] = v1.size();
v1.push_back(i);
}
}
for (int i = 0; i < tel.size(); ++i) v2.push_back(mp[tel[i] - '0']);
cout << "int[] arr = new int[]{";
for (int i = 0; i < v1.size(); ++i) {
cout << v1[i];
if (i != v1.size() - 1) cout << ',';
}
cout << "};\nint[] index = new int[]{";
for (int i = 0; i < v2.size(); ++i) {
cout << v2[i];
if (i != v2.size() - 1) cout << ',';
}
cout << "};";
return 0;
}
L1-028 判斷素數 (10 分)
水題。
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int x) {
if (x < 2) return false;
for (int i = 2; i <= sqrt(x); ++i)
if (x % i == 0) return false;
return true;
}
int main() {
int n, x;
cin >> n;
while (n--) {
cin >> x;
if (isPrime(x))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
L1-029 是不是太胖了 (5分)
水題
#include <bits/stdc++.h>
using namespace std;
int main() {
int h;
scanf("%d", &h);
printf("%.1f\n", (1.0 * h - 100) * 0.9 * 2);
return 0;
}
03x
L1-030 一幫一 (15分)
三個標記,一個標記從前往後走,另外兩個標記(一個專門標記男,一個專門標記女),從後往前找與第一個標記對應的一幫一對象。
#include <bits/stdc++.h>
using namespace std;
struct student {
int sex;
string name;
};
int main() {
int n, sex, p0, p1;
string name;
cin >> n;
vector<student> students(n);
for (int i = 0; i < n; ++i) {
cin >> sex >> name;
students[i] = student{sex, name};
}
p0 = p1 = n - 1; // 分別標記倒數地女生和男生
// 因爲題目說,保證男女1:1,所以直接可以分成n/2組
for (int i = 0; i < n / 2; ++i) {
if (students[i].sex == 1) {
while (students[p0].sex == 1) --p0;
cout << students[i].name << ' ' << students[p0--].name << endl;
} else {
while (students[p1].sex == 0) --p1;
cout << students[i].name << ' ' << students[p1--].name << endl;
}
}
return 0;
}
L1-031 到底是不是太胖了 (10分)
水題
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, h, w;
float x;
cin >> n;
while (n--) {
cin >> h >> w;
x = (h - 100) * 0.9 * 2;
cout << "You are ";
if (fabs(x - w) < x * 0.1) {
cout << "wan mei!";
} else {
if (w > x)
cout << "tai pang le!";
else
cout << "tai shou le!";
}
cout << endl;
}
return 0;
}
L1-032 Left-pad (20分)
水題
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char c;
string s;
cin >> n >> c;
cin.get();
getline(cin, s);
if (n == s.size()) {
cout << s;
} else if (n < s.size()) {
cout << s.substr(s.size() - n);
} else {
int m = n - s.size();
string prefix(m, c);
cout << prefix + s;
}
return 0;
}
L1-033 出生年 (15分)
本來以爲枚舉然後用set去重可能會被卡時間,結果也能過。
#include <bits/stdc++.h>
using namespace std;
int year, n, age = 0;
set<int> s;
bool valid(int year) {
s.clear();
if (year < 1000) s.insert(0);
while (year) {
s.insert(year % 10);
year /= 10;
}
return s.size() == n;
}
int main() {
scanf("%d%d", &year, &n);
while (1) {
if (valid(year)) {
printf("%d %04d", age, year);
break;
}
++year;
++age;
}
return 0;
}
L1-034 點贊 (20分)
因爲標籤的編號範圍也不大,所以可以直接用數組做。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, x;
vector<int> v(1005, 0);
cin >> n;
while (n--) {
cin >> k;
while (k--) {
cin >> x;
++v[x];
}
}
int res, cnt = INT_MIN;
for (int i = 0; i < 1005; ++i) {
if (v[i] >= cnt) {
cnt = v[i];
res = i;
}
}
cout << res << ' ' << cnt << endl;
return 0;
}
L1-035 情人節 (15分)
水題。
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, a, b;
int cnt = 0;
while (cin >> s && s != ".") {
++cnt;
if (cnt == 2) a = s;
if (cnt == 14) b = s;
}
if (cnt >= 14)
cout << a << " and " << b << " are inviting you to dinner..." << endl;
else if (cnt >= 2)
cout << a << " is the only one for you..." << endl;
else
cout << "Momo... No one is for you ..." << endl;
return 0;
}
L1-036 A乘以B (5分)
水題。
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a * b;
return 0;
}
L1-037 A除以B (10分)
水題。
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a << '/';
if (b < 0) cout << '(';
cout << b;
if (b < 0) cout << ')';
cout << '=';
if (b == 0)
cout << "Error";
else
printf("%.2f", 1.0 * a / b);
return 0;
}
L1-038 新世界 (5分)
沒啥好說,c++都懶得用了,直接複製輸出,在網頁上直接敲python的print
print("""Hello World
Hello New World""")
L1-039 古風排版 (20 分)
別看它需要豎着很難受,其實可以先橫着填好,再旋轉輸出。
#include <bits/stdc++.h>
using namespace std;
int w, a[1005][1005];
string str;
int main() {
memset(a, -1, sizeof(a));
cin >> w;
cin.get();
getline(cin, str);
int h = ceil(str.size() * 1.0 / w), k = 0;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (k < str.size())
a[i][j] = k++;
}
}
for (int j = 0; j < w; ++j) {
for (int i = h - 1; i >= 0; --i) {
if (a[i][j] == -1)
cout << ' ';
else
cout << str[a[i][j]];
}
cout << endl;
}
return 0;
}
05x
L1-059 敲笨鍾 (20 分)
就是幾個知識點,getline 前用 cin.get() 吸收回車,然後一些字符串基本操作,find 正向查找,rfind 反向查找,substr 切割。最後就是需要考慮到一種特殊情況,逗號前的字符不足 3 個,那麼直接去使用 str.substr(p - 3, 3) == “ong” 判斷的話就會出現錯誤(最後一組測試數據),所以需要加上 p >= 3 這個前提條件。
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
int n;
cin >> n;
cin.get();
while (n--) {
getline(cin, str);
int p = str.find(',');
if (p >= 3 && str.substr(p - 3, 3) == "ong" &&
str.substr(str.size() - 4, 3) == "ong") {
p = str.size();
for (int i = 0; i < 3; ++i) p = str.rfind(' ', p - 1);
cout << str.substr(0, p) << " qiao ben zhong.\n";
} else
cout << "Skipped\n";
}
return 0;
}
用 regex 這個東西騷一下:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
string s;
cin >> n;
cin.get();
while (n--) {
getline(cin, s);
if (regex_match(s, regex(".*?ong,.*?ong\\.")))
cout << regex_replace(s, regex("(\\s\\w+){3}\\."), " qiao ben zhong.") << endl;
else
cout << "Skipped" << endl;
}
return 0;
}