1073. Scientific Notation (20)
2015-09-07 18:31
239 查看
题目链接:http://www.patest.cn/contests/pat-a-practise/1073
题目:
Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one
digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.
Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.
Input Specification:
Each input file contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.
Output Specification:
For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros,
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
分析:
不用能用double,因为位数不够,而应该用string来做
AC代码:
刚开会用double,出错了
后来用了string
截图:
——Apie陈小旭
题目:
Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one
digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.
Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.
Input Specification:
Each input file contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.
Output Specification:
For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros,
Sample Input 1:
+1.23400E-03
Sample Output 1:
0.00123400
Sample Input 2:
-1.2E+10
Sample Output 2:
-12000000000
分析:
不用能用double,因为位数不够,而应该用string来做
AC代码:
刚开会用double,出错了
#include<stdio.h> #include<string> #include<iostream> #include<algorithm> #include<math.h> using namespace std; int main(void){ freopen("F://Temp/input.txt", "r", stdin); string input; cin >> input; string ans = ""; string str1, str2; double num1 = 0; double num2 = 0; //处理input字符串 int pos = 0; while ('E' != input[pos])pos++; int bit1 = pos - 2;//减去一个符号位和一个小数点的位 str1 = input.substr(0, pos); str2 = input.substr(pos + 1, input.size() - pos - 1); double flag = 0; if ('-' == input[pos + 1])flag = -1; else if ('+' == input[pos + 1])flag = 1; // ans += input[0]; num1 = atof(str1.c_str()); num2 = atof(str2.c_str()); int bit2 = abs(num2); double res = num1 * pow(10, num2); if (flag == 1){ printf("%*.*f\n", bit2 + 1, bit2 - bit1 - 1 >= 0 ? 0 : bit2 - bit1 - 1, res); } else if (flag == -1){ printf("%.*f\n", bit1 + bit2 - 1, res); } return 0; }
后来用了string
#include<stdio.h> #include<string> #include<iostream> #include<algorithm> #include<math.h> using namespace std; int main(void){ freopen("F://Temp/input.txt", "r", stdin); string input; cin >> input; string ans = ""; string str1, str2; //处理input字符串 int pos = 0; while ('E' != input[pos])pos++; str1 = input.substr(0, pos); str2 = input.substr(pos + 1, input.size() - pos - 1); int flag = 0;//标识指数是正还是负 if ('-' == input[pos + 1])flag = -1; else if ('+' == input[pos + 1])flag = 1; // ans += input[0]; int bit1 = pos - 2;//减去一个符号位和一个小数点的位,前面位数的有效位 int bit2 = abs(atof(str2.c_str()));//指数的绝对值,指数所代表的有效位 ans += input[0]; if (flag == 1){//如果指数是正数的话 if (bit1 <= bit2 + 1){//比如+1.1E+01或+02的情况,不是小数形式且后面要添加至少0个0的 for (int i = 1; i < str1.size(); i++){ if ('.' != str1[i])ans += str1[i]; } for (int i = 0; i < bit2 + 1 - bit1; i++){ ans += '0'; } } else if (bit1 > bit2 + 1){//比如+1.2345E+02的情况,是小数形式 ans += str1[1]; for (int i = 0; i < bit2; i++){ ans += str1[3 + i]; } ans += '.'; for (int i = 3 + bit2; i < str1.size(); i++){ ans += str1[i]; } } } else if (flag == -1){ ans += "0.";//这里添加最前面的头部 for (int i = 0; i < bit2 - 1; i++)//计算最前面的0的个数 ans += '0'; for (int i = 0; i < str1.size(); i++){//把str1中的除了'.'的部分全都添加到ans字符串中 if ('.' != str1[1 + i]) ans += str1[1 + i]; } } if ('+' == ans[0])//如果结果是正数,则应该把最前面的'+'去掉 ans = ans.substr(1); cout << ans << endl; return 0; }
截图:
——Apie陈小旭
相关文章推荐
- Druid原理介绍(1)
- Android中ListView的item按钮监听错乱问题解决办法 推荐
- Windows下Anaconda的安装和简单使用
- HASH表实现
- Trie树(压缩Trie树及Double-Array Trie)
- UE4 computing lightmap UV mapping in SpeedTree
- two sample ttest & paired ttst
- JXL获取excel批注
- 高仿58同城的Gridview
- JAVA高并发学习笔记(二) 多线程基础
- 正反方向传值及打地鼠游戏的实现
- linux下IPTABLES配置详解
- quick-cocos2d-x基于源码加密打包功能的更新策略(3)
- (function(){})(),function(){}()的相关探究
- iOS如何把所有页面状态栏的字体颜色都设置为白色
- java基础 用arraylist做电话本
- iOS7自定义视图控制器过渡2-手势交互过渡
- Git中pull对比fetch和merge
- Docker基础技术:Linux Namespace(上)
- quick-cocos2d-x基于源码加密打包功能的更新策略(2)