首页 > 试题广场 >

个位数统计 (15)

[编程题]个位数统计 (15)
  • 热度指数:22881 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。

输入描述:
每个输入包含1个测试用例,即一个不超过1000位的正整数N。


输出描述:
对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。
示例1

输入

100311

输出

0:2<br/>1:3<br/>3:1
import java.util.Scanner; public class appear_time { public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine(); int[] array = new int[10]; for (int i = 0; i < str.length();i ++){
            array[Integer.valueOf(String.valueOf(str.charAt(i)))]++;
        } for (int j = 0; j < 10; j++){ if (array[j] > 0)
                System.out.println(j+ ":" +array[j]);
        }
    }
}
发表于 2019-06-24 21:27:23 回复(0)
更多回答

python三行解法:

from collections import Counter
a=sorted(Counter(input()).items(),key=lambda c:c[0])
for i in a:print(str(i[0])+":"+str(i[1]))
发表于 2017-10-09 11:03:13 回复(4)
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
	string n;
	map<char, int> m;
	while (cin >> n)
	{
		for (auto x :n)
		{
			if (m.find(x)==m.end())
			{
				m[x] = 1;
			}
			else
			{
				m[x]++;
			}
		}
		for (auto x : m)
			cout << x.first << ":" << x.second << endl;
	}
	return 0;
}

发表于 2016-02-18 19:50:17 回复(0)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[] a = new int[10];
		String n = in.next();
		for(int i = 0;i<n.length();i++)
			a[n.charAt(i)-'0']++;
		for(int i = 0;i<a.length;i++)
			if(a[i]!=0)
				System.out.println(i+":"+a[i]);
	}
}

发表于 2016-06-09 09:37:40 回复(11)
import java.util.*;

/**
 * 个位数统计
 * 题目描述
 * 给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),
 * 请编写程序统计每种不同的个位数字出现的次数。例如:给定N = 100311,则有2个0,3个1,和1个3。
 * 输入描述:
 * 每个输入包含1个测试用例,即一个不超过1000位的正整数N。
 * 输出描述:
 * 对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。
 * 输入例子:
 * 100311
 * 输出例子:
 * 0:2
 * 1:3
 * 3:1
 *
 * @author shijiacheng
 * @date 2018/1/28
 */
public class B1011StatisticSingleNumber {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String N = sc.next();
        char[] chars = N.toCharArray();

        /**
         * list***有10个item表示0-9,每个item的值表示数字的个数
         */
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(0);//初始化每个item都是0
        }
        for (int i = 0; i < chars.length; i++) {
            // 每遇到一个数字,就把值+1
            list.set(chars[i]-'0',list.get(chars[i]-'0')+1);
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)>0){
                System.out.println(i+":"+list.get(i));
            }
        }
    }
}
发表于 2018-02-02 22:21:01 回复(0)
我又来贴python版的来了==  
把输入直接当成字符串来处理,使用字典记录每个数字出现的情况,最后辅助一个列表进行“排序”
#coding=utf-8
s=raw_input()
ans={}
for i in s:
	if i in ans.keys():
		ans[i]+=1
	else:
		ans[i]=1
#print ans
#完成统计 以下代码按照D的升序输出
up=[]
for i in ans.keys():
	up.append(i)
up.sort()
for i in up:
	print "%s:%d"%(i,ans[i])

发表于 2016-01-20 21:15:24 回复(0)
import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner in=new Scanner(System.in);
        String s=in.nextLine();
        int num[]=new int[10];
        for(char c:s.toCharArray()){
            num[c-'0']++;
        }
        for(int i=0;i<10;i++){
            if(num[i]!=0)
                System.out.println(i+":"+num[i]);
        }
    }
}

发表于 2019-04-25 13:49:48 回复(0)
//大家好,我是yishuihan
#include<iostream>
#include<string>
using namespace std;
void helper_4(string num)
{
	int hash[10]={0};
	if(num.length()<=0) return;
	int res=0;
	for(int i=0;i<num.length();i++)
	{
		hash[num[i]-'0']++;
	}
	for(int i=0;i<=9;i++)
	{
		if(hash[i]!=0)
		{
			cout<<i<<":"<<hash[i]<<endl;
		}
	}
}
int main()
{
	string num;
	while(cin>>num)
	{
	   helper_4(num);
	 } 
	return 0;
}

