每个输入包含1个测试用例,即一个不超过1000位的正整数N。
对N中每一种不同的个位数字,以D:M的格式在一行中输出该位数字D及其在N中出现的次数M。要求按D的升序输出。
100311
0:2<br/>1:3<br/>3:1
#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; }
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]); } }
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));
}
}
}
}
#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])
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]); } } }
//大家好,我是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; }
/* * 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; }
思路: 数组统计 #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"); }
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); } } }
#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; }很经典的用法,自加
初学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=':')
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]);
}
}
}
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()); } } }
#encoding:utf-8 # 解法1 a = raw_input() tmp = [] for v in range(10): k = a.count(str(v)) if k: tmp.append((v,k)) for v in tmp: print '%s:%s' % v # 解法2 a = raw_input() res = {'0':0,'1':0,'2':0,'3':0,'4':0,'5':0,'6':0,'7':0,'8':0,'9':0} res1 = {} for v in a: res[v]+=1 res = [(x,res[x]) for x in res if res[x] !=0] res = sorted(res) for v in res: print '%s:%s' % v