首页 > 试题广场 >

字母统计

[编程题]字母统计
  • 热度指数:17053 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一行字符串,计算其中A-Z大写字母出现的次数

输入描述:
案例可能有多组,每个案例输入为一行字符串。


输出描述:
对每个案例按A-Z的顺序输出其中大写字母出现的次数。
示例1

输入

DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK

输出

A:0
B:0
C:0
D:3
E:2
F:5
G:0
H:0
I:1
J:4
K:2
L:3
M:0
N:1
O:0
P:0
Q:1
R:0
S:2
T:0
U:0
V:0
W:1
X:0
Y:0
Z:0
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] array = scanner.nextLine().toCharArray();
        int[] record = new int[256];
        for (char c : array) {
            record[c]++;
        }
        for (int i = 'A'; i <= 'Z'; i++) {
            System.out.println((char) i+":"+record[i]);
        }
    }
}


发表于 2020-03-07 09:54:13 回复(0)
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{

    string str;

    while(cin>>str)
    {
        map<char,int> mp;
        for(int i=0;i<26;i++)
        {
            mp[char(int('A')+i)]=0;
        }
        for(int i=0;i<str.length();i++)
        {
            if(str[i]>='A'&&str[i]<='Z')
                mp[str[i]]++;
        }
        for(auto it=mp.begin();it!=mp.end();it++)
        {
            cout<<it->first<<":"<<it->second<<endl;
        }

    }

    return 0;
}
mp要记得先进行正确的初始化。
发表于 2019-03-21 16:56:29 回复(0)
#include<iostream>
using namespace std;
#include<string> 
#include <algorithm>  

int main()
{
    string a;
    cin>>a;
    int num;
    char temp='A';
    while(temp<='Z')
    {
        num=count(a.begin(),a.end(),temp);
        cout<<temp<<":"<<num<<endl;
        temp++;
    }
    return 0;
}


ez

发表于 2018-02-26 22:47:01 回复(0)
#include<iostream>
#include<map> 
using namespace std;

int main()
{
	string s;
	cin >> s;
	map<char, int> m;
	for(int i=0; i<s.length(); i++)
		m[s[i]]++;
	for(char i='A'; i<='Z'; i++)
		cout << i << ":" << m[i] << endl;

    return 0;
}

发表于 2021-02-17 12:59:58 回复(0)
用一个大小为26的数组,依次统计大写字母的个数,便可以得到解答。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

int number[26];  //标记数组
int main(){
    string str;
    while (getline(cin, str)) {
        memset(number, 0, sizeof(number));
        for (int i = 0; i < str.size(); ++i) {
            if ('A' <= str[i] && str[i] <= 'Z') {
                number[str[i] - 'A']++;
            }
        }
        for (int j = 0; j < 26; ++j) {
            printf("%c:%d\n", 'A' + j, number[j]);
        }
    }
    return 0;
}


发表于 2020-02-01 22:33:26 回复(0)
送分题长得真是可爱hhh
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
    string str;
    while(getline(cin,str)){
        char ch[100];
        int count[26];
        for(int i=0;i<26;i++)
            count[i]=0;
        strcpy(ch,str.c_str());
        for(int i=0;i<strlen(ch);i++){
            if(ch[i]>='A'&&ch[i]<='Z')
                count[ch[i]-'A']++;
        }
        for(int i=0;i<26;i++){
            cout<<(char)('A'+i)<<":"<<count[i]<<endl;
        }
    }
}

发表于 2019-02-10 22:28:24 回复(0)
#include <stdio.h>
#include <string.h>
int main(){
    char a[100];
    int num[26];
    while(scanf("%s",&a)!=EOF){
        for(int i=0;i<26;i++){
            num[i]=0;
        }
        for(int i=0;i<strlen(a);i++){
            if(a[i]>=65&&a[i]<=90){
                num[a[i]-65]++;
            }
        }
        for(int i=0;i<26;i++){
            printf("%c:%d\n",65+i,num[i]);
        }
    }
    return 0;
}

发表于 2019-01-24 16:40:11 回复(0)
import java.lang.*;
import java.util.*;
public class Main {
    public static void InversePairs(String string) {
        char [] chars=string.toCharArray();
        HashMap<Character,Integer> hashMap=new HashMap<>();
        char [] abc={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        for(int i=0;i<abc.length;i++)
        {
            hashMap.put(abc[i],0);
        }
        for(int i=0;i<chars.length;i++)
        {
            if(hashMap.containsKey(chars[i]))
            {
                int count=hashMap.get(chars[i]);
                hashMap.put(chars[i],count+1);
            }
        }
        for(int i=0;i<abc.length;i++)
        {
            System.out.println(abc[i]+":"+hashMap.get(abc[i]));
        }
    }
    public static void main(String[] args)
    {
        Scanner in =new Scanner(System.in);
        while(in.hasNext())
        {
            String s=in.nextLine();
            InversePairs(s);
        }
    }
}
发表于 2018-08-28 09:34:18 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main(){
    string line;
    while(cin>>line){
        int count[256]={0};
        for(int i=0;i<line.size();i++){
            count[line.at(i)]++;
        }
        for(int i='A';i<='Z';i++){
            printf("%c:%d\n",(char)i,count[i]);
        }
    }
    return 0;
}

发表于 2016-10-28 13:06:24 回复(0)
#include <stdio.h>
#include <string.h>
#define N 3000

int main()
{
    char str[N];
    int count[26];
    while(gets(str))
    {
        int len=strlen(str);
        for(int i=0; i<26; i++)
        {
            count[i]=0;
        }
        for(int i=0; i<len; i++)
        {
            if('A'<=str[i]&&str[i]<='Z')
            {
                count[str[i]-'A']++;
            }
        }
        for(int i=0; i<26; i++)
        {
            printf("%c:%d\n", 'A'+i, count[i]);
        }
    }
    return 0;
}

发表于 2018-02-23 11:06:48 回复(1)

python简单的解法,使用defaultdict来解,再适合不过了。

from collections import defaultdict

while True:
    try:
        a, dd = input(), defaultdict(int)
        for i in a:
            if i.isupper(): dd[i] += 1
        for i in range(65, 91):
            print(chr(i) + ":" + str(dd[chr(i)]))