发表于 2016-08-11 13:09:30 回复(1)
/*
 * app=PAT-Basic lang=c++
 * https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296
 */
#include <cstdio>
#include <cstring>
using namespace std;

char ch[1010] = {};
int main()
{
    int cout[11] = {};
    fgets(ch,1010,stdin);
    int len = strlen(ch) - 1;
    for (int i = 0; i < len;i++){
        cout[ch[i] - '0'] ++;
    }
    for (int i = 0; i < 10;i++){
        if (cout[i] != 0){
            printf("%d:%d\n",i,cout[i]);
        }
    }
    return 0;
}
发表于 2019-12-03 13:55:58 回复(0)
#include <stdio.h>
#include <string.h>
 int main()
{
char c;
int num[10];
int i;
for(i=0;i<10;i++)
num[i]=0;
while((c=getchar())!='\n')
if(c>='0'&&c<='9')
num[c-48]+=1;
for(i=0;i<10;i++)
if(num[i])
printf("%c:%d\n",'0'+i,num[i]);
return 0;
}
编辑于 2019-07-26 09:42:14 回复(0)
import java.util.*;
public class Main {

    /**
     * 个位数统计
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        String N=sc.next();
        int a[]=new int[N.length()];
        for(int i=0;i<N.length();i++)
        {
            a[i]=Integer.parseInt(N.charAt(i)+"");
            
        }
        int temp;
        for(int i=0;i<N.length();i++)
        {            
            for(int j=0;j<N.length()-1-i;j++)
            {
                if(a[j]>a[j+1])
                {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }

        int x=1;//记录不同数的个数
        for(int i=0;i<N.length()-1;i++)
        {
            if(a[i]<a[i+1])
                x++;
        }
        //找出有哪些数与其对应的个数
        int D[]=new int[x];//有哪些数
        int shu[]=new int[x];//对应的个数
        for(int i=0,j=0,k=0;i<N.length()&&j<x&&k<N.length();)
        {            
            if(a[i]==a[k])
            {
                D[j]=a[i];
                shu[j]=k-i+1;
                k++;
            }            
            else
            {
                i=k;
                j++;            
            }
        }        
        //输出
        for(int i=0;i<x;i++)
        {
            System.out.println(D[i]+":"+shu[i]);
        }
        
    }

}

发表于 2019-06-11 17:25:52 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main(){
    int  a[10] = { 0 };
    string str;
    cin >> str;
    for (auto s : str){
        a[s - '0']++;
    }
    for (int i = 0; i < 10; i++){
        if (a[i]>0){
            cout << i << ":" << a[i] << endl;
        }
    }
    return 0;
}



发表于 2019-04-24 16:20:49 回复(0)
思路: 数组统计
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string num;
    cin >> num;
    int numArray[10] = { 0 };
    int sizeN = num.size();
    for (int i = 0; i < sizeN; i++){
        numArray[num[i] - '0']++;
    }
    for (int i = 0; i < 10; i++){
        if (numArray[i] == 0)
            continue;
        cout << i << ":" << numArray[i] << endl;;
    }
    system("pause");
}

编辑于 2018-09-04 10:37:44 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
#define FOR(i,n) for(i=0;i<n;i++)



int main()
{
    char N[1000];
    int M[10] = { 0 }, i;
    cin >> N;

    FOR(i,(int)strlen(N))
    {
        switch (N[i])
        {
        case '0':
            M[0]++;    break;
        case '1':
            M[1]++;    break;
        case '2':
            M[2]++;    break;
        case '3':
            M[3]++;    break;
        case '4':
            M[4]++; break;
        case '5':
            M[5]++; break;
        case '6':
            M[6]++; break;
        case '7':
            M[7]++; break;
        case '8':
            M[8]++; break;
        case '9':
            M[9]++; break;
        default:
            break;
        }
    }

    FOR(i, 10)
    {
        if (M[i])
        {
            cout << i << ":" << M[i] << endl;
        }
    }


    return 0;
}
发表于 2018-04-21 20:11:07 回复(0)
  1. 想起来map很好用
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. using namespace std;

  6. int main()
  7. {
  8. string strN;
  9. int count[10] = {0,0,0,0,0,0,0,0,0,0};
  10. while (cin >> strN)
  11. {
  12. for (int i = 0; i < strN.size(); i++)
  13. {
  14. for (int j = 0; j < 10; j++)
  15. {
  16. if (strN[i] - 48 == j)
  17. {
  18. count[j]++;
  19. break;
  20. }
  21. }
  22. }
  23. //将结果存储到map中
  24. map<int, int> result;
  25. for (int m = 0; m < 10; m++)
  26. {
  27. if (count[m] != 0)
  28. {
  29. result[m] = count[m];
  30. }
  31. }

  32. //输出map
  33. for (map<int, int>::iterator iter = result.begin(); iter != result.end(); iter++)
  34. {
  35. cout << iter->first << ":" << iter->second << endl;
  36. }
  37. }
  38. return 0;
  39. }
编辑于 2018-03-14 16:33:05 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    //对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出 100311
    //0:2<br/>1:3<br/>3:1
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        String s=sc.next();
        char[] number = s.toCharArray();
        Map<Integer,Integer> map=new HashMap<>();
        for (char c:number) {
                map.put(c-'0',(map.getOrDefault(c-'0',0))+1);
        }
        for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
            int num=entry.getKey();
            int count=entry.getValue();
            System.out.println(num+":"+count);
        }
    }
}

发表于 2020-12-06 19:41:46 回复(0)
c语言桶排序思想,代码简短易懂
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[10];
char arr[1005];
int main(){
    int i;
  scanf("%s",arr);
  int len = strlen(arr);
  for(i = 0; i < len; i++){
    a[arr[i] - '0']++;
  }
  for(i = 0; i< 10; i++){
    if(a[i])
        printf("%d:%d\n",i,a[i]);
  }
 return 0;
}
发表于 2020-11-14 10:36:49 回复(0)
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str;
    cin>>str;
    int count[1000]={0};
    for(int i=0;i<str.size();i++)
    {
        int t=str[i]-'0';
        count[t]++;
    }
    for(int i=0;i<1000;i++)
    {
        if(count[i]!=0)
            cout<<i<<":"<<count[i]<<endl;
    }
    return 0;  
    
}
很经典的用法,自加
发表于 2019-01-15 16:05:50 回复(2)
初学python,强迫自己使用python解题练习,果然很难受。。
num=input()
dic=dict()
lst=[]
length=len(num)
num=int(num)
for i in range(length):
    t=num%10
    num//=10
    lst.append(t)
for j in lst:
    if j in dic:
        dic[j]=dic[j]+1
    else:
        dic[j]=1
mlist=dic.keys()
mlist=sorted(mlist)
for i in mlist:
    print(i,dic[i],sep=':')

发表于 2018-02-23 11:35:11 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        int[] num = {0,0,0,0,0,0,0,0,0,0};
        for (int i = 0;i < a.length();i++){
            num[(int)(a.charAt(i) - '0')]++;
        }
        for (int i = 0;i < 10;i++){
            if (num[i] != 0)
                System.out.println(i + ":" + num[i]);
        }
    }
}
发表于 2017-08-25 12:05:25 回复(0)
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Main{
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		char [] arr= sc.nextLine().toCharArray();
		Map <Character,Integer> number = new TreeMap<>();
		for (char c : arr) {
			if(number.containsKey(c)){
				number.put(c, number.get(c)+1);
			}else{
				number.put(c, 1);
			}
		}
		Iterator<Entry<Character, Integer>> it = number.entrySet().iterator();
		Entry<Character, Integer> en;
		while(it.hasNext()){
			en = it.next();
			System.out.println(en.getKey()+":"+en.getValue());
		}
	}

}


发表于 2017-08-14 21:45:30 回复(0)