日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

PAT甲级题目翻译+答案 AcWing(模拟)

發(fā)布時(shí)間:2025/3/19 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级题目翻译+答案 AcWing(模拟) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1008 Elevator (20 分)

  • 思路 :last可能等于cur,而無(wú)論是否相等,res都是+5的
#include <iostream>using namespace std;int main() {int n;cin >> n;int res = 0, last = 0;while (n -- ){int cur;cin >> cur;if (cur > last) res += 6 * (cur - last);else res += 4 * (last - cur);res += 5;last = cur;}cout << res;return 0; }

1011 World Cup Betting (20 分)

  • 思路 :三次,每次將輸入的三個(gè)數(shù)中最大的的對(duì)應(yīng)字母輸出,最后再乘上最大的數(shù)
#include <iostream>using namespace std;int main() {double res = 1;double w, t, l;for (int i = 0; i < 3; i ++ ){cin >> w >> t >> l;double x = max(w, max(t, l));if (w == x) cout << "W ";else if (t == x) cout << "T ";else cout << "L ";res *= x;}printf("%.2lf", (res * 0.65 - 1) * 2);return 0; }

1014 Waiting in Line (30 分)

思路 :

  • 對(duì)于每個(gè)窗口,sum[i]表示第i個(gè)窗口當(dāng)前累積到多少分鐘,也就是下一個(gè)人的業(yè)務(wù)受理時(shí)間。隊(duì)列q[i]表示當(dāng)前這個(gè)隊(duì)伍的排隊(duì)情況,通過front和pop和push維護(hù)這個(gè)隊(duì)列
  • 對(duì)于每個(gè)人,如果是nm個(gè)人之前的,挑當(dāng)前排隊(duì)人數(shù)最少的窗口進(jìn)去,然后維護(hù)窗口數(shù)組和隊(duì)列;如果是nm個(gè)人之后,看每個(gè)隊(duì)伍最前面的人所在的時(shí)間,挑最早的,然后維護(hù)隊(duì)列
  • 最后如果業(yè)務(wù)受理時(shí)間合理,就將業(yè)務(wù)結(jié)束時(shí)間存入哈希表
  • 最后輸出時(shí)間的時(shí)候,小時(shí)注意記得加上8!
#include <iostream> #include <unordered_map> #include <queue> using namespace std;const int N = 25;int n, m, k, Q; queue<int> q[N]; int sum[N]; unordered_map<int, int> ma;int main() {scanf("%d%d%d%d", &n, &m, &k, &Q);for (int i = 1, s; i <= k && scanf("%d", &s); i ++ ){int t = 0;for (int j = 0; j < n; j ++ ){if (i <= n * m){if (q[t].size() > q[j].size()) t = j;}else{if (q[t].front() > q[j].front()) t = j;}}sum[t] += s;if (i > n * m) q[t].pop();q[t].push(sum[t]);if (sum[t] - s < 540) ma[i] = sum[t];}int id;while (Q -- ){scanf("%d", &id);if (ma.count(id)) printf("%02d:%02d\n", ma[id] / 60 + 8, ma[id] % 60);else printf("Sorry\n");} }

1031 Hello World for U (20 分)

  • 題意 :輸出U型字符串,滿足n1==n3,n1+n2+n3?2==nn_1==n_3,n_1+n_2+n_3-2==nn1?==n3?,n1?+n2?+n3??2==n,且n1n_1n1?n2n_2n2?盡可能近
  • 思路 :根據(jù)等式,一個(gè)n1n_1n1?一定對(duì)應(yīng)n2和n3n_2和n_3n2?n3?,所以直接令n1=(n+2)/3n_1=(n+2)/3n1?=(n+2)/3就是盡可能近的結(jié)果,那么得到了n1,n2,n3n_1,n_2,n_3n1?,n2?,n3?;剩下的就是在一個(gè)矩陣中填入字符了,分為三個(gè)部分,注意第三個(gè)部分是從下往上填的;輸出矩陣時(shí),如果g[i][j]==0g[i][j] ==0g[i][j]==0,說明沒有在這個(gè)位置填過(注意不是字符0),就輸出空格。
  • 語(yǔ)法 :字符矩陣中沒有被填過的部分如何表示。
