http://noi.openjudge.cn/ch0105/40/
#include <iostream>
#include <iomanip>
using namespace std;
int main( void )
{
int n, temp, value=0;
cin >> n;
for (int i=1;i<=n;i++)
{
temp = i;
while (temp > 0){
if (temp%10 == 1) value++;
temp /= 10;
}
}
cout << value;
return 0;
}
python代码
"""
1.5编程基础之循环控制 40 数1的个数
http://noi.openjudge.cn/ch0105/41/
"""
print(sum([str(i+1).count("1")for i in range(int(input()))]))