1到N中1出现的个数

来源:互联网 发布:软件研发流程 编辑:程序博客网 时间:2024/05/21 10:27
#include <iostream>using namespace std;int count_one(int n){    if(n<1)       return 0;    int round = n;    int base = 1;    int count = 0;    while(round>0)    {       int weight = round%10;       round/=10;       //count+=round;       if(weight==1)          count+=round*base+(n%10)+1;       else if(weight>1)          count+=round*base+base;        else if(weight==0)            count+=round*base;       base*=10;    }    return count;}/*    若weight为0,则1出现次数为round*base    若weight为1,则1出现次数为round*base+former+1    若weight大于1,则1出现次数为rount*base+base*/int main(){    int n;    while(cin>>n)    {        cout<<count_one(n)<<endl;    }    return 0;}
原创粉丝点击