PAT乙级题目题解

时间:2021-07-13 18:47:23

1003. 我要通过!(20)

思路:

  仔细读题....其实代码很简单

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 bool judge(char s[])
10 {
11 int len = strlen(s);
12 int cnt_p = 0, cnt_a = 0, cnt_t = 0;
13 for(int i = 0; i < len; i++)
14 {
15 cnt_p += s[i] == 'P';
16 cnt_a += s[i] == 'A';
17 cnt_t += s[i] == 'T';
18 }
19
20 if(cnt_p != 1 || cnt_t != 1 || !cnt_a || cnt_p + cnt_a + cnt_t != len) return false;
21
22 int x = strchr(s, 'P') - s;
23 int y = strchr(s, 'T') - strchr(s, 'P') - 1;
24 int z = len - 1 - (strchr(s, 'T') - s);
25 return x * y == z;
26 }
27
28 int main()
29 {
30 int n;
31 char s[105];
32 scanf("%d", &n);
33 while(n--)
34 {
35 scanf("%s", s);
36 if(judge(s)) puts("YES");
37 else puts("NO");
38
39 }
40 return 0;
41 }

 

1005. 继续(3n+1)猜想 (25)

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 int a[105];
10
11 int main()
12 {
13 set<int>s;
14 int n;
15 scanf("%d", &n);
16 for(int i = 0; i < n; i++)
17 {
18 scanf("%d", &a[i]);
19 s.insert(a[i]);
20 }
21 for(auto o : s)
22 {
23 while(o != 1)
24 {
25 if(o & 1) o = (3 * o + 1) / 2;
26 else o /= 2;
27 if(s.find(o) != s.end()) s.erase(o);
28 }
29 }
30
31 set<int>::reverse_iterator it = s.rbegin(); cout << *it++;
32 while(it != s.rend()) cout << " " << *it++;
33
34 puts("");
35 return 0;
36 }

 

1007. 素数对猜想 (20)

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 int prime[9600], check[100000 + 5];
10 int sieve(LL n)
11 {//O(n)复杂度的线性筛。 接受不了的可以先看埃氏筛法
12 memset(check, false, sizeof(check));
13 check[0] = check[1] = true;
14 int tot = 0;
15 for(LL i = 2; i <= n; i++)
16 {
17 if(!check[i]) prime[tot++] = i;
18 for(LL j = 0; j < tot && i * prime[j] <= n; j++)
19 {
20 check[i * prime[j]] = true;
21 if(i % prime[j] == 0) break;
22 }
23 }
24 return tot;
25 }
26
27
28 int main()
29 {
30 int n;
31 cin >> n;
32 int tot = sieve(n);
33 int ans = 0;
34 for(int i = 1; i < tot; i++)
35 {
36 if(prime[i] - prime[i - 1] == 2) ans++;
37 }
38 cout << ans << endl;
39 return 0;
40 }

 

1008. 数组元素循环右移问题 (20)

思路:

  倍增这个数组,直接输出就好了。另外m可能远大于n,所以先m %= n;

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 int a[205];
10
11 int main()
12 {
13 int n, m;
14 scanf("%d%d", &n, &m);
15 m %= n;//m可能远大于n
16 for(int i = 0; i < n; i++)
17 {
18 scanf("%d", &a[i]);
19 a[i + n] = a[i];
20 }
21
22 for(int i = n - m; i <= n + n - m - 1; i++)
23 {
24 printf("%d%c", a[i], i == (2 * n - m - 1) ? '\n' : ' ');
25 }
26 return 0;
27 }

 

1009. 说反话 (20)

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9
10 int main()
11 {
12 stack<string>sta;
13 string s;
14 while(cin >> s)
15 {
16 sta.push(s);
17 char ch = getchar();
18 if(ch == '\n') break;
19 }
20 while(sta.size())
21 {
22 cout << sta.top();
23 sta.pop();
24 if(sta.size()) cout << " ";
25 else cout << endl;
26 }
27 return 0;
28 }

 