#include <iostream>using namespace std;char g[85][85];int main() {string str;cin >> str;int n = str.size();int n1 = (n + 2) / 3;int n3 = n1, n2 = n - n1 - n3 + 2;int k = 0;for (int i = 0; i < n1; i ++ ) g[i][0] = str[k ++ ];for (int i = 1; i <= n2 - 2; i ++ ) g[n1 - 1][i] = str[k ++ ];for (int i = n1 - 1; i >= 0; i -- ) g[i][n2 - 1] = str[k ++ ];for (int i = 0; i < n1; i ++ ){for (int j = 0; j < n2; j ++ )if (g[i][j]) cout << g[i][j];else cout << ' ';cout << endl;}return 0; }

1041 Be Unique (20 分)

  • 題意 :找到輸入的數(shù)中第一個(gè)只出現(xiàn)過一次的數(shù)
  • 思路 :先用一維數(shù)組存下按順序輸入的數(shù),再套一個(gè)一維數(shù)組存數(shù)對(duì)應(yīng)出現(xiàn)的次數(shù),那么遍歷第一個(gè)數(shù)組首先找到次數(shù)為1的數(shù)就可以直接break了
#include <iostream>using namespace std;const int N = 1e5 + 10;int a[N], c[N];int main() {int n;cin >> n;for (int i = 0; i < n; i ++ ){cin >> a[i];c[a[i]] ++ ;}for (int i = 0; i < n; i ++ )if (c[a[i]] == 1){cout << a[i];return 0;}puts("None");return 0; }

1042 Shuffling Machine (20 分)

  • 思路 :牌號(hào)的規(guī)律,先不管字母,就是1-54號(hào)的牌,然后再根據(jù)函數(shù)對(duì)應(yīng)打印;最后結(jié)果要按照位序打印出對(duì)應(yīng)位序上的數(shù),所以想到用一個(gè)一維數(shù)組記錄當(dāng)前位序i對(duì)應(yīng)的數(shù),因?yàn)橐?jīng)過k次洗牌,且不能覆蓋當(dāng)前的,想到再開一個(gè)一維數(shù)組記錄上一次位序i對(duì)應(yīng)的數(shù)。
  • 語(yǔ)法 :memcpy函數(shù)在cstring頭文件中
#include <iostream> #include <cstring>using namespace std;const int N = 60;int p[N], q[N], w[N];void print(int x) {if (x <= 13) cout << 'S' << x;else if (x <= 26) cout << 'H' << x - 13;else if (x <= 39) cout << 'C' << x - 26;else if (x <= 52) cout << 'D' << x - 39;else cout << 'J' << x - 52; }int main() {int k;cin >> k;for (int i = 1; i <= 54; i ++ ) cin >> q[i];for (int i = 1; i <= 54; i ++ ) p[i] = i;while (k -- ){memcpy(w, p, sizeof p);for (int i = 1; i <= 54; i ++ ) p[q[i]] = w[i];}for (int i = 1; i <= 54; i ++ ){print(p[i]);if (i != 54) cout << ' ';} }

1047 Student List for Course (25 分)

題意 :

  • 給出每個(gè)學(xué)生對(duì)應(yīng)參加的所有課程,求輸出每門課程分別有哪些學(xué)生參與

思路 :

  • 由于說了課程號(hào)是1-k,因此直接用裝string的vector即可,不需要map
#include <iostream> #include <vector> #include <algorithm> using namespace std;#define pb push_backconst int N = 2510;int n, k; vector<string> lesson[N];int main() {scanf("%d%d", &n, &k);char str[5];int cnt, l;while (n -- ){scanf("%s %d", str, &cnt);while (cnt -- ){scanf("%d", &l);lesson[l].pb(str);}}for (int i = 1; i <= k; i ++ ){printf("%d %d\n", i, lesson[i].size());sort(lesson[i].begin(), lesson[i].end());for (auto& l : lesson[i])printf("%s\n", l.c_str());} }

