1073 Scientific Notation(20 分)

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

题目大意:将科学记数法转换为正常表示的数。由于给的范围都比较大,肯定只能用字符串来表示了

//之前见过一道科学记数法的题目呢,最关键的就是小数点的位置和首位非0元的位置。

pat上一次就AC,牛客网上也通过了,开心。

#include <iostream>
#include<stdlib.h>
#include <string>
#include<cmath>
using namespace std; int main()
{
string s;
cin>>s;
//找到指数。
int pos=s.find("E");
int expo=atoi(s.substr(pos+).c_str());
//如果指数是负数,那么就往前加0;
if(expo<){
int p=s.find(".");
s.erase(p,);
s.insert(,"0.");
int to=abs(expo)+;
for(int i=;i<to;i++){
s.insert(i,"");
}
s=s.substr(,pos+abs(expo));
//cout<<s; }else if(expo>){
//找到小数点后有几位
int len=pos-;
// cout<<len<<'\n';
if(len>expo){
s.insert(expo+,".");
s.erase(,);
s=s.substr(,pos);
}else{//len<=expo
int p=expo-len;
s=s.substr(,pos);
for(int i=;i<p;i++){
s+="";//怎么把0放进去呢?
}
s.erase(,);
} }
if(s[]=='+')
s=s.substr();
cout<<s;
return ;
}
// +1.2345E03
// -1.2E+10

主要就是几种情况的考虑:

1.当指数<0的时候,往前补0就可以了

2.当指数>0的时候,分两种情况:一种是小数点后的位数=<指数的,那么需要去小数点后,在后面补0;另一种是小数点后的位数>指数的,那么需要挪动小数点就可以了。

3.对stoi和atoi有了更深的认识,我用的编译器,不支持stoi(头文件为#include<string>),而支持aoti(头文件为#include<stdlib.h>)

4.atoi(s.substr(pos+1).c_str());因为后者是C中的函数,那么需要对字符串进行转换成C中形式,就是调用c_str()函数。

另外还有一点,我的代码中并没有判断指数为0的情况。。明显样例中并没有给出这样的情况:

。。。

查看了柳神的代码:https://www.liuchuo.net/archives/2061

进行了判断指数是否为0。

PAT 1073 Scientific Notation[字符串处理][科学记数法]的更多相关文章

  1. PAT 1073 Scientific Notation

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

  2. PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

  3. 1073 Scientific Notation (20 分)

    1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very la ...

  4. PAT A1073 Scientific Notation (20 分)——字符串转数字

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  5. PAT Advanced 1073 Scientific Notation (20 分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  6. PAT Basic 1024 科学计数法 (20 分) Advanced 1073 Scientific Notation (20 分)

    科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指 ...

  7. PAT甲级——1073 Scientific Notation (20分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  8. 1073. Scientific Notation (20)

    题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...

  9. PAT甲题题解-1073. Scientific Notation (20)-字符串处理

    题意:给出科学计数法的格式的数字A,要求输出普通数字表示法,所有有效位都被保留,包括末尾的0. 分两种情况,一种E+,一种E-.具体情况具体分析╮(╯_╰)╭ #include <iostrea ...

随机推荐

  1. select2 选择框插件

    <select id="selBusi_type"><select> //初始化业务类型下拉 var initBusiTypeSel = function( ...

  2. JavaScript入门第4天

    闭包:子函数可以使用父函数的局部变量 <html> <head> <title>闭包 </title> <script> function ...

  3. Maven War包 POM配置文件

    如何为你的Web程序(war包设定配置文件) 约定 http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering ...

  4. pcduino nfs挂在光盘

    1.首先在开发板上安装nfs-common 2.在pc端安装nfs-server 3.配置nfs vim /etc/exports 添加:/home/nfsroot *(rw,sync,no_root ...

  5. ubuntu samba

    Linux 下的文件共享利器 Samba 想必大家已经非常熟悉了,今天我们介绍下如何在Ubuntu 14.04中使用 Samba 共享网络文件.打印机和其它公共资源. 1.安装 Samba 和图形配置 ...

  6. SQLServer------如何快速插入几万条测试数据

    方法一: 1.建表 if OBJECT_ID('test') is not null drop table test go create table test (id ,),vid ), constr ...

  7. 全面解析Linux 内核 3.10.x - 如何开始

    万事开头难 - 如何开始? 人总是对未知的事物充满恐惧!就像航海一样,在面对危难的时候,船员和船长是一样心中充满恐惧的!只是船员始终充满恐惧,而船长却能压抑恐惧并从当前找出突破口! 我没有船长之能,但 ...

  8. FastCGI中fastcgi_param 详细说明

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param  QUERY ...

  9. Django学习笔记 开发环境搭建

    为什么使用django?1.支持快速开发:用python开发:数据库ORM系统,并不需要我们手动地构造SQL语句,而是用python的对象访问数据库,能够提升开发效率.2.大量内置应用:后台管理系统a ...

  10. android编译make错误——"javalib.jar invalid header field”、"classes-full-debug.jar 错误 41 "

    错误:读取 out/target/common/obj/JAVA_LIBRARIES/core-tests_intermediates/javalib.jar 时出错:invalid header f ...