3-3 数数字

算法入门经典 P57

把前n(n<=100000)个整数顺序写在一起,123456789...数一数0-9各出现多少次。

#include<stdio.h>
#include<string.h>
#include "stdafx.h"
#include "iostream"        
#include <string>
#define maxn 10

int main() {
    std::cout << "Please enter numbers:" << std::endl;
    std::string s;
    std::cin >> s;
    int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0;
    for (int n = 0; n < s.size(); n++) {
        switch (s[n]) {
            case '0': a++; break;
            case '1': b++; break;
            case '2': c++; break;
            case '3': d++; break;
            case '4': e++; break;
            case '5': f++; break;
            case '6': g++; break;
            case '7': h++; break;
            case '8': i++; break;
            case '9': j++; break;
            }
        }
    std::cout << s << std::endl;
    std::cout << a << b << c << d << e << f << g << h << i << j << std::endl;
}

猜你喜欢

转载自www.cnblogs.com/NK-007/p/9235269.html
3-3