#include <iostream>
using namespace std;
long count1sOfInteger(int num)
{
long res = 0;
int factor = 1;
long high, cur, low;
while(num / factor)
{
low = num - (num/factor)*factor;//num % factor;
cur = (num/factor) % 10;
high = (num/factor) / 10;
switch(cur)
{
case 0:
res += (high*factor);
break;
case 1:
res += (high*factor) + (low+1);
break;
default:
res += (high+1) * factor;
break;
}
factor *= 10;
}
return res;
}
int main()
{
int num;
while(cin >> num)
{
int count = count1sOfInteger(num);
cout << count << endl;
}
//cout << "Hello world!" << endl;
return 0;
}
count the number of 1 in a decimal integer
最新推荐文章于 2020-12-02 16:01:00 发布