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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PAT甲级题目翻译+答案 AcWing(数学)

發布時間:2025/3/19 编程问答 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级题目翻译+答案 AcWing(数学) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1059 Prime Factors (25 分)

題意 :

  • 給一正整數,要求分解質因數

思路 :

  • 使用is_first,來完成除了第一個質因數前都有*的效果
  • 如果n=1,要特判
  • 最后如果n>1,說明還沒有除盡,還有最后一個質因數
#include <iostream> using namespace std;int main() {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int n; cin >> n;cout << n << "=";if (n == 1) cout << n;bool is_first = true;for (int i = 2; i <= n / i; i ++ ){if (n % i == 0){int k = 0;while (n % i == 0) k ++ , n /= i;if (is_first) is_first = false;else cout << '*';cout << i;if (k > 1) cout << "^" << k;}}if (n > 1){if (!is_first) cout << '*';cout << n;} }

1081 Rational Sum (20 分)

  • 題意 :rationnal numbers有理數;numerator/denominator分子/分母;integer part整數部分;fractional part小數部分
  • 題意 :給n個分數,求和,要寫成真分數且約分
  • 思路 :初始化a為0,b為1;輸出時 :如果分母是1,直接輸出整數部分就可以;如果a>b,說明有整數部分和小數部分;pat上分子和分母數據范圍是long int,相當于是int,而acwing是long long,如果沒有中間那步會溢出,因此分母要變成b和d的最小公倍數,找到b和d的最大公約數,那么分母就是b/最大公約數再*d(原先應該是b *d),分子是…(原先是a * d + b * c),但是分子式子的前半部分是由d/t而不是a/t
  • 語法 :gcd模版;scanf時如果是負數這個符號是在分子上讀入的
#include <iostream>using namespace std;typedef long long LL;LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a; }int main() {int n;cin >> n;LL a = 0, b = 1;for (int i = 0; i < n; i ++ ){LL c, d;scanf("%lld/%lld", &c, &d);LL t = gcd(c, d);c /= t, d /= t;t = gcd(b, d);a = d / t * a + b / t * c;b = b / t * d;t = gcd(a, b);a /= t, b /= t;}if (b == 1) cout << a;else{if (a > b) printf("%lld ", a / b), a %= b;printf("%lld/%lld", a, b);}return 0; }

1088 Rational Arithmetic (20 分)

  • 題意 :給兩個分數,分別輸出加減乘除式子,注意分數要轉成真分數,給的可能是假分數
  • 思路 :打印時,先約分,然后判斷分母是否為負,然后判斷分子是否為負決定是否需要輸出兩個半括號,然后如果分母是1一種情況,否則要看是否是假分數,假分數時注意是abs,因為這道題會出現分子為負也可能出現分母為負的情況
#include <iostream>using namespace std;typedef long long LL;LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a; }void print(LL a, LL b) {LL t = gcd(a, b);a /= t, b /= t;if (b < 0) a *= -1, b *= -1;bool is_minus = a < 0;if (is_minus) cout << "(";if (b == 1) cout << a;else{if (abs(a) > b) printf("%lld ", a / b), a = abs(a) % b;printf("%lld/%lld", a , b);}if (is_minus) cout << ")"; }void add(LL a, LL b, LL c, LL d) {print(a, b), cout << " + ", print(c, d), cout << " = ";a = a * d + b * c;b = b * d;print(a, b), cout << endl; }void sub(LL a, LL b, LL c, LL d) {print(a, b), cout << " - ", print(c, d), cout << " = ";a = a * d - b * c;b = b * d;print(a, b), cout << endl; }void mul(LL a, LL b, LL c, LL d) {print(a, b), cout << " * ", print(c, d), cout << " = ";a = a * c;b = b * d;print(a, b), cout << endl; }void div(LL a, LL b, LL c, LL d) {print(a, b), cout << " / ", print(c, d), cout << " = ";if (!c) puts("Inf");else{a = a * d;b = b * c;print(a, b), cout << endl;} }int main() {LL a, b, c, d;scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);add(a, b, c, d);sub(a, b, c, d);mul(a, b, c, d);div(a, b, c, d);return 0; }

1096 Consecutive Factors (20 分)

  • 題意 :給一數,求最長連續因子
  • 思路 :先枚舉起始位置,然后枚舉長度;注意有可能只有它本身的情況
  • 語法 :2312^{31}231還是int范圍;一直循環到連續斷了,就直接退出for循環
#include <iostream> #include <vector>using namespace std;int main() {int n;cin >> n;vector<int> res;for (int i = 2; i * i <= n; i ++ )if (n % i == 0){vector<int> seq;for (int m = n, j = i; m % j == 0; j ++ ){seq.push_back(j);m /= j;}if (seq.size() > res.size()) res = seq;}if (res.empty()) res.push_back(n);cout << res.size() << endl;cout << res[0];for (int i = 1; i < res.size(); i ++ ) cout << "*" << res[i];return 0; }

1103 Integer Factorization (30 分)

題意 :

  • 正整數 N 的 K?P 分解,是將 N 寫為 K 個正整數的 P 次冪的和。
  • 請你編寫一個程序,給定 N,K,P 的情況下,找到 N 的 K?P 分解。
  • 注意,答案也許不唯一。
  • 你需要輸出各因子之和最大的一種解法。
  • 如果仍不能確定唯一解法,則選擇因子序列更大的解法。

