题目描述:
给定一个整数n,返回从1到n的数字钟1出现的个数。
例如 n=5,1~n为1,2,3,4,5所以1出现的次数为1,返回1;
n=11,1~11为1,2,3,4,5,6,7,8,9,10,11。所以1出现的次数是4,返回4。
方法一:暴力枚举
代码
#include <iostream>
using namespace std;
int main(){
int n,res=0;
cin >> n;
for(int i=1; i<=n; i++){
int t = i;
while(t){
if(t%10 == 1)
res++;
t /= 10;
}
}<