标签:
输入N(1 <= N <= 10^9)
输出包含1的个数
12
5
详解请看大牛博客:http://www.cnblogs.com/jy02414216/archive/2011/03/09/1977724.html
之前一直不明白为什么要循环,原来自己把题解看得太死了,百位是举例子,意思是判断每一个当前的数字都得这么算。。。
1 #include<bits/stdc++.h> 2 #define LL long long 4 using namespace std; 5 LL f(LL n) { 6 LL count = 0; 7 LL i = 1; 8 LL current = 0, after = 0, before = 0; 9 //int t=0; 10 while ((n / i) != 0) { 11 current = (n / i) % 10; 12 before = n / (i * 10); 13 after = n - (n / i) * i; 14 if (current > 1) 15 count = count + (before + 1) * i; 16 else if (current == 0) 17 count = count + before * i; 18 else if (current == 1) 19 count = count + before * i + after + 1; 20 i = i * 10; 21 //printf("t:%d before:%d current:%d after:%d count:%d\n",++t,before,current,after,count); 22 } 23 return count; 24 } 25 int main(){ 26 LL n; 27 cin>>n; 28 cout<<f(n)<<endl; 29 }
标签:
原文地址:http://www.cnblogs.com/zhien-aa/p/5679808.html