思路 :

1104 Sum of Number Segments (20 分)

  • 語法 :i和n-i+1都是10510^5105,相乘有101010^{10}1010,會超過int,所以那個公式寫的時候盡可能把double寫到前面,如果寫到后面會溢出;(精度?)long double輸出時是%Lf
#include <iostream>using namespace std;int main() {int n;cin >> n;long double res = 0;for (int i = 1; i <= n; i ++ ){long double x;cin >> x;res += x * i * (n - i + 1);}printf("%.2Lf", res);return 0; } #include <iostream>using namespace std;typedef long long ll;int main() {int n;cin >> n;ll res = 0;for (int i = 1; i <= n; i ++ ){double x;cin >> x;res += (ll)(1000 * x) * i * (n - i + 1); // 乘1000放大數字,避免精度的影響,注意括號}printf("%.2lf", res / 1000.0); // 加上.0可以轉化成小數return 0; }

1112 Stucked Keyboard (20 分)

  • 題意 :給k和一字符串,字符串中連續出現k次的為壞
  • 思路 :用原字符串判斷某個鍵(數量固定)是否壞;遍歷原字符串,每種字符只有三種情況,沒壞,壞了但沒被輸出過,壞了但被輸出過,所以不用bool,而用int
#include <iostream>using namespace std;const int N = 200;int st[N]; // 1沒壞,0壞了,2輸出過int main() {string s;int k;cin >> k >> s;for (int i = 0; i < s.size(); i ++ ){int j = i + 1;while (j < s.size() && s[j] == s[i]) j ++ ;int len = j - 1 - i + 1;if (len % k) st[s[i]] = 1;i = j - 1; // 因為還要 ++}string res;for (int i = 0; i < s.size(); i ++ ){if (!st[s[i]]) cout << s[i], st[s[i]] = 2;if (st[s[i]] == 1) res += s[i];else{res += s[i];i += k - 1;}}cout << endl << res << endl;return 0; }

1116 Come on! Let’s C (20 分)

  • 題意 :按排名給id(1e4以內的正整數),根據詢問的id判斷是否在排名中出現過和是否被詢問過,然后是排名第一和排名是否質數
  • 思路 :先把10^4以內的質數先標記一下,用篩法求質數,O(nloglogn);先用過Rank[id]再標記為-1;Rank數組有三種狀態,Rank==0代表沒有出現過,Rank是-1代表輸出過了,Rank是正整數代表正常排名且未輸出;st數組有三種狀態,0表示篩法時沒有被篩過(未被染色)在10 ^4以內也就只有1,1表示是質數,2表示非質數
  • 語法 :注意觀察發現輸出時必須%04d,像這種固定位數的編號題;
#include <iostream>using namespace std;const int N = 1e4 + 10;int Rank[N], st[N];void init() {for (int i = 2; i < N; i ++ )if (!st[i]){st[i] = 1;for (int j = i * 2; j < N; j += i)st[j] = 2;} }int main() {init();int n;cin >> n;for (int i = 1; i <= n; i ++ ){int id;cin >> id;Rank[id] = i;}int k;cin >> k;while (k -- ){int id;cin >> id;printf("%04d: ", id);if (!Rank[id]) puts("Are you kidding?");else if (Rank[id] == -1) puts("Checked");else{int t = st[Rank[id]];if (!t) puts("Mystery Award");else if (t == 1) puts("Minion");else puts("Chocolate");Rank[id] = -1;}}return 0; }

1152 Google Recruitment (20 分)

  • 題意 :在給出的一個L(<=1000)位的數中找到第一個K(<10)位的質數
  • 思路 :判斷一個數是否是質數,這個題k最大是9位,就是109?110^9-1109?1,不可能把這之間所有質數都找出來,可以用試除法判斷,既可以用自然數來試,也可以用質數來試,到x1/2x^{1/2}x1/2,如果用自然數來試,最壞情況每次試(109)1/2(10^9)^{1/2}(109)1/2,差不多3e4,最多1000位,所以3e7,有可能會超時,我們優化一下,試除法的時候不用自然數來試而是用質數來試;費馬 :1到n中質數的個數大概是n/(lnn)n/(lnn)n/(lnn),ln(3e4)肯定大于10,也就是可以比自然數效率高10倍以上;所以這道題就是先將1到(109)1/2(10^9)^{1/2}(109)1/2以內的質數先篩出來,
  • 語法 :這里是先篩質數而不是判斷質數,所以1這個特殊情況就不用管了,因為我們只用到primes數組里的東西,st數組不在這題里用;循環條件中的primes[i]
#include <iostream>using namespace std;const int N = 4e4;int primes[N], cnt; bool st[N];void init() {for (int i = 2; i < N; i ++ )if (!st[i]){primes[cnt ++ ] = i;for (int j = i * 2; j < N; j += i)st[j] = true;} }bool check(string s) {int x = stoi(s);for (int i = 0; primes[i] <= x / primes[i]; i ++ )if (x % primes[i] == 0)return false;return true; }int main() {init();int l, k;string s;cin >> l >> k >> s;for (int i = 0; i + k <= l; i ++ ){string ss = s.substr(i, k);if (check(ss)){cout << ss;return 0;}}cout << 404;return 0; }

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。