您的位置:首页 > 其它

浙江大学PAT_甲级_1073. Scientific Notation (20)

2015-08-18 16:52 387 查看
题目链接:点击打开链接

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

我的C++程序:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int stringToint(const string &S)//利用stringstream完成string到int的转换
{
stringstream ss;
int result;
ss << S;
ss >> result;
return result;
}
int main()
{
string s, number;
int exp;
cin >> s;
char sign = s[0];//符号位,正负号
int LocE = s.find('E');//找到E的位置
number = s[1];
number =number+s.substr(3, LocE - 3);//得到数字部分内容
exp = stringToint(s.substr(LocE + 1));
if (sign == '-')
{
cout << "-";
}
//三种可能的情况
if (exp<0)//前面加0
{
cout << "0.";
for (int i = 0; i < -exp - 1; i++)
{
cout << '0';
}
cout << number;
}
else if (exp >= number.length() - 1)//后面加0,没小数点
{
cout << number;
for (int i = 0; i < exp - number.length() + 1; i++)
{
cout << '0';
}
}
else //小数点在中间
{
cout << number.substr(0, exp + 1);
cout << ".";
cout << number.substr(exp + 1);
}
//system("pause");
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: