a222簡單流程模擬、時間與狀態更新
TOI 2022/10 新手組:資料分類
依目前整數位數套規則,每次轉回整數去掉前導零。
這題在學什麼
四位數拆成兩組,先將各組轉成整數:小於 10 原樣保留,否則用十位乘個位,再將兩組結果串接。
三位數 abc 產生 a×b 與 b×c 的十進位結果並串接;兩位數直接相乘。
每回合把串接結果轉成整數,再判斷新位數,直到小於 10。像 03 應視為 3,不能錯算成 0。
Step-by-step animation
資料分類・分組乘積與字串串接
步驟 1 / 3
四位數 1234
12 得 1×2=2,34 得 3×4=12,串接為 212。
12→2
34→12
212
- n
- 212
#include <iostream>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
auto part = [](int x) { return x < 10 ? x : (x / 10) * (x % 10); };
while (n >= 10) {
string s = to_string(n);
if (s.size() == 4)
n = stoi(to_string(part(n / 100)) + to_string(part(n % 100)));
else if (s.size() == 3) {
int a = n / 100, b = n / 10 % 10, c = n % 10;
n = stoi(to_string(a * b) + to_string(b * c));
} else
n = (n / 10) * (n % 10);
}
cout << n << '\n';
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。