【CPE】一顆星選集(Part 4) | C++

更新 發佈閱讀 5 分鐘

Problem J: Summing Digits

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2307#google_vignette

中文題目:https://zerojudge.tw/ShowProblem?problemid=c813

#include <iostream>
using namespace std;

int solve(int x) {
int ret = 0;
while (x) {
ret += x % 10;
x /=10;
}
if (ret < 10) return ret;
else return solve(ret);
}

int main() {
int n;
while (cin >> n && n) {
cout << solve(n) << "\n";
}
return 0;
}

Common Permutation

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1193

中文題目:https://zerojudge.tw/ShowProblem?problemid=e507

#include <iostream>
using namespace std;

string a, b;
int cnta[26], cntb[26];

int main() {
while (cin >> a >> b) {
for (int i=0; i<26; i++) {
cnta[i] = 0;
cntb[i] = 0;
}
for (int i=0; i<a.length(); i++) {
cnta[a[i]-'a']++;
}
for (int i=0; i<b.length(); i++) {
cntb[b[i]-'a']++;
}
for (int i=0; i<26; i++) {
for (int j = min(cnta[i], cntb[i]); j>0; j--) {
cout << (char) ('a'+i);
}
}
cout << "\n";
}
return 0;
}

Rotating Sentences

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=431#google_vignette

中文題目:https://zerojudge.tw/ShowProblem?problemid=c045

#include <iostream>
using namespace std;

string s[105];

int main() {
int col = 0, row = 0;
while (getline(cin, s[col])) {
row = max(row, (int) s[col].size());
col++;
}
for (int i=0; i<row; i++) {
for (int j = col-1; j>=0; j--) {
if (i >= s[j].size()) cout << " ";
else cout << s[i][j];
}
cout << "\n";
}
return 0;
}
留言
avatar-img
留言分享你的想法!
avatar-img
Jim的沙龍
1會員
31內容數
Jim的沙龍的其他內容
2025/08/13
List of Conquests 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1361 #include <iostream
2025/08/13
List of Conquests 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1361 #include <iostream
2025/08/13
The 3n + 1 problem 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36#google_vignette 中文題目
2025/08/13
The 3n + 1 problem 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36#google_vignette 中文題目
2025/08/12
Vito's family 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=982 中文題目:https://zerojudge.
2025/08/12
Vito's family 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=982 中文題目:https://zerojudge.
看更多