題:有一個無窮數(shù)列,其通項表示為: a[n]=10*10*... n=0,1,2,3..... 構(gòu)成了1,10,100,1000... 把它連起來,就成了數(shù)串。 判斷數(shù)串的第i位到底是0還是1 .性能要求1s
程序如下:
int i=0;
int total =0, j=1, pos=0; //total:終點 pos: 起點 j:第幾個數(shù)字
//輸入
cout<<"Please input digit:";
cin>>i;
while(1){
pos=total+1;
total+=j;
if(total>=i && i>=pos)
break;
++j;
}
double num2 = pow(10.0,j-1);
int digit = (i==pos)?1:0; //左起相對位置
cout<<" 數(shù)字表示:"<<num2<<endl;
cout<<" 總長度:"<<total-pos+1<<endl;
cout<<" 第"<<i<<"位的數(shù)字:"<<digit<<endl;
system("pause");
return 0;
}
運行結(jié)果:
Please input digit:800
數(shù)字表示:1e+039
總長度:40
第800位的數(shù)字:0
Press any key to continue . . .