题解 | #统计字符#
统计字符
http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
/**
Input:
awq
p`rreuhmgf$o_mtjfkjc&kv_#gudn(&wjtu!z`&lkh+p))e)ags!)#&ecosk)om* n^wiz
Output:
3
0
0
0
47
1
0
22
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void stat(const string& s, vector<size_t>& a) {
size_t len = s.length();
int code = 0;
a[0] = a[1] = a[2] = a[3] = 0;
for (size_t i = 0; i < len; i++) {
code = s.at(i);
if (('A'<=code && code<='Z')|| ('a'<=code && code<='z')) {
a[0] += 1;
} else if (' ' == code) {
a[1] += 1;
} else if ('0'<=code && code<='9') {
a[2] += 1;
} else {
a[3] += 1;
}
}
}
};
int main() {
Solution solution;
vector<size_t> sum(4, 0);
int c = 0;
string s = "";
for (;;) {
c = cin.get();
if (c == EOF) {
break;
}
if (c=='\r' || c=='\n') {
solution.stat(s, sum);
for (auto it = sum.begin(); it != sum.end(); ++it) {
cout << *it << "\n";
}
s = "";
} else {
s.append(1, c);
}
}
}
// 64 位输出请用 printf("%lld")
查看14道真题和解析