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 ...
随机推荐
- POJ 1837 Balance(01背包变形, 枚举DP)
Q: dp 数组应该怎么设置? A: dp[i][j] 表示前 i 件物品放入天平后形成平衡度为 j 的方案数 题意: 有一个天平, 天平的两侧可以挂上重物, 给定 C 个钩子和G个秤砣. 2 4 - ...
- 在Extjs中动态增加控件
Ext.onReady(function () { Ext.QuickTips.init(); Ext.form.Field.prototype.msgTarget = 'side'; var aut ...
- hadoop程序MapReduce之SingletonTableJoin
需求:单表关联问题.从文件中孩子和父母的关系挖掘出孙子和爷奶关系 样板:child-parent.txt xiaoming daxiong daxiong alice daxiong jack 输出: ...
- Spring中通过构造方法传参数
1.实体类 package com.xbq.bean;import java.util.Timer; /** * @ClassName: Student * @Description: TODO 学生 ...
- m4a文件在iOS上的流媒体播放
Date: 2016-03-23 Title: m4a文件在iOS上的流媒体播放 Tags: m4a, mp4, iOS, Android URL: m4a-streaming-play-on-mob ...
- DiscuzX的目录权限设置1
经常有朋友遇到Discuz目录权限设置出错的问题,网上千奇百怪的教程非常多,所谓的终极安全的教程更是满天飞,各种所谓的安全加强软件也随处可见,可实际过程中发现,老手用不上,新手则只会因为这些东西徒增麻 ...
- Delphi使用ADO连接网络数据库,断网后重连问题
原始文章: https://blog.csdn.net/blog_jihq/article/details/11737699# 使用TADOConnection对象连接网络数据库(以MySQL为例), ...
- java基础---->java中Properties的使用
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支 持的配置文件,配置文件中很多变量是经常改变的,这样做也 ...
- Excel 2010 最熟悉的陌生功能:筛选器(将当前所选内容添加到筛选器)
使用excel2010版的同学,在进行筛选时,肯定都对这句话很熟悉:将当前所选内容添加到筛选器.但很多同学天天看到,却不知道什么是筛选器?它有什么作用. 其实,这里所指的筛选器就是储存筛选结果的一个虚 ...
- Express 框架的安装
从零开始用 Node.js 实现一个微博系统,功能包括路由控制.页面模板.数据库访问.用户注册.登录.用户会话等内容. Express 框架. MVC 设计模式. ejs 模板引擎 MongoDB 数 ...