#include <iostream>
using namespace std;
int numberOf1sBetween1andN(int n)
{
int lowerNum=0;
int currentNum=0;
int higherNum=0;
int iFactor=1;
int iCount=0;
while(n/iFactor)
{
lowerNum=n%iFactor;
currentNum=(n-lowerNum)/iFactor%10;
higherNum=n/iFactor/10;
switch(currentNum)
{
case 0:
iCount+=higherNum*iFactor;
break;
case 1:
iCount+=higherNum*iFactor+lowerNum+1;
break;
default:
iCount+=(higherNum+1)*iFactor;
break;
}
iFactor*=10;
}
return iCount;
}
void main()
{cout<<numberOf1sBetween1andN(123)<<endl;;
}
求1到n这n个整数的十进制表示中1出现的次数
最新推荐文章于 2022-01-18 18:07:41 发布