2的N次方 【转】
题目的链接为: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次方 【转】的更多相关文章
- [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是要求的 ...
随机推荐
- JAVA的数组和输入
package com.java1995; import java.util.Scanner; public class Array { public static void main(String[ ...
- C++ algorithm 里的sort函数应用
MSDN中的定义: template<class RanIt> void sort(RanIt first, RanIt last); //--> 1)template< ...
- ASP.NET状态管理之四(暂存状态HttpContext.Items)
ASP.NET提供一个类System.Web.HttpContext ,用来表示上下文,此对象有一个属性Items 暂存状态就是利用HttpContext.Items属性来存放数据 MSDN中Http ...
- C#不安全代码和stackalloc
stackalloc 关键字用于不安全的代码上下文中,以便在堆栈上分配内存块.如下: ]; 注意:关键字仅在局部变量的初始值中有效. 下面的代码导致编译器错误. int* block; // The ...
- PHP date和time
一.time()函数 time():得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒. 前一天的时间就是 time()-60*60*24. 前一年的时间就是 time()-60*60 ...
- mysql的ERROR:1042
在虚拟机上测试数据库备份功能,需要连接外部机器上的mysql,pdo总是报超时错误! 起初认为是用的mysql账号的域不匹配!后来发现不是因为这个! 在终端中用mysql命令尝试连接,发现返回的错误是 ...
- magento -- 给后台分类管理页的分类商品加一栏商品类型
当使用特定分类来控制前台的商品显示时,后台分类管理页的分类商品只有编号.名称.SKU和价格这几栏,选择特定商品相当不便. 可以在这里多加一栏商品类型用来筛选商品,添加的方式很简单. 打开文件/app/ ...
- Centos 6.4 8250/16550 只生成了4个串口
/********************************************************************* * Centos 6.4 8250/16550 只生成了4 ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- mac自带apache服务器开启
mac的os x操作系统自带的有apach服务器, 命令行: sudo apachectl -v 可查看自带apache版本信息 输入: sudo apachectl start 就开启了apa ...