pat解题报告【1073】
2014-08-10 22:56
204 查看
1073. Scientific Notation (20)
时间限制100 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming
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
这题本质是模拟科学计数法的转换,坑在这句话:The number is no more than 9999 bytes in length,什么意思呢,这个数的位数可以很长!刚开始没注意这句话,把字符串转换成数字,做计算。结果最后两个点过不了,仔细看了看发现题目里有这句话。改成字符串模拟计算过程的算法,ac了。
代码:
// pat-1073.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "iostream" #include "string" #include "stdio.h" #include "vector" #include "fstream" using namespace std; vector<char> ans_pre; vector<char> ans_apend; int main() { //+1.23400E-03 //shishu:1.23400 zhishu:03 //op:+ dir:- fstream fcin; fcin.open("C:\\Users\\Administrator\\Desktop\\456.txt"); string str; fcin>>str; int offset=str.find('E'); string shishu=str.substr(1,offset-1);//实数部分 int shishu_l=shishu.length();//实数的长度 char op=str[0]; char dir=str[offset+1]; int offset2=str.length()-1-offset-1; string expart=str.substr(offset+2,offset2); double zhishu=atof(expart.c_str()); int apend=0; int pos=0;//原始小数点位置 if (dir=='-') pos-=zhishu; else pos+=zhishu; if (pos<0) { while(pos<0) { ans_pre.push_back('0'); pos++; } } else { apend=pos-(shishu_l-2);//小于0说明还是有小数位的 while(apend>0){ ans_apend.push_back('0'); apend--; } } int index=shishu.find('.'); shishu.erase(index,1); if (apend<0)//移动小数点 { shishu.insert(shishu_l-1+apend,"."); } if (op=='-') { cout<<'-'; } if (!ans_pre.empty()) { cout<<ans_pre.back(); ans_pre.pop_back(); cout<<"."; while(!ans_pre.empty()) { cout<<ans_pre.back(); ans_pre.pop_back(); } } cout<<shishu; while(!ans_apend.empty()) { cout<<ans_apend.back(); ans_apend.pop_back(); } return 0; }
这个题目里有些字符串处理函数,可以仔细研究研究。
相关文章推荐
- pat解题报告【1078】
- Ural 1073 Square Country 解题报告
- PAT 解题报告 1013. Battle Over Cities (25)
- PAT 解题报告 1049. Counting Ones (30)
- pat(basic level) practise 解题报告
- PAT(B) Basic level 1045 解题报告
- 解题报告-PAT- Tree Traversals Again(1086)
- PAT (Advanced Level) 1127. ZigZagging on a Tree (30) 解题报告
- pat解题报告【1082】
- PAT1002:1002. A+B for Polynomials 解题报告
- PAT 解题报告 1050. String Subtraction (20)
- 浙大PAT_1044:Shopping in Mars 解题报告
- [置顶] PAT甲级解题报告索引
- PAT-1010解题报告
- TYVJ 1073 加分二叉树 解题报告
- PAT 解题报告 1051. Pop Sequence (25)
- pat解题报告【1074】
- PAT 解题报告 1052. Linked List Sorting (25)
- 浙大PAT_1038:Recover the Smallest Number 解题报告
- PAT 1025 反转链表 (25) 解题报告