PAT 1073 Scientific Notation[字符串处理][科学记数法]
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 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元的位置。
pat上一次就AC,牛客网上也通过了,开心。
#include <iostream>
#include<stdlib.h>
#include <string>
#include<cmath>
using namespace std; int main()
{
string s;
cin>>s;
//找到指数。
int pos=s.find("E");
int expo=atoi(s.substr(pos+).c_str());
//如果指数是负数,那么就往前加0;
if(expo<){
int p=s.find(".");
s.erase(p,);
s.insert(,"0.");
int to=abs(expo)+;
for(int i=;i<to;i++){
s.insert(i,"");
}
s=s.substr(,pos+abs(expo));
//cout<<s; }else if(expo>){
//找到小数点后有几位
int len=pos-;
// cout<<len<<'\n';
if(len>expo){
s.insert(expo+,".");
s.erase(,);
s=s.substr(,pos);
}else{//len<=expo
int p=expo-len;
s=s.substr(,pos);
for(int i=;i<p;i++){
s+="";//怎么把0放进去呢?
}
s.erase(,);
} }
if(s[]=='+')
s=s.substr();
cout<<s;
return ;
}
// +1.2345E03
// -1.2E+10
主要就是几种情况的考虑:
1.当指数<0的时候,往前补0就可以了
2.当指数>0的时候,分两种情况:一种是小数点后的位数=<指数的,那么需要去小数点后,在后面补0;另一种是小数点后的位数>指数的,那么需要挪动小数点就可以了。
3.对stoi和atoi有了更深的认识,我用的编译器,不支持stoi(头文件为#include<string>),而支持aoti(头文件为#include<stdlib.h>)
4.atoi(s.substr(pos+1).c_str());因为后者是C中的函数,那么需要对字符串进行转换成C中形式,就是调用c_str()函数。
另外还有一点,我的代码中并没有判断指数为0的情况。。明显样例中并没有给出这样的情况:

。。。
查看了柳神的代码:https://www.liuchuo.net/archives/2061
进行了判断指数是否为0。
PAT 1073 Scientific Notation[字符串处理][科学记数法]的更多相关文章
- PAT 1073 Scientific Notation
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)
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 ...
- PAT A1073 Scientific Notation (20 分)——字符串转数字
Scientific notation is the way that scientists easily handle very large numbers or very small number ...
- 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 ...
- 1073. Scientific Notation (20)
题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...
- PAT甲题题解-1073. Scientific Notation (20)-字符串处理
题意:给出科学计数法的格式的数字A,要求输出普通数字表示法,所有有效位都被保留,包括末尾的0. 分两种情况,一种E+,一种E-.具体情况具体分析╮(╯_╰)╭ #include <iostrea ...
随机推荐
- sftp,get命令使用*通配符的方式获取批量的文件
需求描述: 今天在使用sftp进行get文件的时候,有很多文件名类似的文件,以为还是需要一个一个get 后来发现get也可以使用通配符的方式进行匹配获取多个文件,在此记录下 操作过程: 1.通过sft ...
- 超全面的JavaWeb笔记day04<dom树等>
1.案例:在末尾添加节点(*****) 创建标签 createElement方法 创建文本 createTextNode方法 把文本添加到标签下面 appendChild方法 2.元素对象(了解) 如 ...
- 【RF库Collections测试】Remove From Dictionary
Name:Remove From DictionarySource:Collections <test library>Arguments:[ dictionary | *keys ]Re ...
- MySQL性能优化(四)-- MySQL explain详解
前言 MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 一.格式 explain + ...
- apache编译安装完成后的服务启动设置
Apache安装后可通过其安装路径的bin目录下的apachectl脚本控制服务的启动和停止.本例中apache安装在/usr/local/apache-2.2.6,服务控制脚本为: /usr/loc ...
- 当div没有设置宽度,使用width的fit-content和margin:auto实现元素的水平居中
当我们做水平居中的时候,会有许多方法,margin:0 auto,或者test-align:center,以及flex布局.当元素的width不固定的时候,我们如何实现水平居中呢,代码如下: < ...
- iPad UIPopoverController弹出窗口的位置和坐标
本文转载至:http://blog.csdn.net/chang6520/article/details/7921181 TodoViewController *contentViewControll ...
- js基础---->js中的消息框
可以在JavaScript 中创建三种消息框:警告框.确认框.提示框.今天我们就这几个框做一个介绍.我们笑着说再见,却深知再见遥遥无期. javascript消息框 一.警告框:警告框经常用于确保用户 ...
- ubuntu android studio kvm
Android studio 启动模拟器失败: Cannot launch AVD in emulator.Output:emulator: ERROR: x86 emulation currentl ...
- cocos2dx3.1从零学习(二)菜单、场景切换、场景传值
转:http://www.it165.net/pro/html/201406/16195.html 回顾一下上一篇的内容,我们已经学会了创建一个新的场景scene,添加sprite和label到层中, ...