首先讲解一下PTA乙级在哪里。PTA乙级题其实就是PAT (Basic Level) Practice (中文)
1001 害死人不偿命的(3n+1)猜想
#include<iostream>
using namespace std;int main()
{int cnt = 0;int n;cin >> n;while(n != 1){cnt ++;if (n & 1){n = (3 * n + 1) / 2;}else n /= 2;}cout << cnt;return 0;
}
1002 写出这个数
#include <iostream>
#include <string>
#include <stack>
using namespace std;string z[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int main()
{string s;cin >> s;int sum = 0;for (int i = 0; i < s.size(); i ++ ){sum += s[i] - '0';}stack<string> S;while (sum){int t = sum % 10;S.push(z[t]);sum /= 10;}while (S.size()){if(S.size() == 1) cout << S.top();else cout << S.top() << ' ';S.pop();}return 0;
}
1003 我要通过!
1.形如xPATx,注意两边都为相同的串
2.如果 aPbTc 是正确的,那么 aPbATca 也是正确的。也就是每次在尾部加a串,但初始PAT两边串相同,故最后c长度一定为a长度的倍数。
ssn:太久不写代码了,竟然连语法题也会卡壳hhh
#include <iostream>
#include <string>
using namespace std;void solve(){string s;cin >> s;int i;int cnt1 = 0, cnt2 = 0, cnt3 = 0;for (i = 0; i < s.size(); i ++ ){if (s[i] == 'A'){cnt1 ++;continue;}else break;}if (s[i] == 'P') i ++;else{cout << "NO" << endl;return;}if (s[i] != 'A'){cout << "NO" << endl;return;}for (; i < s.size(); i ++ ){if (s[i] == 'A'){cnt2 ++;continue;}else break; }if (s[i] == 'T') i ++;else{cout << "NO" << endl;return;}for (; i < s.size(); i ++ ){if (s[i] == 'A'){cnt3 ++;continue;}else {cout << "NO" << endl;return;} }if (cnt3 == 0) cout << "YES" << endl;else if (cnt2 > cnt3 || cnt3 % cnt1) cout << "NO" << endl;else cout << "YES" << endl;
}int main(){int t;cin >> t;while(t --) solve();return 0;
}
1004 成绩排名
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;typedef struct student{string name;string id;int score;
}stu;bool cmp(stu a, stu b){return a.score > b.score;
}int main(){int n;cin >> n;stu t[n];for (int i = 0; i < n; i ++ ){cin >> t[i].name >> t[i].id >> t[i].score;}sort(t, t + n, cmp);cout << t[0].name << ' ' << t[0].id << endl;cout << t[n - 1].name << ' ' << t[n - 1].id;return 0;
}
1005 继续(3n+1)猜想
#include <iostream>
#include <queue>
using namespace std;bool st[10000];int main(){int n;cin >> n;int a[n];for (int i = 0; i < n; i ++ ){cin >> a[i];if (st[a[i]]) continue;int t = a[i];while (t != 1){if (t & 1){t = (t * 3 + 1) / 2;st[t] = true;}else{t /= 2;st[t] = true;}}} priority_queue<int> q;for (int i = 0; i < n; i ++ ){if (!st[a[i]]) q.push(a[i]);}while(q.size()){if(q.size() == 1){cout << q.top();q.pop();}else{cout << q.top() << ' ';q.pop();}}return 0;
}