1054 The Dominant Color (20 分)

  • 題意 :找到n * m的矩陣中出現(xiàn)次數(shù)超過n * m / 2的并輸出,直接模擬即可。
#include <iostream> #include <unordered_map>using namespace std;int main() {int n, m;cin >> m >> n;unordered_map<int, int> cnt;for (int i = 0; i < n; i ++ )for (int j = 0; j < m; j ++ ){int x;cin >> x;if ( ++ cnt[x] > n * m / 2){cout << x;break;}}return 0; }

1056 Mice and Rice (25 分)

題意 :

  • 初始一共np個(gè)老鼠,給出這np個(gè)老鼠的大小以及參加比賽的順序,每輪在剩下的數(shù)中以每ng個(gè)為一組,每組中選最大的為勝者,所有敗者排名都一樣,每輪中所有勝者進(jìn)入下一輪繼續(xù)循環(huán)

思路 :

  • 模擬即可
  • 每一輪中輸?shù)睦鲜蟮呐琶扔谮A的老鼠的數(shù)量+1,贏的老鼠的數(shù)量ramin=cur.size()+m?1mramin=\frac{cur.size()+m-1}{m}ramin=mcur.size()+m?1?,也就是向上取整
  • 模擬直到只剩下一個(gè)(不是不剩)為止;最后出來了要對(duì)第一名賦值
  • 之所以cur用vector是因?yàn)槊枯喍荚跍p小大小,因此用不定長(zhǎng)即vector

語(yǔ)法 :

  • for (int i = 0; i < cur.size();)
  • int j = min((int)cur.size(), i + m);
  • 以上兩個(gè)搭配來枚舉一些定長(zhǎng)區(qū)間+最后一個(gè)區(qū)間
#include <iostream> #include <vector> using namespace std;const int N = 1010;int n, m; int w[N], Rank[N];int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n && scanf("%d", &w[i]); i ++ );vector<int> cur(n);for (int i = 0; i < n && scanf("%d", &cur[i]); i ++ );while(cur.size() > 1){vector<int> next;int remain = ((int)cur.size() + m - 1) / m;for (int i = 0; i < cur.size(); ){int j = min((int)cur.size() - 1, i + m - 1);int t = -1;for (int k = i; k <= j; k ++ )if (t == -1 || w[cur[t]] < w[cur[k]])t = k;next.push_back(cur[t]);for (int k = i; k <= j; k ++ )if (k != t)Rank[cur[k]] = remain + 1;i = j + 1;}cur = next;}Rank[cur[0]] = 1;for (int i = 0; i < n; i ++ ) cout << Rank[i] << " \n"[i == n - 1]; }

1062 Talent and Virtue (25 分)

題意 :

  • 給每個(gè)人的id和moral值和talent值,根據(jù)后兩者數(shù)據(jù)決定它們的level,然后根據(jù)每個(gè)level內(nèi)部排序,分別輸出

思路 :

  • 不需要開四個(gè)容器,直接用結(jié)構(gòu)題內(nèi)的level屬性來sort隔斷即可

語(yǔ)法 :

  • int total() const {return moral + talent;}
  • total() != t.total()
#include <iostream> #include <algorithm> using namespace std;const int N = 1e5 + 10;struct Person {int id, moral, talent;int level;int total() const{return moral + talent;}bool operator< (const Person& t) const{if (level != t.level) return level < t.level;if (total() != t.total()) return total() > t.total();if (moral != t.moral) return moral > t.moral;return id < t.id;} }p[N];int main() {int n, l, h, m = 0;scanf("%d%d%d", &n, &l, &h);int id, moral, talent, level;for (int i = 0; i < n; i ++ ){scanf("%d %d %d", &id, &moral, &talent);if (moral < l || talent < l) continue;if (moral >= h && talent >= h) level = 1;else if (moral >= h && talent < h) level = 2;else if (moral < h && talent < h && talent <= moral) level = 3;else level = 4;p[m ++ ] = {id, moral, talent, level};}sort(p, p + m);printf("%d\n", m);for (int i = 0; i < m; i ++ )printf("%08d %d %d\n", p[i].id, p[i].moral, p[i].talent); }

