生活随笔
收集整理的這篇文章主要介紹了
牛客网_PAT乙级1014_科学计数法 (20)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
科學計數法是科學家用來表示很大或很小的數字的一種方便的方法,其滿足正則表達式[+-][1-9]"."[0-9]+E[+-][0-9]+,即數字的整數部分只有1位,小數部分至少有1位,該數字及其指數部分的正負號即使對正數也必定明確給出。
現以科學計數法的格式給出實數A,請編寫程序按普通數字表示法輸出A,并保證所有有效位都被保留。
輸入描述:
每個輸入包含1個測試用例,即一個以科學計數法表示的實數A。該數字的存儲長度不超過9999字節,且其指數的絕對值不超過9999。
輸出描述:
對每個測試用例,在一行中按普通數字表示法輸出A,并保證所有有效位都被保留,包括末尾的0。
輸入例子:
+1.23400E-03
輸出例子:
0.00123400
代碼 C++
輸出部分在39~末尾,是整個算法的核心。
對E后面的指數進行判斷,分為大于0,等于0,小于0的情況。
整個輸入和輸出過程,算法并不優雅,可以算得上是暴力分類討論,把整個字符串拆開揉碎之后接收,再一點一點地輸出到一行。
#include<iostream>
#include<iostream>
#include<vector>
int main()
{char symbol
;std
::cin
>> symbol
;if (symbol
== '-'){std
::cout
<< "-";}int beforeDecimal
;std
::cin
>> beforeDecimal
;std
::vector
<int> num
;num
.push_back(beforeDecimal
);char afterDecimal
;int INTafterDecimal
;while (std
::cin
>> afterDecimal
){if (afterDecimal
>= '0'&&afterDecimal
<= '9'){INTafterDecimal
= afterDecimal
- 48;num
.push_back(INTafterDecimal
);}else if (afterDecimal
== 'E'){break;}}int numAfterE
;std
::cin
>> numAfterE
;std
::vector
<int>::iterator iter
= num
.begin();int i
;int decimalLocate
;int printCount
= 0;if (numAfterE
< 0){std
::cout
<< "0.";for (i
= -numAfterE
- 1; i
> 0; i
--){std
::cout
<< "0";}while (iter
!= num
.end()){std
::cout
<< *iter
;iter
++;}}else if (numAfterE
== 0){decimalLocate
= 1;while (iter
!= num
.end()){std
::cout
<< *iter
;iter
++;printCount
++;if (printCount
== decimalLocate
){std
::cout
<< ".";}}}else if (numAfterE
> 0){decimalLocate
= numAfterE
+ 1;while (iter
!= num
.end()){std
::cout
<< *iter
;iter
++;printCount
++;if (printCount
== decimalLocate
){std
::cout
<< ".";}}int t
;for (t
= numAfterE
- printCount
; t
>= 0; t
--){std
::cout
<< "0";}}system("pause");
}
總結
以上是生活随笔為你收集整理的牛客网_PAT乙级1014_科学计数法 (20)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。