输入的前六位数表示一个小数,然后输入一个数表示几次方。要求用高精度算出结果。

高精度水题,主要注意处理小数点,先在输入时把小数点提取出来并记录位置,用普通乘法计算出结果后由后向前计算位置添加小数点。

代码:

#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 浮点数乘方运算 高精度水题的更多相关文章

  1. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

  2. UVA - 748 Exponentiation

    Problems involving the computation of exact values of very large magnitude and precision are common. ...

  3. UVA 699 The Falling Leaves (二叉树水题)

    本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...

  4. 高精度水题(POJ2109)

    题目链接:http://poj.org/problem?id=2109 double 可以虽然可以表示10^-307~~~10^208,但是精确度只有16位,这个题有bug. #include < ...

  5. UVa 11040 Add bricks in the wall (水题递推)

    题意:给定一个金字塔,除了最后一行,每个数都等于支撑它的两个数的和,现在给奇数行的左数奇数位置,求整个金字塔. 析:很容易看出来,从下往上奇数行等于 a[i][j] = (a[i-2][j-1] - ...

  6. UVa 699 The Falling Leaves (树水题)

    Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...

  7. JS/PHP 浮点数精确运算

    php浮点数精确运算 bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(string $left_oper ...

  8. php浮点数精确运算

    php浮点数精确运算 Php: BCMath bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(strin ...

  9. UVa 1584 Circular Sequence --- 水题

    UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...

随机推荐

  1. Ajax跨域访问解决办法

    方法1. jsonp实现ajax跨域访问示例 jsp代码: <body> <input type="button" onclick="testJsonp ...

  2. 数据库的优化tips

    数据库   TIPS:: 1.用于记录或者是数据分析的表创建时::使用Id作为主键,1,2,3...表示消息条数.用户账号id用于做外键.一个用户相应唯一个accountId             ...

  3. Android 内部存储相关的函数(getCacheDir,getDir, getFileStreamPath,getFilesDir,openFileInput, ...)

    为了保证应用程序存储数据的安全性,开发人员在开发应用程序的过程中须要注意使用Android 应用程序的内部存储空间. 依据不同的要求.将相应的数据文件.缓存文件.暂时文件等分别存储在相应的位置. 注意 ...

  4. module require区别

    LUA modue require package 区别 2011-01-19 12:41:35|  分类: 默认分类 |  标签:lua  package  module  require  加载  ...

  5. strcpy与memcpy的区别

    strcpy和memcpy的区别 strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制 ...

  6. 新生赛(2) problem 2 丁磊养猪

    Problem B Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  7. ubuntu下ffmpeg的安装,实现支持3gpp等转换

    最近上线的项目,语音格式转码需要调试3gpp,所以需要再spx,3gpp,3gp等格式之间转换,特记录基于ubuntu环境下的环境ffmpeg部署细则 安装测试环境:ubuntu 14.04 64bi ...

  8. 在IIS上Office Word下载失败,检索 COM 类工厂中 CLSID 为000209FF的组件失败,80070005 拒绝访问。

    最近在做一个网站时,有一个下载word文档功能,在本地直接调试是可以下载的,但部署到IIS上就出现问题了. 出现问题如下:Error:下载简历方法出错:检索 COM 类工厂中 CLSID 为 {000 ...

  9. PHP学习笔记十四【面向对象】

    <?php class Cat{ public $name; public $age; public $color; } //创建一个对象 $cat1=new Cat(); $cat1-> ...

  10. JQ 替换节点

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...