1010. 一元多项式求导 (25)

思路:

  这题坑点比较多。。要考虑b==0  b!=0 b==0且只有一组数之类的情况...比较恶心

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9
10 int main()
11 {
12 char ch;
13 int a, b;
14 bool first = true;
15 while(~scanf("%d %d%c", &a, &b, &ch))
16 {
17 if(b != 0)
18 {
19 if(first)
20 printf("%d %d", a * b, b - 1);
21 else
22 printf(" %d %d", a * b, b - 1);
23 first = false;
24 }
25
26 if(ch == '\n')
27 {
28 if(first && b == 0) puts("0 0");
29 else puts("");
30 break;
31 }
32 }
33 return 0;
34 }

 

1012. 数字分类 (20)

思路:

  纯模拟没啥好说的.... 就是注意判空的情况就好了。

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8 vector<int>vec[6];
9 int main()
10 {
11 int n;
12 cin >> n;
13 for(int i = 0; i < n; i++)
14 {
15 int x;
16 scanf("%d", &x);
17 vec[x % 5].push_back(x);
18 }
19
20 int a1 = 0, ok = 0;
21 for(auto o : vec[0]) if(o % 2 == 0) a1 += o, ok = 1;
22 if(ok) printf("%d ", a1);
23 else printf("N ");
24
25 int a2 = 0;
26 for(int i = 0; i < vec[1].size(); i++)
27 {
28 if(i & 1) a2 -= vec[1][i];
29 else a2 += vec[1][i];
30 }
31 if(vec[1].size()) printf("%d ", a2);
32 else printf("N ");
33
34 int a3 = vec[2].size();
35 if(vec[2].size()) printf("%d ", a3);
36 else printf("N ");
37
38 double a4;
39 if(vec[3].size())
40 {
41 int sum = 0;
42 for(auto o : vec[3]) sum += o;
43 a4 = 1.0 * sum / vec[3].size();
44 printf("%.1f ", a4);
45 }
46 else printf("N ");
47
48
49 if(vec[4].size())
50 {
51 int a5 = *max_element(vec[4].begin(), vec[4].end());
52 printf("%d\n", a5);
53 }
54 else printf("N\n");
55 return 0;
56 }

 

1014. 福尔摩斯的约会 (20)

