Pat Problem Solving report [1073]

Source: Internet
Author: User

Tag: String

1073. Scientific notation (20) Time Limit 100 MS
The memory limit is 32000 kb.
Code length limit: 16000 B
Criterion author 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 essence of this question is to simulate the transformation of scientific notation. In this sentence, the number is no more than 9999 bytes in length. What does this mean? The number of digits can be very long! At the beginning, I did not pay attention to this sentence. I converted the string into numbers for computation. The last two points of the result failed. I looked at it carefully and found this sentence in the question. Changed to the algorithm used to simulate the computation process of the string, AC.

Code:

// Pat-1073.cpp: defines the entry point for the console application. // # 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); // real number part int s Hishu_l = Shishu. length (); // the length of a real number. 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; // original decimal place if (DIR = '-') pos-= zhishu; elsepos + = zhishu; if (Pos <0) {While (Pos <0) {ans_pre.push_back ('0'); POS ++ ;}} else {apend = pos-(shishu_l-2 ); // if the value is smaller than 0, the while (apend> 0) {ans_apend.push_back ('0'); APEN D --;} int Index = Shishu. find ('. '); Shishu. erase (index, 1); If (apend <0) // move the decimal point {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 ;}


Some string processing functions in this question can be carefully studied.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.