团体程序设计天梯赛 L1-026~L1-030

L1-026

思路:

输出一次换行一次

代码:

#include<bits/stdc++.h>

using namespace std;

int main() {
	string s = "I Love GPLT";
	for(char & c : s) {
		putchar(c);
		putchar('
');	
	}
	return 0;
}

L1-027

思路:

映射一下即可

代码:

#include<bits/stdc++.h>

using namespace std;

int cnt[15], pos[15];
void out(vector<int> & v){ cout << v[0]; for(int i = 1; i < v.size(); ++i) putchar(','), cout << v[i]; cout << "};
"; }

int main() {
	string s;
	cin >> s;
	for(char & c : s) ++cnt[c- '0'];
	vector<int> rcd, pos(15), tel;
	for(int i = 10; i >= 0; --i) if(cnt[i]) pos[i] = rcd.size(), rcd.push_back(i);
	cout << "int[] arr = new int[]{";
	out(rcd);
	for(char & c : s) tel.push_back(pos[c - '0']);
	cout << "int[] index = new int[]{";
	out(tel);
	return 0;
}

L1-028

思路:

学会O(n)O(sqrt{n})复杂度判断素数

代码:

#include<bits/stdc++.h>

using namespace std;

bool isPrime(int n){
	int sq = sqrt(n);
	for(int i = 2; i <= sq; i++){
		if(n % i == 0) return false;
	}
	return n != 1;
}

int main() {
	int kase, n;
	cin >> kase;
	while(kase--){
		cin >> n;
		puts(isPrime(n) ? "Yes" : "No");	
	}
	return 0;
}

L1-029

思路:

照意思计算即可

代码:

#include<bits/stdc++.h>

using namespace std;

int main() {
	double h;
	cin >> h;
	printf("%.1f", (h - 100) * 1.8);
	return 0;
}

L1-030

思路:

从排名高到底输出,需要配合排名最低的男生/女生;维护一下男生女生当前最低的是谁就行;

代码:

#include<bits/stdc++.h>

using namespace std;
typedef pair<string, int> P;
#define pb(x) push_back(x) 

int main() {
	int n;
	cin >> n;
	vector<string> boy, girl;
	vector<pair<string, int> > tot; 
	for(int i = 0; i < n; i++){
		int x;
		string s;
		cin >> x >> s;
		if(x) boy.pb(s); else girl.pb(s);
		tot.pb(P(s, x));	
	}
	int x = n / 2, y = x;  //boy girl
	map<string, bool> vst;
	for(P & p : tot){
		string now = p.first;
		if(vst[now]) continue;
		if(p.second){
			cout << now << ' ' << girl[--y] << '
';
			vst[girl[y]] = true;
		}else{
			cout << now << ' ' << boy[--x] << '
';
			vst[boy[x]] = true;
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308685.html