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

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

代码:

#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. Stones(优先队列)

    Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  2. vs在线工具杂烩

    http://visualstudiogallery.msdn.microsoft.com/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Va ...

  3. Web Service中的XFire 传输List 自定义对象.

    我把这个创建的步骤和代码的贴出来,. 首先新建一个工程,取名就随便点啦..MyWebService,然后复制jar包到lib目录下, 创建包,建立接口..写一个javaBean的类, 以下是一个简单的 ...

  4. 购买DigtalOcean VPS 以及 连接Linux

    1.DigtalOcean简介 digitalocean是一家成立于2012年的总部设置在纽约的云主机商家,眼下在荷兰的阿姆斯特丹(AMS1.AMS2).美国的纽约(NYC1.NYC2)和旧金山(SF ...

  5. linux下 /etc/profile、~/.bash_profile ~/.profile的执行过程

    关于登录linux时,/etc/profile.~/.bash_profile等几个文件的执行过程. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时,首先启动 /etc/profile ...

  6. 【贪心】【POJ3154】墓地雕塑(Graveyard, NEERC 2006, LA 3708)需要稍稍加工的(先贪心,再确保能这样贪(可行性&&如果可行必定最优&&非证明最优性)的题)(K)

    例题4  墓地雕塑(Graveyard, NEERC 2006, LA 3708) 在一个周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周 ...

  7. asp.net 后台对话框,确认跳转

    Response.Write("<script>alert('不合法!'); window.location.href='" + ResolveClientUrl(&q ...

  8. 通过递归方法对一个单词所有的组合进行列举(java)

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public ...

  9. linux系统学习笔记:文件、目录、用户

    本篇主要从stat函数开始,逐个说明stat结构的每一个成员,以此来了解文件的所有属性.同时将说明修改这个属性的各个函数. 一.文件 使用stat函数族得到和文件有关的信息结构. #include & ...

  10. 从汇编看c++的new和delete

    下面是c++源码: class X { private: int _x; public: X() : _x(xx) {} ~X() {} }; int main() { X* xp = new X; ...