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

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

代码:

#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. 2、elasticsearch 的安装和插件的安装

    1.安装Elasticsearch集群 1.下载elasticsearch-2.0.0.tar.gz,执行tar -zxvf elasticsearch-2.0.0.tar.gz解压 2.修改conf ...

  2. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  3. 用c++语言编写函数 int index(char *s,char * t),返回字符串t在字符串s中出现的最左边的位置,如果s中没有与t匹配的子串,则返回-1。类似于索引的功能。

    首先,分析一下程序的思路: 1:从s的第i个元素开始,与t中的第1个元素匹配,如果相等,则将s的第i+1元素与t中的第2个元素匹配,以此类推,如果t所有元素都匹配,则返回位置i;否则,执行2; 2: ...

  4. poj 2429 Pollard_rho大数分解

    先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...

  5. Jvm垃圾回收堆内存变化过程

    当Eden区域满时,触发minor GC,垃圾收集器把Eden区域中的不可达对象标记出来.第一次执行minor GC时Survivor 1与Survivor 2均为空: Eden中的不可达对象占用的内 ...

  6. Git使用过程

    Git-------目前世界上最先进的分布式版本控制系统(没有之一) 什么是版本控制系统? 说简单点,就是一个文件,对其增加.删除.修改都可以被记录下来,不仅自己可以修改,其他人也可以进行修改 每次对 ...

  7. sizeof 和 strlen

    1. sizeof 1.1 sizeof是一个独立的运算符,不是函数.sizeof给我们提供有关数据项目所分配的内存的大小.例如: 1 2 cout << sizeof(long) < ...

  8. rowid的作用

    一.快速删除重复的记录的方法: 1.通过创建临时表删除重复的的记录 1)创建emp表的临时表,把数据导入临时表中,删除原来的表中的数据然后把临时表中的数据导入原表 create table emp_t ...

  9. 使用SQLiteHelper创建数据库并插入数据

    参考<疯狂android讲义>8.4节P424 1.获取SQLiteDatabase实例有2种方法,一是直接new SQLiteDatabase(),另一种使用SQLiteHelper.一 ...

  10. python核心编程-习题-第二章

    PS:PDF在线地址:http://bcmi.sjtu.edu.cn/~zhaohai/ptm2012/data/Python-kernel.programming.v2.pdf 2-1  变量,pr ...