题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/4ec4325634634193a7cd6798037697a8
定义string变量,想要读入空格,可以用getline函数,参数为(cin,变量)
#include <iostream>
using namespace std;
#include <string>
int main() {
string str1,str2;
while (getline(cin,str1) && str1[0] != '#') {
int count;
getline(cin, str2);
for (int i = 0; i < str1.size(); i++){
count = 0;
for (int j = 0; j < str2.size(); j++)
if (str1[i] == str2[j])
count++;
cout << str1[i] << " "<< count << endl; //按格式输出
}
}
}
// 64 位输出请用 printf("%lld")

查看15道真题和解析