原題地址:牛客網
題目描述:
輸入一行字符串,計算其中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
分析:
就是統計所有大寫字母出現的次數而已。
如果不會ascii碼的話甚至可以一一比對。
代碼:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
while(cin>>a)
{
int b[100];
for(int i=65;i<=90;i++)
{
b[i]=0;
}
for(int i=0;i<a.length();i++)
{
if(a[i]>='A'&&a[i]<='Z')b[a[i]]++;
}
int t=0;
for(int i=65;i<=90;i++)
{
cout<<char('A'+t)<<":"<<b[i]<<endl;
t++;
}
}
return 0;
}
2020.4.4