团体程序设计天梯赛 L1-021~L1-025

L1-021

思路:

循环输出

代码:

#include<bits/stdc++.h>

using namespace std;

int main() {
	int k = 3;
	while(k--) puts("I'm gonna WIN!");
	return 0;
}

L1-022

思路:

判断奇偶,计数

代码:

#include<bits/stdc++.h>

using namespace std;

int main() {
	int n, x = 0, y = 0;
	cin >> n;
	while(n--){
		int a;
		cin >> a;
		if(a & 1) ++x;
		else ++y;	
	}
	cout << x << ' ' << y;
	return 0;
}

L1-023

思路:

计数

代码:

#include<bits/stdc++.h>

using namespace std;

char a[4] = {'G', 'P', 'L', 'T'};
int cnt[4];

int main() {
	string s;
	cin >> s;
	for(char & c : s) {
		c = toupper(c);	
		for(int i = 0; i < 4; i++) {
			if(c == a[i]) { ++cnt[i]; break; }	
		}
	}
	while(true) {
		bool ans = 0;	
		for(int i = 0; i < 4; i++) {
			if(cnt[i]) putchar(a[i]), --cnt[i], ans = true;
		}
		if(!ans) break;
	}
	return 0;
}

L1-024

思路:

+2,多了就减

代码:

#include<bits/stdc++.h>

using namespace std;

int main() {
	int x;
	cin >> x;
	x += 2;
	if(x <= 7) cout << x;
	else cout << x - 7;
	return 0;
}

L1-025

思路:

对字符串进行检测即可

代码:

#include<bits/stdc++.h>

using namespace std;

int deal(string & s){
	for(char & c : s){
		if(c < '0' || c > '9') return -1;	
	}
	int num = stoi(s);
	if(num < 1 || num > 1000) return -1;
	return num;
}
#define ELSE else putchar('?')
int main() {
	string a, b;
	cin >> a;
	getchar();
	getline(cin, b);
	int x = deal(a), y = deal(b);
	if(~x) cout << a; ELSE;
	cout << " + ";
	if(~y) cout << b; ELSE;
	cout << " = ";
	if((~x) && (~y)) cout << x + y; ELSE;
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308692.html