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 ...
随机推荐
- Mockito单元测试框架学习
基本使用方法: http://zhongl.iteye.com/blog/296136 一.问题:如何将mock的类自动注入到待测类,特别是在没有setter方法的情况下. 解答: 前提:待测的ser ...
- 阿里巴巴CI/CD之分层自动化
一佛是阿里巴巴B2B事业群高级产品经理.从事多年互联网系统的研发和测试工作,目前主要负责云效分层自动化测试的产品设计.因为自动化测试在实践过程中,总是碰到各种各样的问题,导致进入自动化测试盲区.所以, ...
- Maven War包 POM配置文件
如何为你的Web程序(war包设定配置文件) 约定 http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering ...
- easyui------自动合并行
转载: http://www.cnblogs.com/xiangzhong/p/5088259.html#undefined 1.引入插件 $.extend($.fn.datagrid.methods ...
- nohub和重定向文件
1.如果使用远程连接的Linux的方式并想后台运行执行如下命令: 格式:nohup <程序名> & 比如:nohup /usr/local/collection/bin/start ...
- js方法区分IE浏览器和非IE浏览器
可以从IE特有的方法和非IE特有的方法来区分不同的浏览器 1.为元素添加事件监听: 非IE:.addEventListener("click",show,false)//第三个参数 ...
- web 前端规范实例
<!DOCTYPE html> <html> <head> <title>tmall</title> <!-- 为了被搜索引擎作为流量 ...
- 打造不死的asp木马
作者:黑色记忆本文已发表于<黑客X档案>杂志第十期 版权归<黑客X档案>所有 转载请注明版权 想不到,前几天我才发现,我千辛万苦收集的asp木马,居然没有几个不被Kill的.常 ...
- 深入浅出Docker(二):Docker命令行探秘
1. Docker命令行 Docker官方为了让用户快速了解Docker,提供了一个交互式教程,旨在帮助用户掌握Docker命令行的使用方法.但是由于Docker技术的快速发展,此交互式教程已经无法满 ...
- 【Linux系列】find命令使用
Linux下find命令在目录结构中搜素文件,病执行制定的操作. 一.命令格式 find pathname -options[-print -exec -ok] 二.命令功能 用于在文件树种查找文件, ...