1065 A+B and C (64bit) (20 分)

  • 思路 :考察C++中整數(shù)在計(jì)算機(jī)中表示的方法;數(shù)據(jù)范圍是所有的long long范圍,所以ab相加可能會(huì)溢出,而溢出情況只可能有兩種,一正一負(fù)不可能溢出,不溢出直接運(yùn)算就可以;如果是第一種情況的溢出,由于C是范圍內(nèi)的數(shù),所以一定大于c,第二種情況則反之
  • 最高位是0是非負(fù)數(shù),是1是負(fù)數(shù);表示負(fù)數(shù)就是正數(shù)所有位取反再加一(~x+1);大于等于0的范圍是0到263?10到2^{63}-10263?1,負(fù)數(shù)是?263到?1-2^{63}到-1?263?1;所以兩個(gè)正數(shù)相加溢出的范圍是263到264?22^{63}到2^{64}-2263264?2,那么最高位一定是1,所以兩個(gè)正數(shù)相加溢出一定變成負(fù)數(shù)。
  • 語(yǔ)法 :long long 對(duì)應(yīng)%lld
#include <iostream>using namespace std;typedef long long LL;bool check(LL a, LL b, LL c) {LL d = a + b;if (a >= 0 && b >= 0 && d < 0) return true;if (a < 0 && b < 0 && d >= 0) return false;return a + b > c; }int main() {int n;cin >> n;for (int i = 1; i <= n; i ++ ){LL a, b, c;scanf("%lld%lld%lld", &a, &b, &c);if (check(a, b, c)) printf("Case #%d: true\n", i);else printf("Case #%d: false\n", i);}return 0; }

1069 The Black Hole of Numbers (20 分)

  • 題意 :直到得到結(jié)果是0或者6174才停下,這個(gè)時(shí)候的式子也是要輸出的;注意這道題分別獲得兩個(gè)數(shù)的時(shí)候無(wú)論原來的數(shù)是幾位,后來都是獲得兩個(gè)四位數(shù),也就是四次*10
  • 語(yǔ)法 :sort默認(rèn)從小到大;函數(shù)可以返回vector容器;sort在algorithm頭文件中;reversereversereverse在algorithm頭文件中;dowhile。
#include <iostream> #include <vector> #include <algorithm>using namespace std;vector<int> get(int n) {int nums[4];for (int i = 0; i < 4; i ++ ){nums[i] = n % 10;n /= 10;}sort(nums, nums + 4);int a = 0;for (int i = 0; i < 4; i ++ ) a = a * 10 + nums[i];reverse(nums, nums + 4);int b = 0;for (int i = 0; i < 4; i ++ ) b = b * 10 + nums[i];return {b, a}; }int main() {int n;cin >> n;do{auto t = get(n);printf("%04d - %04d = %04d\n", t[0], t[1], t[0] - t[1]);n = t[0] - t[1];} while (n && n != 6174);return 0; }

1070 Mooncake (25 分)

題意 :

  • 給每個(gè)月餅的總共數(shù)量以及這些數(shù)量總共對(duì)應(yīng)的價(jià)格,和總需求量,求最多賣多少錢

思路 :

  • 按照單價(jià)排序,在m>0的同時(shí)遍歷,每次減去能減去的最多需求量,并累積價(jià)格
#include <iostream> #include <algorithm> using namespace std;const int N = 1010;int n; double m; struct Cake {double p, w;bool operator< (const Cake &t) const{return p / w > t.p / t.w;} }c[N];int main() {scanf("%d %lf", &n, &m);for (int i = 0; i < n && scanf("%lf", &c[i].w); i ++ );for (int i = 0; i < n && scanf("%lf", &c[i].p); i ++ );sort(c, c + n);double res = 0;for (int i = 0; i < n && m > 0 ; i ++ ){double r = min(m, c[i].w);m -= r;res += r * c[i].p / c[i].w;}printf("%.2lf\n", res); }