题目:

  这题很没意思.....就是题目给的字符串不一定满足条件,你要自己控制一下两个字符相等的时候,满不满足时间/星期的要求,会不会越过去,比如星期的时候,字母<='G'之类的orz。

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 10000 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8 string day[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
9 int main()
10 {
11 string a, b, c, d;
12 cin >> a >> b >> c >> d;
13 int len = min(a.length(), b.length());
14 int cnt = 1;
15 for(int i = 0; i < len; i++)
16 {
17 if(a[i] == b[i])
18 {
19 if(cnt == 1)
20 {
21 if(a[i] >= 'A' && a[i] <= 'G') cout << day[a[i] - 'A'] << " ";
22 else continue;
23 }
24 else if(cnt == 2)
25 {
26 int temp = 0;
27 if(a[i] >= 'A' && a[i] <= 'N') temp = a[i] - 'A' + 10;
28 else if(a[i] >= '0' && a[i] <= '9') temp = a[i] - '0';
29 else continue;
30 printf("%02d:", temp);
31 break;
32 }
33 cnt ++;
34 }
35 }
36 len = min(c.length(), d.length());
37 for(int i = 0; i < len; i++)
38 {
39 if(c[i] == d[i] && isalpha(c[i]))
40 {
41 printf("%02d\n", i);
42 break;
43 }
44 }
45 return 0;
46 }

 

1015. 德才论 (25)

思路:

  用一个level来记录分类,会方便一些。

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 1e5 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 struct node
10 {
11 int num, de, cai, sum, level;
12 bool operator < (const node &other)const
13 {
14 if(level != other.level)
15 return level < other.level;
16 if(sum != other.sum)
17 return sum > other.sum;
18 if(de != other.de)
19 return de > other.de;
20 if(num != other.num)
21 return num < other.num;
22 }
23 }nodes[maxn];
24
25 int main()
26 {
27 int n, low, high;
28 scanf("%d%d%d", &n, &low, &high);
29 int cnt = 0;
30 for(int i = 0; i < n; i++)
31 {
32 int num, de, cai, level;
33 scanf("%d%d%d", &num, &de, &cai);
34
35 if(de < low || cai < low) level = 5, cnt++;
36 else if(de >= high && cai >= high) level=1;
37 else if(de >= high && cai < high) level=2;
38 else if(de < high && cai < high && de >= cai) level=3;
39 else if(de >= low && cai >= low) level=4;
40
41 nodes[i] = {num, de, cai, de + cai, level};
42 }
43 sort(nodes, nodes + n);
44 printf("%d\n", n - cnt);
45 for(int i = 0; i < n - cnt; i++)
46 printf("%d %d %d\n", nodes[i].num, nodes[i].de, nodes[i].cai);
47
48 return 0;
49 }

 

1017. A除以B (20)

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 1e5 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 int main()
10 {
11 string a;
12 int b;
13 cin >> a >> b;
14 int len = a.length();
15 int temp = a[0] - '0';
16 if(len == 1)
17 {
18 printf("0 %d\n", temp);
19 return 0;
20 }
21
22 for(int i = 1; i < len; i++)
23 {
24 temp = temp * 10 + a[i] - '0';
25 printf("%d", temp / b), temp %= b;
26 }
27 printf(" %d", temp);
28 return 0;
29 }

 

1019. 数字黑洞 (20)

思路:

  做这种题就小心一点就行了,样例读出来0000也能终止,然后看一下输入要求1-10000内正整数,就意味着可能456,23这种输入也有。所以求余要小心点。

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 1e5 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 int main()
10 {
11 int n, d;
12 cin >> n;
13 d = n;
14 do
15 {
16 int cnt = 0, a[5];
17 for(int i = 0; i < 4; i++)
18 {
19 a[i] = d % 10, d /= 10;
20 }
21 sort(a, a + 4);
22 int x = 0, y = 0;
23 for(int i = 0; i < 4; i++) x = x * 10 + a[i];
24 for(int i = 3; i >=0; i--) y = y * 10 + a[i];
25 d = y - x;
26 printf("%04d - %04d = %04d\n", y, x, d);
27 }while(d != 6174 && d != 0);
28 return 0;
29 }

 

1021. 个位数统计 (15)

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 1e5 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 int main()
10 {
11 string s;
12 cin >> s;
13 int len = s.length();
14 map<int, int>ma;
15 for(int i = 0; i < len; i++)
16 {
17 ma[s[i] - '0'] ++;
18 }
19 for(int i = 0; i < 10; i++)
20 {
21 if(ma.count(i) != 0)
22 {
23 printf("%d:%d\n", i, ma[i]);
24 }
25 }
26 return 0;
27 }

 

1022. D进制的A+B (20)

思路:

  坑点:进制转换的时候一定要小心0的转换要特判。

 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int mod = 1e9 + 7;
4 const int maxn = 1e5 + 5;
5 const int INF = 0x3f3f3f3f;
6 typedef long long LL;
7 typedef unsigned long long ull;
8
9 int main()
10 {
11 int a, b, d;
12 cin >> a >> b >> d;
13 a += b;
14 //(a)10 -> (x)d
15 stack<int>s;
16 if(a == 0)
17 {
18 puts("0");
19 return 0;
20 }
21 while(a)
22 {
23 s.push(a % d);
24 a /= d;
25 }
26 while(s.size()) printf("%d", s.top()), s.pop();
27 puts("");
28 return 0;
29 }

 

 

1023. 组个最小数 (20)

 

1030. 完美数列(25)

1039. 到底买不买(20)

1040. 有几个PAT(25)

1043. 输出PATest(20)

1055. 集体照 (25)