POJ-1001
来源:互联网 发布:手机淘宝怎样添加好友 编辑:程序博客网 时间:2024/06/05 09:19
来源:http://poj.org/problem?id=1001&lang=zh-CN
这题没什么算法可言,主要是各种情况的处理。
思路:先忽略小数点,算完后输出时在适当的位置输出一个小数点就行了,完全按照高精度乘单精度的方法。至于小数点的输出,因为每乘一次带小数点的数,小数位数相加就是了。
给出几种特殊情况:
25 2
25.0 2
0.1 3
输出的结果应该是:
625
625
.001
注意好这几种情况基本上AC了。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <string>using namespace std;int a[100];int pre,dd;void doit(string s) //把字符串存储到数组a中{ dd=0; memset(a,0,sizeof(a)); int kk=0; //判断输入的数据有没有小数点 while (s[kk]!='.' && kk<s.length()) kk++; if (kk==s.length()) a[0]=s.length(); else a[0]=s.length()-1; int h; for (h=0;h<s.length() && s[h]!='.';h++) { dd=dd*10+s[h]-'0'; a[a[0]-h]=s[h]-'0'; } //pre存储小数点的位置 if (h==s.length()) {pre=0; return;} pre=s.length()-h-1; for (int i=h+1;i<s.length();i++) { a[a[0]-i+1]=s[i]-'0'; dd=dd*10+s[i]-'0'; }}void work(){ for (int i=1;i<=a[0];i++) { a[i]=a[i]*dd; if (i!=1) { a[i]+=a[i-1]/10;a[i-1]%=10; } //因为只用了一个数组,所以进位要这样写 } while (a[a[0]]>9) //处理位数问题 { a[a[0]+1]=a[a[0]]/10; a[a[0]]%=10; a[0]++; }}int main(){ string R; int n; while (cin>>R>>n) { doit(R); for (int i=1;i<n;i++) work(); for (int i=a[0];i>pre*n;i--) //输出整数部分 printf("%d",a[i]); int xx=1; while (a[xx]==0) xx++; //把多余的0去掉 if (xx<=pre*n) //处理没有小数点的情况 { printf("."); for (int i=pre*n;i>=xx;i--) printf("%d",a[i]); } printf("\n"); } return 0;}
- poj 1001
- poj 1001
- poj 1001
- POJ 1001
- POJ 1001
- poj 1001
- POJ 1001
- POJ 1001
- POJ 1001
- POJ-1001
- POJ 1001
- poj 1001
- POJ 1001
- POJ 1001
- POJ#1001
- poj-1001
- poj 1001
- POJ 1001
- ObjC-10 网络、文件
- USACO-Shaping Regions
- MST-最小生成树
- 最短路
- easyui datagrid 让某行复选框置灰不能选
- POJ-1001
- POJ-1003
- POJ-1002
- POJ-1004
- POJ-1005
- C和C++中的数字与字符串的转换
- POJ-1006
- POJ-1007
- POJ-1008