1071 Speech Patterns (25 分)

題意 :

  • alphanumerical文數(shù)字的,one character from the set [0-9 A-Z a-z].;case insensitive不區(qū)分大小寫;lexicographically字典序
  • 給一含空格字符串,“單詞”定義為由連續(xù)的數(shù)字或大小寫字符組成的連續(xù)子串,求字符串中出現(xiàn)次數(shù)最多的單詞,注意輸出時(shí)必須以小寫輸出,如果一樣多,輸出字典序最小的

語(yǔ)法 :

  • 大寫字母+32就會(huì)變成相應(yīng)的小寫字母
#include <iostream> #include <unordered_map> using namespace std;bool check(char c) {if (c >= '0' && c <= '9') return true;if (c >= 'a' && c <= 'z') return true;return false; }int main() {string s;getline(cin, s);for (int i = 0; i < s.size(); i ++ ) s[i] = tolower(s[i]);unordered_map<string, int> hash;for (int i = 0; i < s.size(); i ++ ){if (check(s[i])){string word;int j = i;while (j < s.size() && check(s[j])) word += s[j ++ ];hash[word] ++ ;i = j;}}string word;int cnt = -1;for (auto item : hash){if (cnt < item.second || (cnt == item.second && word > item.first)){cnt = item.second;word = item.first;}}printf("%s %d", word.c_str(), cnt); }

1092 To Buy or Not to Buy (20 分)

  • 思路 :多多少和少多少相互之間是沒有影響的,如果少多少是0才輸出多多少
  • 語(yǔ)法 :unordered_map的遍歷
#include <iostream> #include <unordered_map>using namespace std;int main() {string a, b;cin >> a >> b;unordered_map<char, int> S;for (auto c : a) S[c] ++ ;for (auto c : b) S[c] -- ;int sp = 0, sn = 0;for (auto item : S)if (item.second > 0) sp += item.second;else sn -= item.second;if (sn) printf("No %d", sn);else printf("Yes %d", sp);return 0; }

1105 Spiral Matrix (25 分)

題意 :

  • 給定一個(gè)包含 N 個(gè)正整數(shù)的序列,請(qǐng)你將序列中的元素以非遞增順序填充到螺旋矩陣中。
  • 從左上角的第一個(gè)元素開始填充,并按照順時(shí)針方向旋轉(zhuǎn)。
  • 要求矩陣有 m 行 n 列,并且 m,n 滿足:m×n=N ,m≥n,m?n 盡可能小

思路 :

  • 首先如何找到m和n呢?顯然m和n都是N的約數(shù),且都小于等于根號(hào)N;因此,直接遍歷約數(shù)即可,從1開始到根號(hào),最后遍歷到的約數(shù)就是答案
  • 這里用vector矩陣
  • 如何填充這個(gè)矩陣?四個(gè)變量,i是序列下標(biāo),x和y是矩陣下標(biāo),先向右填然后向下然后向左然后向上為一個(gè)循環(huán)周期,每個(gè)周期碰到邊界或者已經(jīng)填充過,說明換方向了,d是方向變量,記得要%

語(yǔ)法 :

  • vector<vector<int>> res(r, vector<int>(c));表示r行c列的矩陣
  • sort(w, w + n, greater<int>());表示從大到小排序
