【PAT】1073. Scientific Notation (20)
来源:互联网 发布:咸宁淘宝开店培训学校 编辑:程序博客网 时间:2024/05/16 12:48
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-03Sample Output 1:
0.00123400Sample Input 2:
-1.2E+10Sample Output 2:
-12000000000
分析:字符串的处理。有小坑。代码中使用stringstream来输入会比较方便。
#include <iostream>#include <string>#include <vector>#include <sstream>using namespace std;int main(int argc, char** argv) {string outStr="";string str;cin>>str;if(str[0] == '-')outStr+="-";int pos = str.find("E");string num(str, 1, pos-1);string t(str,pos+1);stringstream ss; ss<<t.substr(1,t.size()-1); char eFlag = t[0];int i, val=0, index;ss>>val; if(val == 0){cout<<outStr<<num<<endl;return 0;}if(eFlag== '+'){ outStr += num[0];if( val<=num.size()-2 ){//例如这种情况: +1.23E+1 index = 0;for(i=2; index<val; i++, index++){outStr += num[i];}if(val!=num.size()-2){outStr += ".";}outStr += num.substr(i, num.size()-i);}else{outStr += num.substr(2, num.size()-2);for(i=0; i<val-num.size()+2; i++){outStr += "0";}} }else if(eFlag == '-'){outStr += "0.";for(i=0; i<val-1; i++){outStr += "0";}for(i=0; i<num.size(); i++){if(num[i] != '.'){outStr += num[i];}}}cout<<outStr<<endl;return 0;}
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation(20)
- PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation (20)
- 【PAT】1073. Scientific Notation (20)
- Pat 1073 PAT 1073. Scientific Notation (20)
- PAT 1073. Scientific Notation
- PAT 1073. Scientific Notation
- 【PAT】1073. Scientific Notation
- pat 1073. Scientific Notation
- PAT (Advanced) 1073. Scientific Notation (20)
- PAT Advanced Level 1073. Scientific Notation (20)
- PAT A 1073. Scientific Notation (20)
- PAT甲级练习1073. Scientific Notation (20)
- PAT甲级1073. Scientific Notation (20)
- Foundation框架--NSMutableArray的使用
- C语言的整型溢出问题
- Android服务之单词本
- 5,6天
- UVA - 536 Tree Recovery
- 【PAT】1073. Scientific Notation (20)
- Redis 3.0中文官方文档翻译计划(3)
- 爬爬爬之路:C语言(六) 函数篇
- 关于c语言逆序输出
- javaweb项目-MyEclipse环境搭建配置
- Hive 启动异常: java.net.URISyntaxException: Relative path in absolute URI:
- List<HashMap<String, Object>> mylist...按照键值对来解析
- 马士兵Java课堂笔记-4-Math.round()函数-强制转换中可以实现四舍五入
- Json解析