http://community.csdn.net/Expert/topic/4211/4211591.xml?temp=.9405329

Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n.

For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?

#include<iostream.h>
/*

int f10(int n){
int r=1;
for(int i=1;i<=n;i++)
r=r*10;
return r;
}

int f(int n){
int r=0;
for(int i=1;i<=n;i++){
int a=1;
do{
if(i%f10(a)/f10(a-1)==1)
r++;
a++;
}while(i/f10(a-1)!=0);
}
return r;
}
*/
/*

int f(int n)
{
int ret = 0;
int ntemp=n;
int ntemp2=1;
int i=1;
while(ntemp)
{
ret += (((ntemp-1)/10)+1) * i;
if( (ntemp%10) <= 1 )
{
ret -= i;
}
if( (ntemp%10) ==1 )
{
ret += ntemp2;
}
ntemp = ntemp/10;
i*=10;
ntemp2 = n%i+1;
}
return ret;
}
*/

int f(int n){
static int r=0;
do{
if(n%10==1) r++;
n=n/10;
}while(n);
return r;
}

int main(){
for(long i=1; i<=1111111110;i++)
if(f(i)==i)
cout<<i<<endl;
cout<<"the end"<<endl;
return 0;
}

#include<iostream.h>

int main(){
for(long i=1; i<=1111111110;i++);
cout<<"the end"<<endl;
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：google的一道面世题:(f(n)==n) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)