#include <iostream> #include <vector> #include <algorithm> using namespace std;const int N = 1e4 + 10;int w[N];int main() {int n;scanf("%d", &n);for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);sort(w + 1, w + n + 1, greater<int>());int r, c;for (int i = 1; i * i <= n; i ++ )if (n % i == 0){r = n / i;c = i;}vector<vector<int>> res(r, vector<int>(c));int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};for (int i = 1, x = 0, y = 0, d = 0; i <= n; i ++ ){res[x][y] = w[i];int a = x + dx[d], b = y + dy[d];if (a < 0 || a >= r || b < 0 || b >= c || res[a][b]){d = (d + 1) % 4;a = x + dx[d], b = y + dy[d];}x = a, y = b;}for (int i = 0; i < r; i ++ ){for (int j = 0; j < c; j ++ ){printf("%d", res[i][j]);if (j != c - 1) printf(" ");}if (i != r - 1) puts("");} }

1109 Group Photo (25 分)

題意 :

  • the division result must be rounded down to the nearest integer意思是向下取整,不是四舍五入
  • 合影時(shí)隊(duì)形非常重要,給出 N 個(gè)人排成 K 行的規(guī)則,如下所示:
  • 每行的人數(shù)必須為 N/K(向下取整),所有多余的人(如果有)都放到最后一行;
  • 位于后排的所有人都不得矮于位于前排的任何人;
  • 在每一行中,最高的人站在該行的中心位置(定義為位置 (m/2+1),位置從1開始編號(hào),其中 m 是該行的總?cè)藬?shù),除法結(jié)果向下取整);
  • 在每一行中,其他人必須按照其身高非遞增順序依次排入該行,交替地將他們的位置先安排到最高人的右側(cè),然后再安排到最高人的左側(cè)(例如,假設(shè)五個(gè)人的身高為 190、188,186、175、170,最終陣型將是 175、188、190、186、170。在這里,我們假設(shè)你面對(duì)著合影人員,因此你的左手邊位置其實(shí)是最高人的右手邊位置。);
  • 當(dāng)許多人的身高相同時(shí),必須按姓名的字典序升序進(jìn)行排序,保證所有人的姓名不重復(fù)。

思路 :

  • 首先確認(rèn)每行的人數(shù),如題意,前面每行都是n / m人,最后一行要特判!還要加上n % m
  • 從中間開始往兩邊遞減,使用雙指針,只要兩個(gè)指針中有一個(gè)還合法就繼續(xù)循環(huán),在循環(huán)中特判;再配合一個(gè)最外層循環(huán)指針j,表示使用人數(shù)j
#include <iostream> #include <cstring> #include <algorithm>using namespace std;const int N = 10010;int n, m; struct Person {string name;int h;bool operator< (const Person &t) const{if (h != t.h) return h > t.h;return name < t.name;} }p[N]; string line[N];int main() {cin >> n >> m;for (int i = 0; i < n; i ++ ) cin >> p[i].name >> p[i].h;sort(p, p + n);for (int i = 0, j = 0; i < m; i ++ ){int len = n / m;if (!i) len += n % m; // 特判最后一排for (int r = len / 2 + 1, l = r - 1; l > 0 || r <= len; l --, r ++ ){if (r <= len) line[r] = p[j ++ ].name;if (l > 0) line[l] = p[j ++ ].name;}cout << line[1];for (int k = 2; k <= len; k ++ ) cout << ' ' << line[k];cout << endl;}return 0; }

1121 Damn Single (25 分)

思路 :

  • 查詢的m個(gè)id中,如果其中包含輸入的某一對(duì)關(guān)系,則將這兩個(gè)一起從m個(gè)id中刪除

語(yǔ)法 :

  • set的count
  • set的erase表示刪除這個(gè)元素
#include <iostream> #include <unordered_set> #include <algorithm> using namespace std;const int N = 5e4 + 10;struct Couple {int a, b; }c[N];int ans[N], k;int main() {int n, m;scanf("%d", &n);for (int i = 0; i < n; i ++ ) scanf("%d%d", &c[i].a, &c[i].b);unordered_set<int> se;scanf("%d", &m);for (int i = 0, id; i < m && scanf("%d", &id); i ++ ){se.insert(id);}for (int i = 0; i < n; i ++ ){int a = c[i].a, b = c[i].b;if (se.count(a) && se.count(b)){se.erase(a), se.erase(b);}}for (auto s : se) ans[k ++ ] = s;sort(ans, ans + k);printf("%d\n", k);if (k){printf("%05d", ans[0]);for (int i = 1; i < k; i ++ ) printf(" %05d", ans[i]);} }

