描述试计算在区间 1 到 n的所有整数中数字x0≤x≤9共出现了多少次?例如在 1到11中即在 1234567891011 中数字 1 出现了 4 次。输入描述2个整数nx之间用一个空格隔开。用c++做输出描述1个整数表示x出现的次数。用例输入 1 00010100 1用例输出 1 2用例输入 2 11111111 1用例输出 2 8
#include
int main() { string num; int x; cin >> num >> x; int count = 0; for (int i = 0; i < num.length(); i++) { if (num[i] - '0' == x) { count++; } } cout << count << endl; return 0; }
原文地址: https://www.cveoy.top/t/topic/i93V 著作权归作者所有。请勿转载和采集!