uva 748 Exponentiation 浮点数乘方运算 高精度水题
输入的前六位数表示一个小数,然后输入一个数表示几次方。要求用高精度算出结果。
高精度水题,主要注意处理小数点,先在输入时把小数点提取出来并记录位置,用普通乘法计算出结果后由后向前计算位置添加小数点。
代码:
#include <cstdio>
#include <cstring> const int maxn = 300; void Mul(char *str1, char *str2, char *str3){
int i, j, i1, i2, tmp, carry, jj;
int len1 = strlen(str1), len2 = strlen(str2);
char ch; jj = carry = 0; for( i1=len1-1; i1 >= 0; --i1 ){
j = jj;
for( i2=len2-1; i2 >= 0; --i2, ++j ){
tmp =
(str3[j]-'0')+(str1[i1]-'0')*(str2[i2]-'0')+carry;
if( tmp > 9 ){
carry = tmp/10; str3[j] = tmp%10+'0';
}
else {
str3[j] = tmp+'0'; carry = 0;
}
}
if( carry ) {
str3[j] = carry+'0'; carry = 0; ++j;
}
++jj;
}
--j;
while( str3[j] == '0' && j > 0 ) --j;
str3[++j] = '\0';
for( i=0, --j; i < j; ++i, --j ){
ch = str3[i]; str3[i] = str3[j]; str3[j] = ch;
}
} int main() {
char num[maxn], res[maxn], t[maxn];
int n;
while (1) {
int p, tmp, i = 0;
while ((tmp = getchar()) != EOF && tmp != ' ')
if (tmp == '.')
p = i;
else{
t[i]= num[i] = tmp;
i++;
}
for (int j = i - 1; j >= 0; j--)
if (num[j] != '0') {
i = j + 1;
break;
}
t[i] = num[i] = '\0';
if (scanf("%d", &n) == EOF)
break;
p = (strlen(num) - p) * n;
getchar();
memset(res, '0', sizeof(res));
for (int i = 0; i < n - 1; i++) {
Mul(num, t, res);
// if (i == n - 1)
// printf("%s %s %s\n", num, t, res);
//printf("hehe\n%s\n", res);
if (i != n - 2)
for (int j = 0; j < maxn; j++) {
t[j] = res[j];
res[j] = '0';
}
}
if (p >= strlen(res)) {
printf(".");
for (int i = 0; i < p - strlen(res); i++)
printf("0");
printf("%s\n", res);
}
else {
int i;
for (i = 0; i < strlen(res) - p; i++)
printf("%c", res[i]);
printf(".");
printf("%s\n", res + strlen(res) - p);
}
}
return 0;
}
uva 748 Exponentiation 浮点数乘方运算 高精度水题的更多相关文章
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- UVA - 748 Exponentiation
Problems involving the computation of exact values of very large magnitude and precision are common. ...
- UVA 699 The Falling Leaves (二叉树水题)
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...
- 高精度水题(POJ2109)
题目链接:http://poj.org/problem?id=2109 double 可以虽然可以表示10^-307~~~10^208,但是精确度只有16位,这个题有bug. #include < ...
- UVa 11040 Add bricks in the wall (水题递推)
题意:给定一个金字塔,除了最后一行,每个数都等于支撑它的两个数的和,现在给奇数行的左数奇数位置,求整个金字塔. 析:很容易看出来,从下往上奇数行等于 a[i][j] = (a[i-2][j-1] - ...
- UVa 699 The Falling Leaves (树水题)
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...
- JS/PHP 浮点数精确运算
php浮点数精确运算 bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(string $left_oper ...
- php浮点数精确运算
php浮点数精确运算 Php: BCMath bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(strin ...
- UVa 1584 Circular Sequence --- 水题
UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...
随机推荐
- Mac 下纯lua(三)
文件处理 直接使用io调用 io.close();文件流关闭 io.flush():如果文件流以bufferd缓存模式处理,输入不会立即存入文件,需要调用本函数 io.input(file):输入 i ...
- 总结一下用caffe跑图片数据的研究流程
近期在用caffe玩一些数据集,这些数据集是从淘宝爬下来的图片.主要是想研究一下对女性衣服的分类. 以下是一些详细的操作流程,这里总结一下. 1 爬取数据.写爬虫从淘宝爬取自己须要的数据. 2 数据预 ...
- Codeforces 474D Flowers dp(水
题目链接:点击打开链接 思路: 给定T k表示T组測试数据 每组case [l,r] 有2种物品a b.b物品必须k个连续出现 问摆成一排后物品长度在[l,r]之间的方法数 思路: dp[i] = d ...
- LFS: Interface eth0 doesn't exist
环境 宿主主机:Ubuntu 14.04.4 LTS 32位 LFS内核:Linux 4.2.0 好不用容易将LFS引导起来了,但系统启动后,无法配置网口.系统启动时提示:Interface eth0 ...
- RMAN完整全备份
1.以CATALOG模式连接到目标数据库和恢复目录(如:目标数据库为ORCL) C:\Users\Administrator>RMAN TARGET / CATALOG RMANCT/RMANC ...
- FlashBack-SCN-TIMESTAMP
一.基于时间(as of timestamp)的flashback1.创建表create table flash_tab(id,vl) as select rownum,oname from ( se ...
- Ffplay视频播放流程
主框架流程 下图是一个使用“gcc+eygpt+graphviz+手工调整”生成的一个ffplay函数基本调用关系图,其中只保留了视频部分,去除了音频处理.字幕处理以及一些细节处理部分. 注:图中的数 ...
- Input输入字体颜色改变js(兼容IE)
从网上找的代码,自己封装了一下(前提:引用jQuery库) 方法1: HTML: <div class="box"> <div class="ipt1& ...
- Mongoose的模糊查询
var Commidity = require("./Model/commiditiesModel"); function search(response,request,key) ...
- org.springframework.beans.factory.BeanCreationException
org.springframework.beans.factory.BeanCreationException 这个是创建bean的异常. 我所遇到的情况是由下面这个引起的: @Resource an ...