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 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
Note
- 输出科学记数法的真实数字
- 逻辑题,两个测试点没过,pat考察的半壁江山吧
- 我的思路
Code
#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main(){
int flag = 0, cnt = 0, jie = 1, ptr;
string s;
char digit[100];
getline(cin, s);
for(int i = 1; i < s.size(); i++){
if(s[i] != 'E' && flag == 0){
if(s[i] != '.'){
digit[cnt++] = s[i];
}
}
else if(s[i] == 'E') flag = 1;
else if(flag == 1 && s[i] != 'E'){
if(s[i] == '-') {
jie = - 1; ptr = i;
break;
}
else if(s[i] == '+') {
jie = 1; ptr = i;
break;
}
}
}
int num = cnt;
cnt = 0;
//cout << s.substr(ptr + 1);
jie *= stoi(s.substr(ptr+1));
if(s[0] == '-') cout << "-";
for(int i = 0; cnt != num || jie != 0; i++){
if(jie < 0){
cout << 0;
if(i == 0) cout << ".";
jie++;
}
else if(jie > 0 && cnt == num){// 如果已经输出到最后一个字符那么就在后⾯面补n-cnt个0
cout << 0;
jie--;
}
else{
if(jie == 0 && cnt != 0) cout << ".";//如果没有输出到最后一个字符
cout << digit[cnt++];
if(cnt > 1 && jie > 0) jie--;
}
}
return 0;
}
//+1.234E+2
Note2
问题
-
当时随手改的char digit【100】 浪费了40分钟
string a, b不能通过已有的a进行 b[i] = a[i],要用substr -
小数点输出多次,原因是自己分的三种情况之间相互跳转
#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main(){
int flag = 0, cnt = 0, jie = 1, ptr;
string s;
char digit[10000];
getline(cin, s);
for(int i = 1; i < s.size(); i++){
if(s[i] != 'E' && flag == 0){
if(s[i] != '.'){
digit[cnt++] = s[i];
}
}
else if(s[i] == 'E') flag = 1;
else if(flag == 1 && s[i] != 'E'){
if(s[i] == '-') {
jie = - 1; ptr = i;
break;
}
else if(s[i] == '+') {
jie = 1; ptr = i;
break;
}
}
}
int num = cnt,jie_flag = 0;
cnt = 0;
//cout << s.substr(ptr + 1);
jie *= stoi(s.substr(ptr+1));
if(s[0] == '-') cout << "-";
for(int i = 0; cnt != num || jie != 0; i++){
if(jie < 0){
cout << 0;
if(i == 0) {
cout << ".";
jie_flag = 1;
}
jie++;
}
else if(jie > 0 && cnt == num){
cout << 0;
jie--;
}
else{
if(jie == 0&& cnt != 0 && jie_flag == 0) { cout << "."; jie_flag = 1;}
cout << digit[cnt++];
if(cnt > 1 && jie > 0) jie--;
}
}
return 0;
}
//+1.234E+2
Note3
般柳神代码,逻辑简单易懂
#include<iostream>
using namespace std;
int main(){
string s;
cin >>s;
int i = 0;
while(s[i] != 'E') i++;
string t = s.substr(1, i-1);
int n = stoi(s.substr(i+1));
if(s[0] == '-') cout << "-";
if(n < 0){
cout << "0.";
for(int j = 0; j < abs(n) - 1; j++) cout << "0";
for(int j = 0; j < t.size(); j++)
if(t[j] != '.') cout << t[j];
}else{
cout << t[0];
int cnt, j;
for(j = 2, cnt = 0; j <t.size() && cnt < n;j++, cnt++) cout << t[j];
if(j == t.size())
for(int k = 0; k < n - cnt; k++) cout << "0";
else{
cout << ".";
for(int k = j; k < t.size(); k++ ) cout << t[k];
}
}
return 0;
}