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

更新於 發佈於 閱讀時間約 7 分鐘

The 3n + 1 problem

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

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

#include <iostream>
using namespace std;

int main() {
int i, j;
while (cin >> i >> j) {
int mx = 0;
for (int n=min(i, j); n<=max(i, j); n++) {
int n1 = n, cnt = 1;
while (n1 != 1) {
if (n1 % 2) n1 = 3*n1 + 1;
else n1 /= 2;
cnt++;
}
mx = max(mx, cnt);
}
cout << i << " " << j << " " << mx << "\n";
}
return 0;
}

You can say 11

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

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

#include <iostream>
using namespace std;

int main() {
string s;
while (cin >> s) {
if (s == "0") break;
int odd = 0, even = 0;
for (int i=0; i<s.size(); i++) {
if (i % 2) {
odd += s[i] - '0';
} else {
even += s[i] - '0';
}
}
if ((odd - even) % 11 == 0) {
cout << s << " is a multiple of 11.\n";
} else {
cout << s << " is not a mutiple of 11.\n";
}
}
return 0;
}

Bangla Numbers

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

#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
int a[] = {10000000, 100000, 1000, 100};
string s[] = {"kuti", "lakh", "hajar", "shata"};
string ans;

string num2str(ll x) {
string s = "";
while (x) {
s += '0' + (x % 10);
x /= 10;
}
reverse(s.begin(), s.end());
return s;
}

void solve(ll x) {
if (x >= a[0]) {
solve(x / a[0]);
ans += " " + s[0];
x %= (int) a[0];
}
for (int i=0; i<4; i++) {
if (x / a[i] > 0) {
ans += " " + num2str(x / a[i]) + " " + s[i];
x %= a[i];
}
}
if (x > 0) ans += " " + num2str(x);
}

int main() {
ll N;
int Case = 1;
while (cin >> N) {
cout << " " << Case++ << ".";
if (N == 0) cout << " 0\n";
else {
ans = "";
solve(N);
cout << ans << "\n";
}
}
return 0;
}



留言
avatar-img
留言分享你的想法!
avatar-img
Jim的沙龍
0會員
31內容數
Jim的沙龍的其他內容
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.
2025/08/12
智能晶片是什麼呢? 在邊緣運算中,有一個重要的零組件,就是智能晶片。在無人機上要有一個智能晶片,才能達到即時辨識目標的判斷。 傳統的晶片所做的有加、減、乘、除的運算。而智能晶片是通過智能化的推理給出一個結果。 傳統的晶片中有三個重要的單元,算術邏輯運算單元(ALU)、數學運算單元(MU)、記憶
2025/08/12
智能晶片是什麼呢? 在邊緣運算中,有一個重要的零組件,就是智能晶片。在無人機上要有一個智能晶片,才能達到即時辨識目標的判斷。 傳統的晶片所做的有加、減、乘、除的運算。而智能晶片是通過智能化的推理給出一個結果。 傳統的晶片中有三個重要的單元,算術邏輯運算單元(ALU)、數學運算單元(MU)、記憶
2025/08/11
邊緣運算(Edge Computing)是什麼呢? 邊緣運算(Edge Computing)就是把運算從很高的雲端推送到前緣,在接近使用者的地方進行運算。 為什麼要使用邊緣運算? 當你走進廁所想要開燈並且說出開燈時,電燈就要把你的聲音路起來送到雲端分析你的聲音,再把開燈的指令送回來,如果遇上網
2025/08/11
邊緣運算(Edge Computing)是什麼呢? 邊緣運算(Edge Computing)就是把運算從很高的雲端推送到前緣,在接近使用者的地方進行運算。 為什麼要使用邊緣運算? 當你走進廁所想要開燈並且說出開燈時,電燈就要把你的聲音路起來送到雲端分析你的聲音,再把開燈的指令送回來,如果遇上網
看更多