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. iOS 注冊本地通知(推送)

    注:按Home键让App进入后台执行时.方可查看通知. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithO ...

  2. Entity Framework底层操作封装V2版本号(4)

    这个版本号里面.由于涉及到了多库的操作.原有的系统方法不能做到这种事情了.所以这里有了一点差别 这个类的主要用作就是,连接字符串的作用,默认是指向默认配置里面的,可是你能够指向其它的连接 using ...

  3. CentOS6.4配置163的yum源

    CentOS系统自带的更新源的速度实在是慢,为了让CentOS6使用速度更快的YUM更新源,可以选择163(网易)的更新源. 1.下载repo文件 wget http://mirrors.163.co ...

  4. tableview随笔

    //获得row NSInteger row = [[self.treeTableViewindexPathForCell:(UITableViewCell *)[[[notification.user ...

  5. Java Integer常量池

    public class IntegerExample { public static void main(String[] javalatte) { Integer i = 10; Integer ...

  6. android 软键盘回车键捕获

    EditText editText2 = (EditText)findViewById(R.id.txtTest2); editText2.setOnEditorActionListener(new ...

  7. R载入需要的程辑包:rJava Error

    > library(xlsx) 载入需要的程辑包:rJava Error : loadNamespace()里算'rJava'时.onLoad失败了,详细内容: 调用: inDL(x, as.l ...

  8. open live writer 安装 markdown 插件

    我自己用的是 OpenLiveWriter ,所以本篇只讲 OLW 的,WindowsLiveWriter 戳 MarkdownInWindowsLiveWriter,OpenLiveWriter 戳 ...

  9. JAVA内存构成详解

    java memory = direct memory(直接内存) + jvm memory(MaxPermSize +Xmx)   1)直接内存跟堆 直接内存则是一块由程序本身管理的一块内存空间,它 ...

  10. Linux环境下安装Redis并完成测试(已验证)

    准备工作: 本人测试环境:Win7 xshell远程登录Linux Linux: ubuntu 软件包:redis-3..2.6.tar.gz (Linux下redis安装包) =========== ...