PAT乙級1006

1006 換個格式輸出整數 (15分)

題目地址:https://pintia.cn/problem-sets/994805260223102976/problems/994805318855278592ios

讓咱們用字母 B 來表示「百」、字母 S 表示「十」,用 12...n 來表示不爲零的個位數字 n(<10),換個格式來輸出任一個不超過 3 位的正整數。例如 234 應該被輸出爲 BBSSS1234,由於它有 2 個「百」、3 個「十」、以及個位的 4。測試

輸入格式:

每一個測試輸入包含 1 個測試用例,給出正整數 n(<1000)。spa

輸出格式:

每一個測試用例的輸出佔一行,用規定的格式輸出 n。code

輸入樣例1

234

輸出樣例1

BBSSS1234

輸入樣例2

23

輸出樣例2

SS123

個人理解

對不超過3位的正整數進行拆分,找出其百位,十位,各位格式多少,對應輸出便可。可貴的一次AC。ci

代碼段

#include<iostream>
using namespace std;
int main() {
    int number = 0;
    cin >> number;
    // 百位
    int x = number / 100;
    number %= 100;
    // 十位
    int y = number / 10;
    // 個位
    number %= 10;
    for (int i = 0; i < x; i++) {
        cout << "B";
    }
    for (int i = 0; i < y; i++) {
        cout << "S";
    }
    for (int i = 0; i < number; i++) {
        cout << i + 1;
    }
    return 0;
}
相關文章
相關標籤/搜索