POJ 1001 Exponentiation 大浮点数乘法
来源:互联网 发布:大富翁数据库 编辑:程序博客网 时间:2024/06/02 02:32
题目:
http://poj.org/problem?id=1001
题意:
给出一个浮点数,一个n,求这个浮点数的n次方,输出要求去掉多余的0,小数位为0的只输出整数,小数位不为0且整数位为0的,去掉整数位。
思路:
首先去掉多余的后缀0,然后记录有多少为小数,再把小数位去掉作为整数,模拟大整数乘法,之后加上相应的小数位。注意上述的格式
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 2010;void multiply(char *s1, char *s2, char *s3){ int len1 = strlen(s1), len2 = strlen(s2), len3 = 0; reverse(s1, s1 + len1); reverse(s2, s2 + len2); for(int i = 0; i < len1+len2; i++) s3[i] = '0'; for(int i = 0; i < len1; i++) { int t = 0; for(int j = 0; j < len2; j++) { t += (s1[i]-'0') * (s2[j]-'0'); s3[i+j] += t%10, t /= 10; if(s3[i+j] > '9') s3[i+j] -= 10, t++; len3 = max(len3, i+j); } if(t) s3[i+len2] += t, len3 = max(len3, i+len2); } s3[++len3] = '\0'; reverse(s1, s1 + len1); reverse(s2, s2 + len2); reverse(s3, s3 + len3);}int work(char *s){ char *p = strchr(s, '.'); if(p != NULL) { for(int i = strlen(s)-1; i >= 0; i--) { if(s[i] == '0') s[i] = '\0'; else break; } } bool f = false; int num = 0; for(int i = 0; s[i]; i++) { if(s[i] == '.') f = true; if(f) s[i] = s[i+1], num++; } return num == 0 ? num : num-1;}int main(){ int n; char s1[N], s2[N], s3[N]; while(~ scanf("%s%d", s1, &n)) { int num = work(s1); s2[0] = '1', s2[1] = '\0'; for(int i = 1; i <= n; i++) { multiply(s1, s2, s3); memcpy(s2, s3, sizeof s3); } int len = strlen(s3); for(int i = len; i >= len - num*n; i--) s3[i+1] = s3[i]; s3[len-num*n] = '.'; len = strlen(s3); if(s3[len-1] == '.') s3[len-1] = '\0'; int idx = 0; while(s3[idx] == '0') idx++; if(idx == len-1) printf("0\n");//特判为0的情况 else printf("%s\n", s3 + idx); } return 0;}
0 0
- POJ 1001 Exponentiation 大浮点数乘法
- POJ 1001Exponentiation(高精度浮点乘法)
- 20140709 【高精度浮点数】POJ 1001 Exponentiation
- POJ1001 Exponentiation--浮点数的高精度乘法
- HDU 1063 && POJ 1001 Exponentiation (JAVA浮点数大数)
- POJ 1001 Exponentiation(浮点数的n次方)
- 高精度浮点数幂次方 POJ 1001 Exponentiation
- POJ 1001:Exponentiation —— 高精度浮点数运算
- poj 1001高精浮点数乘法
- poj 1001 (Exponentiation) 高精度乘法
- 【POJ】 1001-Exponentiation 【高精度乘法】
- POJ 1001 Exponentiation 高精度乘法
- poj 1001 Exponentiation 高精度乘法
- 高精度乘法,支持浮点运算。POJ 1001 Exponentiation 顺便总结一下string的常用函数
- PKU-1001 Exponentiation(浮点数相乘)
- poj 1001 Exponentiation(高精度乘法)
- POJ 1001 Exponentiation (高精度/大数乘法)
- POJ 1001 Exponentiation 大数乘法模拟
- POJ
- activeMq遇到的一些问题
- Win7安装msu文件失败引发的一系列问题
- 《利用Python进行数据分析》第二章-学习笔记
- 使用回调函数实现socket服务器端子线程刷新UI数据(socket通信第三弹)
- POJ 1001 Exponentiation 大浮点数乘法
- 解决Windows 7启动 Docker 报错
- lamp环境的搭建(源码包安装)
- Codeforces Round #374 (Div. 2) B. Passwords —— 基础题
- 执行转换时如何让Kettle记录错误并继续执行?——记一种解决方案
- caffeOnSpark install in ubuntu14.04
- centos 关闭防火墙及hostname设置
- base64
- 转载redis之jedis简单使用