题目如下:

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. hdu 5113(2014北京—搜索+剪枝)

    题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...

  2. hdu 3436 splay树+离散化*

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  3. 杜教筛进阶+洲阁筛讲解+SPOJ divcnt3

    Part 1:杜教筛进阶在了解了杜教筛基本应用,如$\sum_{i=1}^n\varphi(i)$的求法后,我们看一些杜教筛较难的应用.求$\sum_{i=1}^n\varphi(i)*i$考虑把它与 ...

  4. LCT模板(BZOJ2631)

    用LCT实现路径加,路径乘,断开及加上一条边(保证是树),查询路径和. #include <cstdio> #include <algorithm> #define l(x) ...

  5. WebStorm配置node.js调试

    最近因为工作关系,一直在做node.js的开发,学习了koa框架,orm框架sequelize,以及swagger文档的配置.但是,最近因为swagger文档使用了es6的修饰器那么个东西(在java ...

  6. PTA 社交网络图中结点的“重要性”计算(30 分)

    7-12 社交网络图中结点的“重要性”计算(30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互 ...

  7. js 数字前面自动补零

    num传入的数字,n需要的字符长度        function PrefixInteger(num, n) {            return (Array(n).join(0) + num) ...

  8. Algorithm in Practice - Sorting and Searching

    Algorithm in Practice Author: Zhong-Liang Xiang Date: Aug. 1st, 2017 不完整, 部分排序和查询算法, 需添加. Prerequisi ...

  9. 剑指架构师系列-ActiveMQ队列的使用

    安装ActiveMQ只需要下载包后解压,然后就可以启动与关闭ActiveMQ了,如下: ./activemq start ./activemq stop 访问管理页面: http://10.10.20 ...

  10. MongoDB 原子操作

    mongodb不支持事务,所以,在你的项目中应用时,要注意这点.无论什么设计,都不要要求mongodb保证数据的完整性. 但是mongodb提供了许多原子操作,比如文档的保存,修改,删除等,都是原子操 ...