51nod1009(1的数目)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009

 

题意:中文题诶~

 

思路:分别考虑各个数位上出现1的次数,再求和就好了..

注意当前数位上数字为0, 1, 2~9三种情况的计数方法略有不同,想到这里其他的细枝末节就简单了啦...

 

代码:

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 int main(void){
 6     ll n, ans=0, gg=1;
 7     cin >> n;
 8     ll t=n;
 9     while(t){
10         ll cnt=t%10;
11         ll xy=n/(gg*10);
12         if(cnt==0){  //当前数字为0的情况
13             ans+=xy*gg;
14         }else if(cnt==1){ //当前数字为1的情况
15             ans+=xy*gg;
16             ans+=n%gg+1;
17         }else{
18             ans+=(xy+1)*gg; //当前数字为2~9的情况
19         }
20         gg*=10;
21         t/=10;
22     }
23     cout << ans << endl;
24     return 0;
25 }

 

  

posted @ 2017-01-09 12:13  geloutingyu  阅读(224)  评论(0编辑  收藏  举报