第一轮
最后一版(AC)
#include <iostream>
#include <vector>
using namespace std;
int main() {
string s;
getline(cin, s);
vector<int> count(126 - 32 + 1, 0);
for (char c : s) {
count[c - ' ']++;
}
// 统计不同情况
int digit = 0;
for (int i = '0' - ' '; i <= '9' - ' '; i++) {
digit += count[i];
}
int space = count[0];
int alpha = 0;
for (int i = 'A' - ' '; i <= 'Z' - ' '; i++) {
alpha += count[i];
}
for (int i = 'a' - ' '; i <= 'z' - ' '; i++) {
alpha += count[i];
}
int other = 0;
for(int i = ' ' - ' '; i <= '~'- ' '; i++){
other += count[i];
}
other = other - space - alpha - digit;
// 输出
cout << alpha << endl;
cout << space << endl;
cout << digit << endl;
cout << other << endl;
}
// 64 位输出请用 printf("%lld")
- 这一版用的是一个笨办法,用一个
vector<int>记录所有字符的出现次数,然后从中提取需要的数字加和。
第二轮
第一版(0/20)
#include <cctype>
#include <iostream>
#include <vector>
using namespace std;
int main() {
string s;
int digit = 0, space = 0, alpha = 0, other = 0;
for(char c : s){
if(isdigit(c)) digit++;
else if(isspace(c)) space++;
else if(isalpha(c)) alpha++;
else other++;
}
cout << alpha << endl;
cout << space << endl;
cout << digit << endl;
cout << other << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
- 经检查,发现犯了一个低级错误——没有写输入语句。
第二版(6/20)
#include <cctype>
#include <iostream>
#include <vector>
using namespace std;
int main() {
string s;
cin >> s;
int digit = 0, space = 0, alpha = 0, other = 0;
for(char c : s){
if(isdigit(c)) digit++;
else if(isspace(c)) space++;
else if(isalpha(c)) alpha++;
else other++;
}
cout << alpha << endl;
cout << space << endl;
cout << digit << endl;
cout << other << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
- 经AI提醒,发现问题出在
cin >> s,这句里的cin是不会输入空格的,必须用getline(cin, s)才行。
第三版(AC)
#include <cctype>
#include <iostream>
#include <vector>
using namespace std;
int main() {
string s;
getline(cin, s);
int digit = 0, space = 0, alpha = 0, other = 0;
for(char c : s){
if(isdigit(c)) digit++;
else if(isspace(c)) space++;
else if(isalpha(c)) alpha++;
else other++;
}
cout << alpha << endl;
cout << space << endl;
cout << digit << endl;
cout << other << endl;
return 0;
}
// 64 位输出请用 printf("%lld")