計算機概論|HW16

題目敘述

請設計一程式,此程式可以不斷地請使用者輸入一整數 N (<=2,147,483,647),直到 N <= 0 時結束程式。假設 N 為一個由 n 個數字所組合而成的整數。即 N = In …I2I1,在此 Ii 為第 i 個數字。請輸出結果 On …O2O1,在此當In … Ii+1 Ii-1 …I2I1 是 Ii 的倍數時 Oi 為 T,否則Oi 為 F。
如果 Ii 為0時,Oi 為 F。
Note that zero is a multiple of every number

範例輸入

1
2
3
4
5
6
7
123456  
> TTTTFF  

2147483647  
> FTFTFFFFFF  

0  

答題思路

參考答案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main() {
    string s;
    while(cin >> s and stoi(s) > 0) {
        if(s.length() == 1)
            cout << ((s == "1")? "Tn": "Fn");
        else {
            for(int i=0; i < s.length(); i++) {
                string copy (s);
                copy.erase(i, 1);
                cout << ((stoi(copy) % int(s[i] - 48) == 0)? 'T': 'F');
            }
            cout << endl;
        }
    }
    
    return 0;
}
最後更新 Dec 28, 2022 11:30 +0800