/**编程精确计算2的N次方。(N是介于100和1000之间的整数)*/
/*问题代码:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
long int c;
a=2;
scanf("%d",&b);
c=pow(a,b);
printf("%d",c);
return 0;
}
很明显,这个不能直接算...要用数组模拟计算和进位...是属于大数处理题目的一种。
我们用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=0;
for(int i=0;i<MAXNUM;i++)
{
arr[i]=0;
}
arr[0]=2;
for( i=1;i<n;i++)
{
int jinwei=0;
for(int j=0;j<=index;j++)
{
int temp=1;
if(j==0)
{
temp=arr[j]*2;
if(temp>=10)
{
jinwei=1;
}
}
else
{
temp=arr[j]*2;
if(jinwei==1)
{
temp=temp+1;
}
if(temp>=10)
{
jinwei=1;
}
else
{
jinwei=0;
}
}
arr[j]=temp%10;
}
if(jinwei==1)
{
index++;
arr[index]=1;
}
}
for( i=index;i>=0;i--)
{
cout<<arr[i];
}
cout<<endl;

system("pause");
return 0;
}

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. Java工作队列和线程池

    背景    最近的需要做一个与设备通信的web项目.当然,我们需要写好与设备之间的通信协议(socket).大致的时序逻辑时:当用户用浏览器点击页面某一控件后,它就向后台发送一个post请求,后台解析 ...

  2. spring boot项目配置文件集合

    表 1. Spring Boot 推荐的基础 POM 文件 名称 说明 spring-boot-starter 核心 POM,包含自动配置支持.日志库和对 YAML 配置文件的支持. spring-b ...

  3. mysql2redis

    mysql2redis这个项目主要解决mysql数据跟redis数据同步的问题 目前在测试环境研究这方面的应用,以下是git上面的介绍 git入口    git安装入口 Dependencies pl ...

  4. js获取几个月前,几周前时间。

    /**  *  DK 命名空间  防止全局变量污染  */ var DK = {} ; /**  * 获取前几个月,默认为一个月,当前时间  * @author duke  * @date 格式为yy ...

  5. Memcached笔记——(四)应对高并发攻击【转】

    http://snowolf.iteye.com/blog/1677495 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...

  6. ios6如何处理内存,分别为前警告后

    这里有一篇文章.非常具体地说明了ios6前后是怎样处理内存警告的: 来自唐巧的技术博客:http://blog.devtang.com/blog/2013/05/18/goodbye-viewdidu ...

  7. C++之枚举

    1. 声明枚举类型格式 enum Day{ Mon,Tue=5,Wed};//Mon=0;Tue=5;Wed=6 enumDay1{Mon1,Tue1,Wed1};//Mon1=0;Tue1=1;We ...

  8. struts2标签汇总

    要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码:<%@taglib prefix="s" uri="/struts- ...

  9. (转载)myeclipse项目名称重命名

    myeclipse项目名称重命名 实例1 今天晚上在做一个jsp唱片显示的实例,myeclipse项目名称原本想写music结果写成了musci.这就需要项目名称的重命名,单纯的使用 “重构--> ...

  10. LAMP环境部署总结

    linux+apche+mysql+php 此次用到的工具有:/etc/init.d/iptables , selinux, useradd, yum,chkconfig,sshd,visudo,cr ...