#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#include <string>
int fun(int N);
int main()
{
for(unsigned int k=400000; k>1; k--)
{
if(fun(k) == k)
{
cout<<"the max of n is:"<<k<<endl;
return 0;
}
}
return 0;
}
//统计从1到N的连续数字中,所有的1出现的次数
//input:25
//output:13
int fun(int N)
{
int count = 0;
int low, cur, high;
int fac = 1;
while(N/fac)
{
low = N - (N/fac)*fac;
cur = (N/fac) % 10;
high = N/(fac*10);
switch(cur)
{
case 0:
count += high * fac;
break;
case 1:
count += high * fac + low + 1;
break;
default :
count += (high + 1)* fac;
break;
}
fac *= 10;
}
return count;
}
统计1到N中1的个数,并找出满足飞fun(n)=n的最大n值
最新推荐文章于 2019-08-24 17:07:03 发布