首页 > 其他 > 详细

计算2的任意次方[通用版]

时间:2016-03-20 11:45:04      阅读:149      评论:0      收藏:0      [点我收藏+]

位数计算: [ N*log2 ] +1

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int Bits,Tp=0,n,j,power_value;
    double y=2;
    double x=log10(y);
    cin>>power_value;
    Bits=(int)(power_value*x+1);//Bits为位数;
    int *a=(int *)malloc(Bits*sizeof(int));//动态数组(Bits个整型元素)
    for(j=0;j<Bits;j++)//动态数组全部置0;
    a[j]=0;
    a[Bits-1]=1;//末尾置1,准备乘2;
    for(j=0;j<power_value;j++) //外层循环,power_value次方;
        for(n=(Bits-1);n>=0;n--)//内层循环,Bits次相乘;
            {
                a[n]=a[n]*2+Tp;
                Tp=a[n]/10;//得到进位值Tp;
                a[n]=a[n]%10;               
            }
    for(n=0;n<Bits;n++) //输出结果;
        cout<<a[n]; 
    return 0;
}

 

计算2的任意次方[通用版]

原文:http://www.cnblogs.com/tinaluo/p/5297258.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!