迅雷校招------2的N次方

对于一个整数N(512 <= N <= 1024),计算2的N次方并在屏幕显示十进制结果。

输入描述:

输入一个整数N(512 <= N <= 1024)

输出描述:

2的N次方的十进制结果

示例1

输入

512

输出

13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096

题目链接:https://www.nowcoder.com/questionTerminal/e9a4919b8848451d9aff81e3cdd133b1

类似于大数乘除,我接下来就总结一下大数乘除

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

string Nof2(int n)
{
    string res("1");
    int c=0;//进位
    for(int i=0;i<n;i++)
    {
        for(int j=res.length()-1;j>=0;j--)
        {
            int tmp=((res[j]-'0')<<1)+c;
            c=tmp/10;
            res[j]=tmp%10+'0';
        }
        if(c>0)
        {
            res.insert(res.begin(),c+'0');
            c=0;//进位以后,c标志清 000
        }
    }

    return res;
}
int main()
{

   int n;
   while(cin>>n)
   cout<<Nof2(n)<<endl;
    return 0;
}

程序运行结果如下:

猜你喜欢

转载自blog.csdn.net/salmonwilliam/article/details/82911295