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 ...
随机推荐
- FJOI2017 RP++
嗯如果算得没错大概十二小时之后就是省选二试了 这次考试貌似就在我们学校 虽然机子挺旧的基本没用过 平时训练都是在专门的机房 其实貌似压力不是很大 因为一试跪了TAT 那时候还是图样 T3按照惯例是 ...
- java9学习之模块化
截止到目前JDK的版本已经更新到10了,虽然java9的生命周期才半年,但是我认为这个版本带来的变革是不可磨灭的,它是第一次深层次的针对架构以及依赖上的革新.下面我们就来学习一下. 一.模块化项目构建 ...
- 《Java技术》第一次作业
(一)学习总结 1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明. (1)使 ...
- Gethub readme 撰写
大标题=== 小标题----- #一级标题 ##二级标题 ###三级标题 ####四级标题 #####五级标题 ######六级标题 插入圆点* 昵称:果冻虾仁 * 别名:隔壁老王 * 英文名:Jel ...
- jquery easyui combobox 高度自适应
data-options="required:true,editable:false,panelHeight:'auto'" 加上panelHeight:'auto'即可 列合并 ...
- debug的一些按钮意义
F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 Step Over 相当于eclipse的f6 跳到下一步 F7 Step ...
- Cannot change version of project facet Dynamic Web Module to 2.5的解决
步骤1 右键项目 点击Project Facets 修改里面的 Dynamic Web Module 成2.5 maven-update看下有没有解决 步骤2 没有就导入 <dependency ...
- 项目管理软件系列-Linux一键安装禅道
linux用一键安装包 简介:本文介绍如何在linux下面使用禅道一键安装包搭建禅道的运行环境. linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅 ...
- Intellij IDEA自动编译问题
对IDEA的界面很有爱,但是感到他的项目启动速度太慢了.所以查了资料做了优化. 1:开启自动测试 File->setting->compiler 勾选上上面的, 2修改run/de ...
- org.apache.maven.archiver.MavenArchiver.getManifest
eclipse导入新的maven项目时,pom.xml第一行报错: org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.mav ...