题目链接: http://ac.jobdu.com/problem.php?pid=1098
题目分析:
使用两个数组分别存储字母表和对应的字母出现次数。
源代码:
#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; }