Codeforces 1352A Sum of Round Numbers 概數的加總

題目說明

難易度 : 800

英文原文如下

A positive (strictly greater than zero) integer is called round if it is of the form d00…0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from 11 to 99 (inclusive) are round.

For example, the following numbers are round: 4000, 1, 9, 800, 90. The following numbers are not round: 110, 707, 222, 1001.

You are given a positive integer (1≤n≤104). Represent the number n as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number n as a sum of the least number of terms, each of which is a round number.

中文翻譯

正整數被稱為「概數」,如果它的形式是 d00…0。換句話說,如果除了最左邊(最高有效位)的數字外,所有數字都等於零,那麼正整數就是概數。特別地,從 11 到 99(包括)的所有數字都是 round 的。

例如,以下數字是概數:4000、1、9、800、90。以下數字不是概數的:110、707、222、1001。

給定一個正整數 n(1≤n≤104)。用最少的加數表示數字 n,使其表示為一系列概數之和。換句話說,您需要將給定的數字 n 表示為最少數量的項,每個項都是一個概數。

輸入

The first line contains an integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.

Each test case is a line containing an integer n (1≤n≤104).

第一行包含了一個整數 t (1≤t≤104) — 多少個測試案例,也就是接下來的 t 行。

每一個測試案例均占一行,包含了一個整數 n (1≤n≤104)。

輸出

Print t answers to the test cases. Each answer must begin with an integer k — the minimum number of summands. Next, k terms must follow, each of which is a round number, and their sum is n. The terms can be printed in any order. If there are several answers, print any of them.

印出 t 個答案,每一個答案應該由一個整數 k 開始 — 加數的最小數量。接下來,k 個加數應該會顯示在下一行,每一個都是一個概數(除了最高位數外,其餘都是0),而且他們的總和是 n。加數的答案可以被用任何順序印出,要是有多個答案,印出其中之一就好。

範例

輸入5
5009
7
9876
10000
10
輸出2
5000 9
1
7
4
800 70 6 9000
1
10000
1
10

5秒鐘快速了解

這一題會給予一個正整數,要將給予的數字拆開來成最小數量的概數(也就是除了最高位數外,其餘都是0)

舉例來說,上面的例子 9876 > 可以拆成 9000 + 800 +70 +6 這4個概數

5009 則是被拆成 5000+9,當然也可以被拆為 4000+1000+9,但題目要求的是最少的數量

故,將給予的數字,除了 0 以外的位數都將自己的數字拆出一個概數就是我們要做的


解題思路

在我們的解題中,我們是從最小位數開始,一路往上判斷是否除10有餘數,代表最後一位是否為0

另外使用了 multi 來判斷位數,因為 n 會 /=10,故後續的計算也要補回來給他

C#解決方案

方案1

int t = int.Parse(Console.ReadLine());

for(int i=0; i<t; i++){
    int n = int.Parse(Console.ReadLine());
    List<int>tmp = new List<int>();
    int multi = 1;
    
    while(n>0){
        if(n%10>0){
            tmp.Add(n%10*multi);       
        }
        n/=10;
        multi*=10;
    }
    Console.WriteLine(tmp.Count);
    Console.WriteLine(string.Join(" ", tmp));
}

結論

round numbers 本來一直不知道該怎麼翻,故找了較為類似的概數(大概的數字)來翻譯

希望意思上沒有太大的問題

另外,數字位數的計算印象中在LeetCode寫過類似的,找到後再來補充下

最近開始接觸資安類的CTF,希望可以增加原碼掃描的功力以及補全一些概念

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 1352A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *