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个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...
随机推荐
- 什么是FastCGI?
什么是FastCGI? PHP的FastCGI使你的所有php应用软件通过mod_fastci运行,而不是mod_phpsusexec.FastCGI应用速度很快 是因为他们持久稳定.不必对每一个请求 ...
- IOS反地理编码取得城市名称
// 获取当前所在的城市名 CLGeocoder *reverseGeocoder=[[CLGeocoder alloc] init]; [reverseGeocoder reverseGeocode ...
- 实现一次请求加载多个js或者css
http://tengine.taobao.org/ 相当牛x
- android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题
android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题 android createbitmap函数内存溢出,求解怎样进行处理out of memor ...
- hdu1573X问题(不互素的中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- C++ 採集音频流(PCM裸流)实现录音功能
与上一篇的"C++ 播放音频流(PCM裸流)" 点击打开链接 相相应,本篇是关于用C++实现录音功能的.相同是直接建一个win32控制台程序然后将代码拷过去改个文件名称就能够用,也 ...
- hdu 4930 Fighting the Landlords--2014 Multi-University Training Contest 6
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4930 Fighting the Landlords Time Limit: 2000/1000 MS ...
- tomcat-maven-plugin 插件使用
配置 在pom.xm 加入以下xml. <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId&g ...
- Android 打开系统相册和系统视
1.打开系统相册 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setType("vnd.android.cursor.dir ...
- iOS控制器的创建方式
iOS控制器的创建.除了常见的alloc init外还有通过加载storyboard和xib的方式,下边逐一展开: 1.代码alloc init 创建方式 ViewController *vc= [[ ...