题目的链接为:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1009
题目为:
2的N次方
时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte
描述
编程精确计算2的N次方。(N是介于100和1000之间的整数)。
输入
正整数N (100≤N≤1000)
输出
2的N次方
样例输入
200
样例输出
1606938044258990275541962092341162602522202993782792835301376

很明显,这个不能直接算...要用数组模拟计算和进位...是属于大数处理题目的一种。
我们用arr这个长度为2000的数组来存储结果。
对于每次乘2,从最后一位开始,乘2,如果有进位,那么此位的值为乘积%10;前面的数,乘2后,判断后面是否有进位,有进位那么加1,再判断此位是否有进位。
从后位遍历到第一位,即可以得到最终结果。
代码如下:

 #include<iostream>
#define MAXNUM 2000
using namespace std;
int arr[MAXNUM];
int main()
{
int n,index;
cin>>n;
//大数乘法
index=;
for(int i=;i<MAXNUM;i++)
{
arr[i]=;
}
arr[]=;
for(int i=;i<n;i++)
{
int jinwei=;
for(int j=;j<=index;j++)
{
int temp=;
if(j==)
{
temp=arr[j]*;
if(temp>=)
{
jinwei=;
}
}
else
{
temp=arr[j]*;
if(jinwei==)
{
temp=temp+;
}
if(temp>=)
{
jinwei=;
}
else
{
jinwei=;
}
}
arr[j]=temp%;
}
if(jinwei==)
{
index++;
arr[index]=;
}
}
for(int i=index;i>=;i--)
{
cout<<arr[i];
}
cout<<endl; system("pause");
return ;
}

原文链接:http://blog.csdn.net/rongyongfeikai2/article/details/7588295

2的N次方 【转】的更多相关文章

  1. [LeetCode] Super Pow 超级次方

    Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...

  2. [LeetCode] Power of Four 判断4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  3. [LeetCode] Power of Three 判断3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

  4. [LeetCode] Power of Two 判断2的次方数

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  5. [LeetCode] Pow(x, n) 求x的n次方

    Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...

  6. 剑指Offer面试题:10.数值的整数次方

    一.题目:数值的整数次方 题目:实现函数double Power(doublebase, int exponent),求base的exponent次方.不得使用库函数,同时不需要考虑大数问题. 在.N ...

  7. GDUFE-OJ 1203x的y次方的最后三位数 快速幂

    嘿嘿今天学了快速幂也~~ Problem Description: 求x的y次方的最后三位数 . Input: 一个两位数x和一个两位数y. Output: 输出x的y次方的后三位数. Sample ...

  8. 《剑指offer》面试题11: 数值的整数次方

    面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...

  9. 打出10的n次方,上标,下标等处理方法(mac)

    我使用mac系统遇到的需求,需要在项目中显示10的6次方 用来做单位,找了很多方案,word等文本编辑工具很好实现(word是使用ctrl + shift + =)(mac  版的word是 Comm ...

  10. 计算2的N次方&&计算e

    2的N次方 注意:这里在处理的时候并没有用循环来处理,而是用移位的做法.    n<<4  就是 n*2^4    ,所以在本例中只需要写 1<<time  (time是要求的 ...

随机推荐

  1. php大力力 [024节]PHP中的字符串连接操作(2015-08-27)

    2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作  阅读:次   时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...

  2. du -sh 目录名称 查看目录大小

    du -sh 目录名称 查看目录大小 df -h 查看磁盘使用情况

  3. AD管理命令

    repadmin /showrepl   显示复制协议 dcdiag /test:dns

  4. HDU 4971 (最小割)

    Problem A simple brute force problem (HDU 4971) 题目大意 有n个项目和m个问题,完成每个项目有对应收入,解决每个问题需要对应花费,给出每个项目需解决的问 ...

  5. Gmail邮箱添加域名解析

    主机记录  MX   服务器地址 优先级@  MX   ASPMX.L.GOOGLE.COM. 10@  MX   ALT1.ASPMX.L.GOOGLE.COM. 20@  MX   ALT2.AS ...

  6. oracle疑难杂症问题

    在虚拟机中安装了oracle10g,由于虚拟机的空间有限,看到磁盘空间快没了,就手贱把oracle目录中的空文件夹(E:\oracle\product\10.2.0\flash_recovery_ar ...

  7. Difference Between Performance Testing, Load Testing and Stress Testing

    http://www.softwaretestinghelp.com/what-is-performance-testing-load-testing-stress-testing/ Differen ...

  8. CMAKE的用法

    一.      基本使用 安装:下载二进制包后可直接解压使用 从源码安装则执行命令:./bootstrap; make; make install——尝试执行bootstrap失败 使用:cmake ...

  9. fsutil

    编号:1035时间:2016年8月29日15:41:57功能:fsutil栗子:fsutil file createnew e:\b.txt 1073741824 //创建1G文件http://www ...

  10. tensorflow0.8.0 安装配置

    参考官网:https://www.tensorflow.org/  Ubuntu15.10 + Eclipse Mars.2(4.5.2)官网最新 + Anaconda3-4.0.0 + Pydev4 ...