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是要求的 ...
随机推荐
- javaweb-dbcp2
package cn.itcast.utils; import java.io.InputStream;import java.sql.Connection;import java.sql.Drive ...
- BZOJ 1600 建造栅栏
O(N)分成1,2与3,4两部分搞一搞. #include<iostream> #include<cstdio> #include<cstring> #includ ...
- 【LeetCode OJ】LRU Cache
Problem Link: http://oj.leetcode.com/problems/lru-cache/ Long long ago, I had a post for implementin ...
- # 20145210 《Java程序设计》第05周学习总结
教材学习内容总结 第八章 异常处理 8.1语法与继承架构 •使用 try.catch •Java中所有信息都会被打包为对象,如果愿意,可以尝试(try)捕捉(catch)代表错误的对象后做一些处理 • ...
- Bad Hair Day_单调栈
Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow ...
- GridView按钮事件
1.html代码 <asp:TemplateField HeaderText="操作"> <ItemTemplate> <div style=&quo ...
- 从数学角度看最大期望(EM)算法 I
[转载请注明出处]http://www.cnblogs.com/mashiqi 2014/11/18 更新.发现以前的公式(2)里有错误,现已改过来.由于这几天和Can讨论了EM算法,回头看我以前写的 ...
- 一秒钟Win7笔记本变无线路由器
开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网,节省网费和路由器购买费.亲测通过,比conncetify方便,稳定,网速好! ...
- [virsh] error: unknown OS type hvm解决办法
今天在linux服务器上编译安装升级了下qemu,升级命令如下: root@ubuntu:/opt/qemu-# ./configure --prefix=/usr/local/ --target-l ...
- Java ArrayList的使用方法
首先ArrayList的一个简单实例: package chapter11; import java.util.ArrayList; public class TestArrayList { publ ...