您的位置:首页 > 其它

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;
}


这个题目里有些字符串处理函数,可以仔细研究研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: