春节刷题day8:PAT
1041 考试座位号
1042 字符统计
1043 输出PATest
1044 火星数字
1045 快速排序
1046 划拳
1047 编程团体赛
1048 数字加密
1049 数列的片段和
1050 螺旋矩阵
1、1041 考试座位号
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 2e7 + 5;
const int MaxN = 1e6;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T, tot;
int l, r, ans;
map<int, pair<string, int> > p;
string s;
int main(){
int i, j, k;
while( scanf("%d", &n) == 1){
while(n--){
cin >> s >> l >> r;
p[l] = mp(s, r);
}
scanf("%d", &m);
while(m--){
scanf("%d", &k);
pair<string, int> e = p[k];
cout << e.fi << " " << e.se << endl;
}
}
return 0;
}
2、1042 字符统计
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 2e7 + 5;
const int MaxN = 1e6;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T, tot;
int l, r, ans;
map<char, int> p;
string s;
int main(){
int i, j, k;
while( getline(cin, s)){
int len = s.size();
int Max = -1;
for(i = 0; i < len; i++){
if(s[i] >= 'A' && s[i] <= 'Z') s[i] += 'a' - 'A';
if(s[i] >= 'a' && s[i] <= 'z'){
p[s[i]]++;
Max = max(p[s[i]], Max);
}
}
for(map<char, int>::iterator it = p.begin(); it != p.end(); it++)
if(it -> se == Max){ printf("%c %d\n", it -> fi, it ->se); break; }
}
return 0;
}
3、1043 输出PATest
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 2e7 + 5;
const int MaxN = 1e6;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T, tot;
int l, r, ans;
map<char, int> p;
string s;
int main(){
int i, j, k;
while( cin >> s){
int len = s.size();
for(i = 0; i < len; i++) p[s[i]]++;
bool ok = true;
while(ok){
ok = false;
if(p['P'] > 0){ printf("P"); p['P']--; ok = true; }
if(p['A'] > 0){ printf("A"); p['A']--; ok = true; }
if(p['T'] > 0){ printf("T"); p['T']--; ok = true; }
if(p['e'] > 0){ printf("e"); p['e']--; ok = true; }
if(p['s'] > 0){ printf("s"); p['s']--; ok = true; }
if(p['t'] > 0){ printf("t"); p['t']--; ok = true; }
}
puts("");
}
return 0;
}
4、1044 火星数字
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<bitset>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T;
int l, r, ans;
string s;
string p[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string q[13] = {"---", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int main(){
int i, j, k;
scanf("%d", &T); getchar();
while( T-- ){
ans = 0;
getline(cin, s);
int len = s.size();
if(s[0] >= '0' && s[0] <= '9'){
reverse(s.begin(), s.end()); k = 1;
for(i = 0; i < len; i++){ ans += (s[i] - '0') * k; k *= 10; }
l = ans / 13; r = ans % 13;
if(l && r) cout << q[l] << " " << p[r] << endl;
else if(l) cout << q[l] << endl;
else cout << p[r] << endl;
}else{
for(i = 0; i < len; i++) if(s[i] == ' ') break;
if(i != len){
string t = s.substr(0, i), tt = s.substr(i + 1);
for(i = 0; i < 13; i++) if(q[i] == t) break;
ans += i * 13;
for(i = 0; i < 13; i++) if(p[i] == tt) break;
ans += i;
}else{
for(i = 0; i < 13; i++) if(q[i] == s){ ans = i * 13; break; }
for(i = 0; i < 13; i++) if(p[i] == s){ ans = i; break; }
}
cout << ans << endl;
}
s = "";
}
return 0;
}
5、1045 快速排序
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 2e7 + 5;
const int MaxN = 1e6;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T, tot;
int l, r, ans[maxx + 5];
int a[maxx + 5], b[maxx + 5];
int main(){
int i, j, k;
while( scanf("%d", &n) == 1){
for(i = 1; i <= n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
}
int Max = -1;
for(i = 1; i <= n; i++){
if(a[i] < Max) b[i] = 0;
Max = max(a[i], Max);
}
int Min = inf;
for(i = n; i >= 1; i--){
if(a[i] > Min) b[i] = 0;
Min = min(a[i], Min);
}
for(i = 1; i <= n; i++) if(b[i]) ans[tot++] = b[i];
cout << tot << endl;
sort(ans, ans + tot);
if(tot > 0) cout << ans[0];
for(i = 1; i < tot; i++)
printf(" %d", ans[i]);
puts("");
}
return 0;
}
6、1046 划拳
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int MaxN = 1e6;
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T;
int a, b, c;
int l1, l2, r1, r2;
int main(){
int i, j, k;
while( scanf("%d", &n) == 1){
a = b = 0;
while(n--){
scanf("%d %d %d %d", &l1, &l2, &r1, &r2);
k = l1 + r1;
if(l2 == k && r2 != k) b++;
else if(l2 != k && r2 == k) a++;
}
printf("%d %d\n", a, b);
}
return 0;
}
7、1047 编程团体赛
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int MaxN = 1e6;
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T;
map<int, int> p;
int main(){
int i, j, k;
while( scanf("%d", &T) == 1){
int idx, Max = -1;
while( T-- ){
scanf("%d-%d %d", &idx, &k, &m);
p[idx] += m; Max = max(Max, p[idx]);
}
for(map<int, int>::reverse_iterator it = p.rbegin(); it != p.rend(); it++){
if(Max == it -> se){ printf("%d %d\n", it -> fi, it -> se); break; }
}
}
return 0;
}
8、1048 数字加密
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int MaxN = 1e6;
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T;
string s, t, p;
int l, r, ans;
int main(){
int i, j, k;
while( cin >> s >> t){
int lens = s.size(), lent = t.size();
reverse(s.begin(), s.end());
reverse(t.begin(), t.end());
for(i = 0; i < max(lens, lent); i++){
if(i < lens){
l = s[i] - '0';
}else l = 0;
if(i < lent){
r = t[i] - '0';
}else r = 0;
if(i & 1) p += (r - l + 10) % 10 + '0';
else{
k = (l + r) % 13;
if(k == 10) p += "J";
else if(k == 11) p += "Q";
else if(k == 12) p += "K";
else p += '0' + k;
}
}
reverse(p.begin(), p.end());
cout << p << endl;
}
return 0;
}
9、1049 数列的片段和
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<bitset>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T;
long double ans, k;
int main(){
while(cin >> n){
for(ll i = 1; i <= n; i++){
cin >> k;
ans += (i - 1) * (n - i + 1) * k + (n - i + 1) * k;
}
printf("%.2llf\n", ans);
}
return 0;
}
10、1050 螺旋矩阵
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<bitset>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#define ll long long
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define esp 1e-5
#define lowbit(x) x & (-x)
using namespace std;
const int maxx = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, T, N;
int a[10005], ans[5005][5005];
bool vis[5005][5005];
int x, y, cnt, tot;
bool Is_prime(int x){
if(x == 1) return false;
for(int i = 2; i <= sqrt(x); i++)
if(x % i == 0) return false;
return true;
}
int main(){
int i, j, k;
while(cin >> N){
for(i = 0; i < N; i++) cin >> a[i];
sort(a, a + N, greater<int>());
if(Is_prime(N)){
for(i = 0; i < N; i++) printf("%d\n", a[i]);
continue;
}
for(i = 1; i <= N; i++){
if(N % i == 0 && i >= N / i){
n = i; m = N / i; break;
}
}
x = 0; y = -1;
while(tot < N){
while(y + 1 < m && !vis[x][y + 1]){ ans[x][y + 1] = a[tot++]; vis[x][y + 1] = true; y++; }
while(x + 1 < n && !vis[x + 1][y]){ ans[x + 1][y] = a[tot++]; vis[x + 1][y] = true; x++; }
while(y - 1 >= 0 && !vis[x][y - 1]){ ans[x][y - 1] = a[tot++]; vis[x][y - 1] = true; y--; }
while(x - 1 >= 0 && !vis[x - 1][y]){ ans[x - 1][y] = a[tot++]; vis[x - 1][y] = true; x--; }
}
for(i = 0; i < n; i++){
for(j = 0; j < m; j++) printf("%d%c", ans[i][j], j == m - 1 ? '\n' : ' ');
}
}
return 0;
}
2021/2/13完结(春节和同学一起出去耍了,计划就搁置了,回来慢慢补吧!争取上班之前把这套题刷完!LeetCode还得接着刷)。