题目如下:

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. Windows下免安装版mysql5.7的初始密码

    MySQL5.7之后,初始密码不在默认为空,而是随机生成的密码. 在mysql/data目录下,生成了一个.err文件(等同linux下的log日志文件,此文件会被mysql服务占用). 使用记事本可 ...

  2. Tinychain 是比特币的一个简易口袋实现

    Putting the rough in "rough consensus" Tinychain is a pocket-sized implementation of Bitco ...

  3. h5的input的required使用中遇到的问题

    form提交时隐藏input发生的错误 问题描述 在form表单提交的时候,有些input标签被隐藏,表单验证过程中会出现An invalid form control with name='' is ...

  4. Java锁机制了解一下

    前言 回顾前面: 多线程三分钟就可以入个门了! Thread源码剖析 多线程基础必要知识点!看了学习多线程事半功倍 只有光头才能变强! 本文章主要讲的是Java多线程加锁机制,有两种: Synchro ...

  5. 浅谈 Axios 在 Vue 项目中的使用

    介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特性 它主要有如下特性: 浏览器端发起XMLHttpRequests请求 Node端发起http ...

  6. Java线程池使用和常用参数

    多线程问题: 1.java中为什么要使用多线程使用多线程,可以把一些大任务分解成多个小任务来执行,多个小任务之间互不影像,同时进行,这样,充分利用了cpu资源. 2.java中简单的实现多线程的方式 ...

  7. Redis集群搭建方案(Linux)

    Redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). list(链表).set(集合)和zset(有序 ...

  8. 吴恩达深度学习第2课第3周编程作业 的坑(Tensorflow+Tutorial)

    可能因为Andrew Ng用的是python3,而我是python2.7的缘故,我发现了坑.如下: 在辅助文件tf_utils.py中的random_mini_batches(X, Y, mini_b ...

  9. python常用执行方式&变量&input函数

    linux系统中执行py文件方式:  ./a.py 需要执行权限 chmod -R 777(最大权限) 常用执行方式: 1. ./a.py2. python a.py 文件内部头加上 #!/usr/b ...

  10. python学习之路网络编程篇(第四篇)- 续

    Memcache简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...