身上无钱你莫邪

moye's c++ blog

C++求解数串中第N位数字表示

题:有一个无穷数列,其通项表示为: 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>=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 . . .

 


 

posted on 2008-12-19 13:36 莫耶 阅读(508) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

常用链接

留言簿(3)

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