题目的链接为: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. yii2 利用小部件生成后台左边菜单栏

    ************   模型层递归查询权限   ************ /**     * 递归方式查询权限     */    public function getPrivilege()  ...

  2. HDU 5009

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 题意:一个数列,每个点代表一种颜色,每次选一个区间覆盖,覆盖的代价是区间内颜色种类数的平方,直到覆盖整个数 ...

  3. Weblogic项目部署及数据源配置

    号线项目总结: weblogic项目配置: 把工作区放到磁盘上,建立weblogic域 在安装的weblogic服务器的开始项里选择 进入weblogic域的建立. 建立完域后用Myeclipse打开 ...

  4. 一直纠结中的"底层模板"含义(借鉴)

    无意间看到这个解释,推荐给哪些和我一样迷惑的人!

  5. css中常用的hack

    <!DOCTYPE html> <html> <head> <title>Css Hack</title> <style> #t ...

  6. 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境

    做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...

  7. Linux下删除乱码或特殊字符文件

    今天遇到一个问题,一个文件名是“-MXV9.log”,直接用rm删除的时候就报错,如下: [localhost]rm -MXV9.log rm: illegal option -- M rm: ill ...

  8. c#部分---好题--顺便练练“类的知识”

    练习:判断邮箱格式是否正确 //1.有且只能有一个@ //2.不能以@开头 //3.@之后至少有一个. //4.@和.不能靠在一起 //5.不能以.结尾

  9. 使用Spring + Jedis集成Redis

    转自:http://my.oschina.net/u/866380/blog/521658 摘要 使用Spring和Jedis完成分片Redis的集成 一.集成环境 Tomcat7 JDK1.7 Je ...

  10. php下载c

    1.php下载中,不认识的类型比如zip,rar,rmvb等可以直接点击链接下载<a href="one.zip">one.zip</a>. 2.php下载 ...