c++警告只用iostream库并用string类型输入一个整数n统计数字n中有多少个0多少个1多少个2并分别把统计的结果输出。【输入】一个整数n。【输出】三个整数分别表示数字n中0的个数1的个数2的个数每个数字之间用空格隔开。【输入样例1】20200115【输出样例1】3 2 2【输入样例2】9202【输出样例2】1 0 2
#include <iostream>
#include <string>
int main() {
std::string n;
std::cin >> n;
int count0 = 0;
int count1 = 0;
int count2 = 0;
for (int i = 0; i < n.length(); i++) {
if (n[i] == '0') {
count0++;
} else if (n[i] == '1') {
count1++;
} else if (n[i] == '2') {
count2++;
}
}
std::cout << count0 << " " << count1 << " " << count2 << std::endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/igwB 著作权归作者所有。请勿转载和采集!