題目內容
Solution
總之就是把數字用字元串來處理,之後int ia = str[j] – ‘0’;把字元串轉回Integer,然後輸出就可以了
| #include <iostream>#include <sstream>#include <vector>#include <string>#include <stdio.h>#include <stdlib.h>usingnamespacestd;intzero, one, two, three, four, five, six, seven, eight, nine;voidaddNum(intnum){    switch(num){        case0:            zero++;            break;        case1:            one++;            break;        case2:            two++;            break;        case3:            three++;            break;        case4:            four++;            break;        case5:            five++;            break;        case6:            six++;            break;        case7:            seven++;            break;        case8:            eight++;            break;        case9:            nine++;            break;    }}intmain(){    intt;    while(cin >> t){        while(t--){            zero = 0;one = 0; two = 0; three = 0; four = 0; five = 0; six = 0; seven = 0; eight = 0; nine = 0;            intn;            vector<int> vn;            cin >> n;            for(inti = 1; i <= n; i++){                vn.push_back(i);            }                        for(inti = 0; i < vn.size(); i++){                string str;                                stringstream ss;                ss << vn[i]; ss >> str;                for(intj = 0; j < str.length(); j++){                    intia = str[j] - '0';                    addNum(ia);                }                        }                        cout << zero << " "<< one << " "<< two << " "<< three << " "<< four << " "<< five << " "<< six << " "<< seven << " "<< eight << " "<< nine << endl;                    }    }        return0;} | 
 
No comments:
Post a Comment