Problem J: Summing Digits
中文題目: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://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;
}