统计1n之间的数字里1出现的个数——1477

http://192.168.7.42/problemDetail.aspx?pid=1477

前打表找规律

后来对数字是一的进行统计

如103

对最高位开始统计

若为0 ,则不处理

若该位为1则总数加上后面的数:加上以1*a[i](相应表中数),03+1

若为大于1的数 则乘以该数的最高位(如203 则 加上以2*a[i](相应表中数),再加上03+1,再加上100)

再到低位执行直到个位数

View Code
#include<stdio.h>
#include
<math.h>

int main()
{
int a[10]={0,1,20,300,4000,50000,600000,7000000,80000000};
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
int add=0;
int b[11],c[11];

int temp=n,all=0;
while(temp)
{
b[add]
=temp%10;
temp
/=10;
add
++;
}

for(i=0;i<add;i++)
{
all
+=b[i]*pow(10,i);
c[i]
=all;
}

int max=0;
for(i=add-1;i>=0;i--)
{
if(b[i]==0)
continue;
temp
=b[i];

max
+=temp*a[i];
if(temp>=2) max+=(int)pow(10,i);
if(temp==1)
{
if(i==0)
max
+=1;
else
max
+=c[i-1]+1;
}

}

printf(
"%d\n",max);
}
return 0;
}
原文地址:https://www.cnblogs.com/huhuuu/p/1984215.html