题解
2023-06-27 19:33:25
发布于:上海
89阅读
0回复
0点赞
#include <iostream>
#include <string>
using namespace std;
int countOnes(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
string numStr = to_string(i);
for (char c : numStr) {
if (c == '1') {
count++;
}
}
}
return count;
}
int main() {
int n;
cin >> n;
int onesCount = countOnes(n);
cout << onesCount << endl;
return 0;
}
全部评论 1
2024-10-23 来自 湖北
0
有帮助,赞一个