1128 N Queens Puzzle (20 分)

  • 題意 :diagonal對(duì)角線
  • 語(yǔ)法 :一組樣例多組輸入的題目不要隨意break!!利用數(shù)組的坐標(biāo)系去看的話,也就是->y,|V為x,得知反對(duì)角線是y-x是個(gè)定值(y=x+b,與尋常坐標(biāo)系反),而正對(duì)角線是y+x是個(gè)定值
#include <iostream> #include <cstring>using namespace std;const int N = 1e3 + 10;bool row[N], dg[N * 2], udg[N * 2];int main() {int T;cin >> T;while (T -- ){memset(row, 0, sizeof row);memset(dg, 0, sizeof dg);memset(udg, 0, sizeof udg);int n;cin >> n;bool success = true;for (int y = 1; y <= n; y ++ ){int x;cin >> x;if (row[x] || dg[y + x] || udg[y - x + n]) success = false;row[x] = dg[y + x] = udg[y - x + n] = true;}if (success) puts("YES");else puts("NO");}return 0; }

1129 Recommendation System (25 分)

思路 :

  • 每次輸出當(dāng)前最大的m個(gè)數(shù),如果不足m個(gè)數(shù),不需要輸出m個(gè)數(shù),因此,與當(dāng)前動(dòng)態(tài)維護(hù)的數(shù)組的大小取min即可
  • 動(dòng)態(tài)維護(hù)一個(gè)最多長(zhǎng)為m的數(shù)組,另外一個(gè)數(shù)組表示i出現(xiàn)的次數(shù),因此,可以在第一個(gè)數(shù)組中根據(jù)第二個(gè)數(shù)組排序
  • 最多長(zhǎng)為m,因此,如果當(dāng)前這個(gè)數(shù)不在窗口中,先插入,如果在就不需要插入。然后再排序,選出最大的m個(gè)
#include <iostream> #include <algorithm> using namespace std;const int N = 5e5 + 10;int n, m; int cnt[N]; int top_k[N], k;int main() {scanf("%d%d", &n, &m);int id;for (int i = 0; i < n; i ++ ){scanf("%d", &id);if (i){printf("%d:", id);for (int j = 0; j < k; j ++ ) printf(" %d", top_k[j]);if (i != n - 1) puts("");}cnt[id] ++ ;bool exists = false;for (int j = 0; j < k; j ++ )if (top_k[j] == id){exists = true;break;}if (!exists) top_k[k ++ ] = id;sort(top_k, top_k + k, [](int x, int y){if (cnt[x] != cnt[y]) return cnt[x] > cnt[y];return x < y;});k = min(k, m);} }

1132 Cut Integer (20 分)

  • 語(yǔ)法 :當(dāng)將任何變量放在“/‘后面或者”%“后面都要注意它是否為0
#include <iostream>using namespace std;int main() {int T;cin >> T;while (T -- ){string s;cin >> s;int len = s.size() / 2;int left = stoi(s.substr(0, len));int right = stoi(s.substr(len, len));int n = stoi(s);if (left * right && n % (left * right) == 0) puts("Yes");else puts("No");}return 0; }

1140 Look-and-say Sequence (20 分)

  • 思路 :問的是序列中第n個(gè)數(shù),所以循環(huán)的是n-1次;找字符串中連續(xù)的次數(shù)的方法
#include <iostream>using namespace std;int main() {int d, n;cin >> d >> n;string cur = to_string(d);for (int k = 0; k < n - 1; k ++ ){string next;for (int i = 0; i < cur.size();){int j = i + 1;while (j < cur.size() && cur[i] == cur[j]) j ++ ;next += cur[i] + to_string(j - i);i = j;}cur = next;}cout << cur;return 0; } 與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的PAT甲级题目翻译+答案 AcWing(模拟)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。