1049. Counting Ones (30)

来源:互联网 发布:淘宝上的食品能买吗 编辑:程序博客网 时间:2024/06/05 06:45

从个位开始分别算出从1到n这个位有多少的1

如果这位大于1,那么1的总数等于(左边的值+1)*(右边的位数)

如果这位等于1,那么1的总数等于(左边的值)*(右边的位数)+右边的值

如果这位等于0,那么1的总数等于(左边的值)*(右边的位数)

#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <map>#include <stack>#include <queue>#include <algorithm>#include <set>using namespace std;int c,base;int main(){    int n;    scanf("%d",&n);    c=0;base=1;    while(n/base!=0){        int low=n%base;        int now=(n/base)%10;        int high=n/(base*10);        if(now==0)            c+=high*base;        else if(now==1)            c+=high*base+low+1;        else            c+=(high+1)*base;        base*=10;    }    printf("%d\n",c);    return 0;}



0 0
原创粉丝点击