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