1. 程式人生 > >UVa-1225 Digit Counting(數數字)

UVa-1225 Digit Counting(數數字)

c++代碼 body class tor count pac cout auto pre

對於一個大於1且小於10000的整數N,我們定義一個唯一與之相關聯的序列。例如,若N為13,則該序列為12345678910111213。現要求對於一個輸入值N,記錄這個序列中每個數字出現了多少次。

輸入格式:先是一個整數以表明要輸入的整數個數,之後每行給出一個大於1且小於10000的整數。

輸出格式:每行輸出每個數字出現的個數。如果輸入為13,那麽輸出為1 6 2 2 1 1 1 1 1 1。

具體見https://cn.vjudge.net/problem/uva-1225

思路:沒有多加思考,直接上。取序列,再遍歷。這次用了一個vector存入答案,最後統一輸出。

我的C++代碼:

 1  #include <iostream>
 2
#include <string> 3 #include <vector> 4 #include <array> 5 using namespace std; 6 int main() 7 { 8 int T, n; 9 cin >> T; 10 vector<string> re; 11 for (int i = 0;i < T;++i) 12 { 13 array<int, 10> dig{ 0 };//這裏使用int dig[10]={ 0 }即可,我僅僅是試試剛學的標準庫容器...
14 cin >> n; 15 string s, s2; 16 for (int j = 1;j <= n;++j) 17 { 18 s += to_string(j); 19 } 20 for (auto x : s) 21 { 22 dig[x - 0]++; 23 } 24 bool t = true; 25 for (auto x : dig)
26 { 27 if (t) 28 { 29 t=!t; 30 s2 += to_string(x); 31 } 32 else s2 += " " + to_string(x); 33 } 34 re.push_back(s2); 35 } 36 for (auto x : re)cout << x << endl; 37 return 0; 38 }

UVa-1225 Digit Counting(數數字)