题目如下:

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

题目要求对给定的科学计数法进行解析,并且输出传统计数法表示的数字,要求正数不带正号,小数保留原来的后缀0个数。

这个题的关键是结合string的find、substr方法查找和截取,使用stringstream来转换字符串到数字。

此类问题最主要的是抓住分类讨论的要点,处理尽可能少的情况。

最初我用遍历的方法找到各个部分的位置,发现比较繁琐,后来参考了xtzmm1215的解法,发现使用find和substr会方便许多。

下面是xtzmm1215的代码,我补充了一些注释,使之易读:

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
using namespace std; int main(){
string s, ans = "";
getline(cin, s);
if (s[0] == '-')
ans += s[0]; // 负数前面才加负号
int indexE = s.find("E");
string num = s.substr(1, indexE-1);
char x = s[indexE + 1];
string exp = s.substr(indexE+2, s.size() - indexE - 2);
stringstream ss;
ss << exp;
int e;
ss >> e;
if (e == 0){ // 指数为0直接输出
cout << ans << num << endl;
return 0;
}
if (x == '+'){ // 正指数的情况
if (e < num.size() - 2){ // num.size()-2是减去小数点和1以后的部分,如果指数达不到,需要在合适的地方加小数点。
ans = ans + num[0] + num.substr(2, e) + "." + num.substr(e + 2, num.size() - e - 2);
}
else{ // num.size()-2如果和指数相当,则需要补充0来扩大数字。
ans = ans + num[0] + num.substr(2, num.size()-2);
for (int i = 0; i < e - num.size() + 2; i++)
ans += "0";
}
}
if (x == '-'){ // 负指数的情况,需要前补0,0.算作一次指数,因此指数减到1就应该停止前补0.
ans = ans + "0.";
while (e-- != 1)
ans += "0";
ans = ans + num[0] + num.substr(2, num.size()-2);
}
cout << ans << endl;
return 0;
}

1073. Scientific Notation (20)的更多相关文章

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

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

  2. PAT Advanced 1073 Scientific Notation (20 分)

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

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

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

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

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

  5. PAT (Advanced Level) 1073. Scientific Notation (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

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

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

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

    题意: 输入科学计数法输出它表示的数字. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> u ...

  8. PAT 1073 Scientific Notation

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

  9. 1073 Scientific Notation (20 分)

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

随机推荐

  1. [bzoj1566][NOI2009]管道取珠

    来自FallDream的博客,未经允许,请勿转载,谢谢. n<=500 神题...... 发现这个平方可以看作两个序列相同的对数  然后就可以表示状态了. f[i][j][k]表示两个序列各选了 ...

  2. 音频自动增益 与 静音检测 算法 附完整C代码

    前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用于评估一定长度音频的音量强度, 而分析之后,很多类似的需求,肯定是做音频增益,提高音量诸如此类做法. ...

  3. Entity Framework DBContext 增删改查深度解析

    Entity Framework DBContext 增删改查深度解析 有一段时间没有更新博客了,赶上今天外面下雨,而且没人约球,打算把最近对Entity Framework DBContext使用的 ...

  4. TOJ 数据结构实验--静态顺序栈

    描述 创建一个顺序栈(静态),栈大小为5.能够完成栈的初始化.入栈.出栈.获取栈顶元素.销毁栈等操作. 顺序栈类型定义如下: typedef struct {  int data[Max];    i ...

  5. Java 反射 Method threw 'java.lang.InstantiationException' exception.

    查看这个InstantiationException:异常的api所说的是: 当应用程序试图使用 Class 类中的 newInstance 方法创建一个类的实例,而指定的类对象无法被实例化时,抛出该 ...

  6. C++中的各种可调用对象

    概述 一组执行任务的语句都可以视为一个函数,一个可调用对象.在程序设计的过程中,我们习惯于把那些具有复用性的一组语句抽象为函数,把变化的部分抽象为函数的参数. 函数的使用能够极大的极少代码重复率,提高 ...

  7. 视频人脸检测——OpenCV版(三)

    视频人脸检测是图片人脸检测的高级版本,图片检测详情点击查看我的上一篇<图片人脸检测——OpenCV版(二)> 实现思路: 调用电脑的摄像头,把摄像的信息逐帧分解成图片,基于图片检测标识出人 ...

  8. 基于babylon3D模型研究3D骨骼动画(1)

    3D骨骼动画是实现较为复杂3D场景的重要技术,Babylon.js引擎内置了对骨骼动画的支持,但Babylon.js使用的骨骼动画的模型多是从3DsMax.Blender等3D建模工具转换而来,骨骼动 ...

  9. 如何搭建apache服务?

    为了日后便于查询,本文所涉及到的所有命令集合如下: chkconfig iptables off #关闭防火墙命令 在Centos7中使用的是chkconfig firewalld off vi /e ...

  10. Ubuntu 16.04.4 LTS下安装JDK

    写在前面 为什么我又装jdk?今天顺手升级了我的双系统中的ubuntu,开始的时候用的图形化界面升级,后来你懂的,升级软件死锁了.. 用命令行也没有效果了,提示锁被占用,手残重启试试,彻底崩了...我 ...