    except:
        break
发表于 2017-10-06 16:35:13 回复(0)
#include<iostream>
#include<map>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        map<char,int> res;
        for(auto c:str)
        {
            if(isupper(c))
                res[c]++;
        }
        for(char c='A';c<='Z';cout<<c<<':'<<res[c++]<<endl);
    }
}

发表于 2018-08-29 20:07:00 回复(1)
#include<stdio.h>
(737)#include<string.h>
int main()
{
    char a[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ",x[1000];
    int b[27]={0},i,j,n;
    gets(x);  n=strlen(x);
    for(i=0;i<26;i++)
        for(j=0;j<n;j++)
            if(a[i]==x[j])
				b[i]=b[i]+1;
    for(i=0;i<26;i++)
        printf("%c:%d\n",a[i],b[i]);
}

发表于 2020-03-26 16:44:26 回复(0)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

struct Zimu{
	char a;
	int count;
};

Zimu zm[26];

//统计字母个数 
int main(){
	string str;
	while (getline(cin, str)){
		
		//赋初值 
		for (int i = 0; i < 26; i++){
			zm[i].a = 'A' + i;
			zm[i].count = 0;
		}
		
		//如果属于A~Z,则对应count++ 
		for (int i = 0; i < str.length(); i++) {
			if (str[i] - 'A' >= 0 && 'Z' - str[i] >= 0) {
				int n = str[i] - 'A';
				zm[n].count++;
			}
		}
		
		for (int i = 0; i < 26; i++){
			cout << zm[i].a << ":" << zm[i].count << endl;
		}
	}
	return 0;
} 

发表于 2023-03-22 16:51:21 回复(0)
我终于会写一道上交的题目了!!!!
发表于 2019-02-05 05:53:21 回复(1)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string s;
    while(cin>>s)
    {
        char a='A'-1;
        for(int i=0;i<26;i++)
        {
            a+=1;
            cout<<a<<":"<<count(s.begin(),s.end(),a)<<endl;
        }

    }
    return 0;
}

编辑于 2024-02-22 15:48:06 回复(0)
次数为0的也要统计,所以本题不可用 map<char,int>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    getline(cin,str);
    int count[26] = {0};
    for(int i = 0; i < str.size(); ++i){
        if(str[i]>='A' && str[i]<='Z'){
            count[str[i]-'A']++;
        }
    }
    for(int i = 0; i < 26; ++i){
        printf("%c:%d\n",'A'+i,count[i]);
    }
    return 0;
}


发表于 2024-02-14 11:08:30 回复(0)

看到 题解里 创建一个128大小的数组,利用ASCLL码的特性,遍历读来的字符串,然后再输出数组里A 到 Z的部分就可以了

题解方法很好,应该积累

我用的是map,这样时间复杂度也会很低, n的时间复杂度

不建议用双重循环,n2复杂度

#include <iostream>
#include "string"
#include "cstdio"
#include "map"
using namespace std;

int main() {
    string str;
    map<char, int> numbers;
    for (char c = 'A'; c <= 'Z'; c++) {
        numbers.insert(pair<char, int> {c, 0});
    }


    while (cin >> str) {
        for (int i = 0; i < str.length(); i++) {
            numbers[str[i]]++;
        }
        for (char c = 'A'; c <= 'Z'; c++) {
            printf("%c:%d\n", c, numbers[c]);
        }



    }






    return 0;
}

发表于 2023-03-02 22:11:27 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    char a[100];
    while(cin>>a){
        int len = strlen(a);
        for(char i='A';i<='Z';i++){
            int sum = 0;
            for(int j=0;j<len;j++){
                if(a[j] == i){
                    sum++;
                }
            }
            cout<<i<<":"<<sum<<endl;
        }
    }
    return 0;
}

发表于 2023-03-02 16:11:18 回复(0)
#include <stdio.h>
int main(){
    char s[1000];
    int a[26] = {0};
    scanf("%s", s);
    for (int i = 0; s[i]!='\0'; i ++) {
        if (s[i] >= 'A' && s[i] <= 'Z') {
            a[s[i]-'A'] ++;
        }
    }
    for (int i = 0; i < 26; i ++) {
        printf("%c:%d\n", 'A'+i, a[i]);
    }
    return 0;
}

发表于 2023-02-13 12:11:25 回复(0)