Problem E: 单身狗
Time Limit: 1 Sec Memory Limit: 128 MBDescription
给定N,统计[1,N]中所有数字1出现的次数。
Input
第一行为一个整数T,表示测试用例的数量。
每个测试用例包含一个数字N。
数据范围:T<=100,1<=N<=1e9。
Output
对于每组测试用例,输出[1,N]中所有数字1出现的次数。
Sample Input
3120100
Sample Output
11221
HINT
#include<stdio.h>
#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
using namespace std;
long getsum(long n)
{
long low=0;//低位
long high=0;//高位
long curren=1;//位数
long cnt=0;
long now=0;//当前位
if(n<=0) return 0;
while(n/curren>0)
{
//cout<<"n="<<n<<endl;
low = n-(n/curren)*curren;
high = n/(curren*10);
now = (n/curren)%10;
//printf("high=%ld low=%ld now%ld\n",high,low,now);
if(now==0)//如果当前位数为0,只与高位有关
{
cnt+=high*curren;
}
if(now==1)//如果当前位数为1,高位*位数+低位+1;如113,当前为2; 10 + 3(11+12+13+10);
{
cnt+=high*curren+low+1;
}
else if(now>1)//如果大于1 只与高位有关,123 当前为十位,(高位+ 1 )*位数;
{
cnt+=(high+1)*curren;
}
curren*=10;//更新位数;
}
return cnt;
}
int main()
{
int t;
while(cin>>t)
{
char ch=getchar();
while(t--)
{
long ac;
cin>>ac;
cout<<getsum(ac)<<endl;
}
}
return 0;
}