案例可能有多组,每个案例输入为一行字符串。
对每个案例按A-Z的顺序输出其中大写字母出现的次数。
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
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]);
}
}
} #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要记得先进行正确的初始化。
#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;
} #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;
} #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;
}
}
} #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;
} #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;
} #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;
} 看到 题解里 创建一个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;
}
#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;
}