团体程序设计天梯赛-练习集L1-021~030

L1-021 重要的话说三遍 (5 分)

1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <cstring>
#include <iostream>
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 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int n;
int js, os;

int main()
{
cin >> n;
for(int i = 0;i < n;i ++)
{
int t;
cin >> t;
if(t % 2 == 0) os ++;
else js ++;
}
cout << js << ' ' << os << endl;
return 0;
}

L1-023 输出GPLT (20 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
string s;
map<char, int> m;

int main() {
cin >> s;
for (int i = 0; i < s.size(); i++) {
char c = s[i];
if (c == 'g' || c == 'G')
m['G']++;
else if (c == 'p' || c == 'P')
m['P']++;
else if (c == 'l' || c == 'L')
m['L']++;
else if (c == 't' || c == 'T')
m['T']++;
}
// 为了循环好写,求个最大值,注意 max 函数只能两个两个来比较,所以写起来怪怪的
int maxs = max(m['G'], max(m['P'], max(m['L'], m['T'])));
for (int i = 0; i < maxs; i++) {
if (m['G'] > 0) {
cout << "G";
m['G']--;
}
if (m['P'] > 0) {
cout << "P";
m['P']--;
}
if (m['L'] > 0) {
cout << "L";
m['L']--;
}
if (m['T'] > 0) {
cout << "T";
m['T']--;
}
}
cout << endl;
return 0;
}

L1-024 后天 (5 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int d[] = {0,3,4,5,6,7,1,2};

int main()
{
int n;
cin >> n;
cout << d[n] << endl;
return 0;
}

L1-025 正整数A+B (15 分)

https://pintia.cn/problem-sets/994805046380707840/problems/994805110318678016

不知道为什么有部分错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

string t;
int a, b;

int main() {
cin >> t;
bool flag1 = true;
for (int i = 0; i < t.size(); i++)
if (t[i] < '0' || t[i] > '9') flag1 = false;

if (flag1) a = stoi(t);

cin >> t;
bool flag2 = true;
for (int i = 0; i < t.size(); i++)
if (t[i] < '0' || t[i] > '9') flag2 = false;

if (flag2) b = stoi(t);

if (flag1 && flag2)
cout << a << " + " << b << " = " << a + b << endl;
else if (flag1 && !flag2)
cout << a << " + ? = ?" << endl;
else if (!flag1 && flag2)
cout << "? + " << b << " = ?" << endl;
else if (!flag1 && !flag2)
cout << "? + ? = ?" << endl;

return 0;
}

L1-026 I Love GPLT (5 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
cout << 'I' << endl;
cout << ' ' << endl;
cout << 'L' << endl;
cout << 'o' << endl;
cout << 'v' << endl;
cout << 'e' << endl;
cout << ' ' << endl;
cout << 'G' << endl;
cout << 'P' << endl;
cout << 'L' << endl;
cout << 'T' << endl;
return 0;
}

L1-027 出租 (20 分)

https://pintia.cn/problem-sets/994805046380707840/problems/994805107638517760


这个题目一眼看上去没什么思路,感觉怪怪的会有好多个解决方案,但是仔细一看,第一行int[] arr = new int[]{xxxx}保存的是这个手机号所有的数字,而第二行int[] index = new int[]{xxx}保存的是序号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
/*
18013820100
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};
*/
string n;
int main() {
cin >> n;
for (int i = 0; i < n.size(); i++) v.push_back(n[i] - '0');
// 处理第一行
// 从大到小排序
sort(v.begin(), v.end(), greater<int>());
// 删除重复元素
v.erase(unique(v.begin(), v.end()), v.end());
cout << "int[] arr = new int[]{";
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i != v.size() - 1) cout << ",";
}
cout << "};" << endl;
// 处理第二行
cout << "int[] index = new int[]{";
for (int i = 0; i < n.size(); i++) {
// 输出位置
cout << find(v.begin(), v.end(), n[i] - '0') - v.begin();
if (i != n.size() - 1) cout << ",";
}
cout << "};" << endl;
return 0;
}

L1-028 判断素数 (10 分)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int n;
typedef long long ll;

bool isprime(ll x) {
if (x < 2) return false;
for (ll i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
int main()
{
cin >> n;

for(int i = 0;i < n;i ++)
{
ll a;
cin >> a;
if(isprime(a))
cout << "Yes" << endl;
else
cout << "No" << endl;
}

return 0;
}

L1-029 是不是太胖了 (5 分)

1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
float H;
cin >> H;
printf("%.1f\n", ( H - 100 )* 0.9 * 2);
return 0;
}

L1-030 一帮一 (15 分)


https://pintia.cn/problem-sets/994805046380707840/problems/994805103557459968

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int n;
vector<int> all;
vector<string> a, b;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int g;
string name;
cin >> g >> name;
all.push_back(g);
if (g)
a.push_back(name);
else
b.push_back(name);
}
int fa = 0, fb = 0;
int ba = a.size() - 1, bb = b.size() - 1;
for (int i = 0; i < all.size() / 2; i++) {
if (all[i] == 0)
cout << b[fb++] << " " << a[ba--] << endl;
else
cout << a[fa++] << " " << b[bb--] << endl;
}

return 0;
}