入门模拟——(字符串处理)B1021个位数统计

第二次:

#include <bits/stdc++.h>
#include <math.h>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAX_LEN = 100005;
//const int MAX_D = 31;
int main(){
    char str[MAX_LEN];
    int result[10] ={0};
    //gets(str);
    cin>>str;
    int len = strlen(str);
    for(int i =0;i<len;++i){
        result[str[i]-'0']++;
    }
    for(int i = 0;i<10;++i){
        if(result[i]!=0){
            printf("%d:%d
",i,result[i]);
        }
    }
    system("pause");
    return 0;
} 

第一次:

#include <bits/stdc++.h>
#include<math.h>
using namespace std;
const int MAX_LEN = 100005;
//const int MAX_D = 31;
int main(){
    int n;
    cin>>n;
    int digit = 0;
    int temp[10]={0};
    if(n == 0){
        printf("0:1
");
    }
    while(n != 0){
        int d =  n % 10;
        temp[d]++;
        n = n/10;
    }
    for(int i=0;i<=9;++i){
        if(temp[i]!=0){
            printf("%d:%d
",i,temp[i]);
        }
    }
    system("pause");
    return 0;
} 
原文地址:https://www.cnblogs.com/JasonPeng1/p/12129611.html