[POJ] #1001# Exponentiation : 大数乘法
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 156373 | Accepted: 38086 |
Description
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Input
input will consist of a set of pairs of values for R and n. The R value
will occupy columns 1 through 6, and the n value will be in columns 8
and 9.
Output
output will consist of one line for each line of input giving the exact
value of R^n. Leading zeros should be suppressed in the output.
Insignificant trailing zeros must not be printed. Don't print the
decimal point if the result is an integer.
Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
Hint
s is a string and n is an integer
C++
while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work */
{
...
}
Source
- 给出字符串表示的浮点数 和 n次方数
- 输出其n次方运算后的值
- 算法核心:
- 大数乘法:利用程序模拟算数运算过程,算出乘积
- 实现细节:
- 将输入数字从低位到高位反转存储,计算时方便进位
- 模拟计算时,去除小数点,统一按照大整数相乘
- 输出时逆序输出,同时计算小数点位置,相应位输出小数点
#include <stdio.h>
#include <memory.h> #define MAXN 100
int a[MAXN], b[MAXN], len_a, len_b; /* 模拟算数运算过程,算出乘积 */
void multiply(int *a, int *b)
{
int i, j, len, c[MAXN]; memset(c, , sizeof(c)); for (i = ; a[i] != -; i++)
for (j = ; b[j] != -; j++)
c[i + j] += a[i] * b[j]; len = i + j - ; for (i = ; i < len; i++)
if (c[i] >= ) { c[i + ] += c[i] / ; c[i] = c[i] % ;} if (c[len] != ) len_b = len + ;
else len_b = len; for (i = ; i < len_b; i++) b[i] = c[i];
} void main()
{
char str[MAXN];
int i, j, exp;
int st, pt, ed; freopen( "input.txt", "r" , stdin); while (scanf("%s %d", str, &exp) != EOF) {
memset(a, -, sizeof(a));
memset(b, -, sizeof(b));
len_a = ;
st = pt = ed = -;
/*
1.st为输入数字的启始位置
2.pt为输入数字小数点的位置
3.ed为输入数字的结束位置
4.此三个中间变量的作用:
4.1 去掉开头和结尾的 0
4.2 对于小数点的特殊处理
4.3 用于计算出乘积后小数点的正确位置
*/
for (i = ; str[i] != '\0'; i++) {
len_a++; if (str[i] != '' && str[i] != '.' && st == -) st = i;
if (str[i] != '' && str[i] != '.' || (str[i] != '.' && pt == -)) ed = i;
if (str[i] == '.') pt = i;
} for (i = ed, j = ; i >= st; i--) {
if (str[i] == '.') continue; b[j] = a[j] = str[i] - '';
j++;
} len_b = len_a = j;
for(i = ; i < exp; i++) multiply(a, b); if (pt < st) {
printf(".");
for (i = ; i < (ed - pt) * exp - len_b; i++) printf("");
} for (i = len_b - ; i >= ; i--) {
if (pt > st && pt < ed && i == (ed - pt) * exp - ) printf(".");
printf("%d", b[i]);
} printf("\n");
}
}
[POJ] #1001# Exponentiation : 大数乘法的更多相关文章
- POJ 1001 Exponentiation(大数运算)
POJ 1001 Exponentiation 时限:500 ms 内存限制:10000 K 提交材料共计: 179923 接受: 43369 描述:求得数R( 0.0 < R < ...
- [POJ 1001] Exponentiation C++解题报告 JAVA解题报告
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 126980 Accepted: 30 ...
- POJ 1001 Exponentiation 无限大数的指数乘法 题解
POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. ...
- POJ 1001 Exponentiation
题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...
- poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 138526 Accepted: 33859 ...
- POJ 1001 Exponentiation(JAVA,BigDecimal->String)
题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static voi ...
- POJ 1001 Exponentiation 模拟小数幂
模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...
- Project Euler 16 Power digit sum( 大数乘法 )
题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...
- 51nod 1027大数乘法
题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...
随机推荐
- Android Handler使用实例
本文主要介绍Android中Handler的简单使用方法,Handler跟多线程,消息队列联系很紧密,在平常的实际程序开发中比较常见.本文分为4个简单的例子来学校handler Handler使用例1 ...
- Android ActionBar标题和渐变背景
需要在AndroidManifest.xml中设置 android:theme="@style/Theme.AppCompat" 如果提示找不到,请按下图设置: 至于如何引入的方法 ...
- [前端]利用a标签获取url里所需的内容
function parseURL(url) { var a = document.createElement('a'); a.href = url; return { source: url, pr ...
- makefile的常用规则
一.前言 这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将会学到以下内容: 源程序编译 Makefile的编写 程序库的链接 程序的调试 头文件和系统求助 二.正文 1 ...
- zlib用法说明
1. 如何获得zlib zlib的主页是:http://www.zlib.net/ 2. 用VC++6.0打开 把 下载的源代码解压打开,VC6.0的工程已经建好了,在\projects\visual ...
- 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)
题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...
- tomcat部署两个相同的项目报错不能访问
需要在同一个tomcat上搭建一个项目的两个版本,都要能跑起来 直接复制两个项目部署,会出现两个错误: 1,webAppKey 冲突 2,tomcat启动会有内存溢出(OutOfMemoryErr ...
- bzoj1355: [Baltic2009]Radio Transmission
将原串看成是循环节的后缀加上若干个循环节,那么考虑每种情况都会发现n-next[n]就是最小循环节.(一开始总输出n...然后发现build_next连调用都没有,%%% #include<cs ...
- LICEcap 简洁易用的动画屏幕录制软件
LICEcap 简洁易用的动画屏幕录制软件 LICEcap 捕捉屏幕的区域并保存为gif动画(便于网络发布)或lcf格式(见下). LICEcap 直观易用,功能灵活,支持 Windows 和 OSX ...
- asp.net输出docx文档出现【文件已损坏 无法打开】问题的解决方案
在某个项目中,有个需求需要将一些附件文档以字节流的形式直接存储在数据库中. 功能实现后,尝试过很多格式文件的上传下载处理,均未发现问题, 唯独在下载docx格式文件后,一打开文件就提示: “无法打开文 ...