題目分析:
使用兩個數組分別存儲字母表和對應的字母出現次數。
源代碼:
[cpp]
#include<iostream>
#include<string>
using namespace std;
int main()
{
char ch[30] = {'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'};
string s;
while (cin>>s)
{
int num[30] = {0};
for (int i = 0; i < s.length(); i++)
{
switch (s[i])
{
case 'A':
num[0] ++;
break;
case 'B':
num[1] ++;
break;
case 'C':
num[2] ++;
break;
case 'D':
num[3] ++;
break;
case 'E':
num[4] ++;
break;
case 'F':
num[5] ++;
break;
case 'G':
num[6] ++;
break;
case 'H':
num[7] ++;
break;
case 'I':
num[8] ++;
break;
case 'J':
num[9] ++;
break;
case 'K':
num[10] ++;
break;
case 'L':
num[11] ++;
break;
case 'M':
num[12] ++;
break;
case 'N':
num[13] ++;
break;
case 'O':
num[14] ++;
break;
case 'P':
num[15] ++;
break;
case 'Q':
num[16] ++;
break;
case 'R':
num[17] ++;
break;
case 'S':
num[18] ++;
break;
case 'T':
num[19] ++;
break;
case 'U':
num[20] ++;
break;
case 'V':
num[21] ++;
break;
case 'W':
num[22] ++;
break;
case 'X':
num[23] ++;
break;
case 'Y':
num[24] ++;
break;
case 'Z':
num[25] ++;
break;
default:
break;
}
}
for (int i = 0; i <= 25; i++)
{
cout<<ch[i]<<":"<<num[i]<<endl;
}
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
char ch[30] = {'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'};
string s;
while (cin>>s)
{
int num[30] = {0};
for (int i = 0; i < s.length(); i++)
{
switch (s[i])
{
case 'A':
num[0] ++;
break;
case 'B':
num[1] ++;
break;
case 'C':
num[2] ++;
break;
case 'D':
num[3] ++;
break;
case 'E':
num[4] ++;
break;
case 'F':
num[5] ++;
break;
case 'G':
num[6] ++;
break;
case 'H':
num[7] ++;
break;
case 'I':
num[8] ++;
break;
case 'J':
num[9] ++;
break;
case 'K':
num[10] ++;
break;
case 'L':
num[11] ++;
break;
case 'M':
num[12] ++;
break;
case 'N':
num[13] ++;
break;
case 'O':
num[14] ++;
break;
case 'P':
num[15] ++;
break;
case 'Q':
num[16] ++;
break;
case 'R':
num[17] ++;
break;
case 'S':
num[18] ++;
break;
case 'T':
num[19] ++;
break;
case 'U':
num[20] ++;
break;
case 'V':
num[21] ++;
break;
case 'W':
num[22] ++;
break;
case 'X':
num[23] ++;
break;
case 'Y':
num[24] ++;
break;
case 'Z':
num[25] ++;
break;
default:
break;
}
}
for (int i = 0; i <= 25; i++)
{
cout<<ch[i]<<":"<<num[i]<<endl;
}
}
return 0;
}