2的N次方
/**编程精确计算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次方的更多相关文章
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- 剑指Offer面试题:10.数值的整数次方
一.题目:数值的整数次方 题目:实现函数double Power(doublebase, int exponent),求base的exponent次方.不得使用库函数,同时不需要考虑大数问题. 在.N ...
- GDUFE-OJ 1203x的y次方的最后三位数 快速幂
嘿嘿今天学了快速幂也~~ Problem Description: 求x的y次方的最后三位数 . Input: 一个两位数x和一个两位数y. Output: 输出x的y次方的后三位数. Sample ...
- 《剑指offer》面试题11: 数值的整数次方
面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...
- 打出10的n次方,上标,下标等处理方法(mac)
我使用mac系统遇到的需求,需要在项目中显示10的6次方 用来做单位,找了很多方案,word等文本编辑工具很好实现(word是使用ctrl + shift + =)(mac 版的word是 Comm ...
- 计算2的N次方&&计算e
2的N次方 注意:这里在处理的时候并没有用循环来处理,而是用移位的做法. n<<4 就是 n*2^4 ,所以在本例中只需要写 1<<time (time是要求的 ...
随机推荐
- Redis Desktop Manager桌面管理工具
Redis Desktop Manager桌面管理工具,方便管理我们放在redis中的各个缓存 及16个数据库 http://redisdesktop.com/download
- hdu4488 Faulhaber’s Triangle(模拟题)
Faulhaber’s Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Introspector(内省)简单演示样例 与 简单应用
简单演示样例: package com.asdfLeftHand.test; import java.beans.BeanDescriptor; import java.beans.BeanInfo; ...
- C++ Virtual介绍 分类: C/C++ 2015-06-16 21:36 26人阅读 评论(0) 收藏
参考链接:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html 学过C++的人都知道在类Base中加了Virtual关键字的函 ...
- MYSQL 系统命令 源码定位
sql_cmd.h enum enum_sql_command { SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_AL ...
- 以色列学生---debugger 构建
http://eli.thegreenplace.net/2011/01/23/how-debuggers-work-part-1 http://eli.thegreenplace.net/
- mysql wait_timeout和interactive_timeout总结
(1)interactive_timeout:参数含义:服务器关闭交互式连接前等待活动的秒数.交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE ...
- ATL 工程下添加右击菜单
首先在dllmain.cpp中添加如下声明 HINSTANCE g_hInstance; g_hInstance = hInstance; 源码如下: CPoint point; ::GetCur ...
- WS_CLIPCHILDREN和WS_CLIPSIBLINGS的理解(转载)
1.1 WS_CLIPCHILDREN WS_CLIPCHILDREN样式从字面上可以理解成ClipChildren,裁减子窗口. MSDN里的E文解释:Excludes the area occup ...
- KALI ssh无法登陆的解决办法
应该是sshd的设置不允许root用户用密码远程登录 修改 vim /etc/ssh/sshd_config 找到# Authentication:LoginGraceTime 120PermitRo ...