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 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)的更多相关文章
- PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- PAT Advanced 1073 Scientific Notation (20 分)
Scientific notation is the way that scientists easily handle very large numbers or very small number ...
- PAT Basic 1024 科学计数法 (20 分) Advanced 1073 Scientific Notation (20 分)
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指 ...
- PAT甲级——1073 Scientific Notation (20分)
Scientific notation is the way that scientists easily handle very large numbers or very small number ...
- PAT (Advanced Level) 1073. Scientific Notation (20)
简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT甲题题解-1073. Scientific Notation (20)-字符串处理
题意:给出科学计数法的格式的数字A,要求输出普通数字表示法,所有有效位都被保留,包括末尾的0. 分两种情况,一种E+,一种E-.具体情况具体分析╮(╯_╰)╭ #include <iostrea ...
- 【PAT甲级】1073 Scientific Notation (20 分)
题意: 输入科学计数法输出它表示的数字. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> u ...
- PAT 1073 Scientific Notation
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- 1073 Scientific Notation (20 分)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very la ...
随机推荐
- shell基本命令
linux基本命令和shell基本命令,好多人傻傻分不清. linux基本命令积累如下: pwd:显示当前工作目录 cd:改变当前目录 ls:显示当前目录中所有目录文件和文本文件 ls -F:显示当前 ...
- Scrapy框架
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以 ...
- Python笔记(十一):多线程
(二)和(三)不感兴趣的可以跳过,这里参考了<深入理解计算机系统>第一章和<Python核心编程>第四章 (一) 多线程编程 一个程序包含多个子任务,并且子任务之间相 ...
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
- Dapper.Contrib拓展及数据库生成实体
1.关于Dapper.Contrib Dapper.Contrib是Dapper的一个拓展类库,Dapper是一个轻量级ORM框架,这个不了解的请自行收集资料,本文主要讲讲,数据库生成实体,并通过实体 ...
- bootstrap的模态框
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- SSH搭建spring,使用依赖注入的方法
配置文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...
- Linux的发行版,不同发行版之间的联系和区别
Linux 主要作为Linux发行版(通常被称为"distro")的一部分而使用.这些发行版由个人,松散组织的团队,以及商业机构和志愿者组织编写.它们通常包括了其他的系统软件和应用 ...
- 跟着小菜学习RabbitMQ启动和基础(系列一)
前言 今天开始我们正式进入RabbitMQ系列学习,在这系列博客中也会发表.NET Core和EF Core文章,网上关于RabbitMQ例子比比皆是,我将综合网上所提供的信息并加上我个人的理解来详细 ...
- 利用Python进行数据分析——Numpy基础:数组和矢量计算
利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...