题目
The task is simple: given any positive integer N, you are supposed to count the total number of 1’s in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1’s in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (<=230).
Output Specification:
For each test case, print the number of 1’s in one line.
Sample Input:
12
Sample Output:
5
解答,会超时
#直接检查每一个数值中1的个数
N=int(input())
sum=0
for i in range(N+1):
si=str(i)
sir=si.replace('1','')
sum+=len(si)-len(sir)
print (sum)

本文介绍了一种计算从1到给定正整数N范围内所有整数中数字1出现总次数的方法。通过逐个检查每个整数并统计1的出现次数来实现。输入为一个正整数N,输出则是在1至N之间所有数字中1的总数。


被折叠的 条评论
为什么被折叠?



