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.
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.
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.
+1.23400E-03
0.00123400
-1.2E+10
-12000000000
题目给出科学计数法的数字,让我们输出它的实际数字。很明显的一道字符串处理问题,分析一下题目,我们发现主要解决的是小数点‘.’的位置问题。根据符号的“+”或者“-”来处理它是往前移动或者往后移动下面就是前后移动的代码:
往后移动如下:
- //往后移动
- string turnback(string a,int b){
- int t = 0;string r;
- for(int i=0;i
size();i++){ - if(a[i]=='.'){
- t = i;
- continue;
- }else{
- r+=a[i];
- }
- }
- if(t+b>=r.size()){
- for(int i = r.size();i
- r+='0';
- }
- return r;
- }else{
- r = r.substr(0,t)+r.substr(t,b)+'.'+r.substr(t+b,r.size()-t-b);//测试点4
- return r;
- }
- }
往前移动如下:
- //往前移动:
- string turnfront(string a,int b){
- int t = 0;string r;
- for(int i=0;i
size();i++){ - if(a[i]=='.'){
- t = i;
- continue;
- }
- else{
- r+=a[i];
- }
- }
- string k;
- if(t-b>0){
- for(int i = 0;i
- k+=r[i];
- }
- k+='.';
- for(int i=t-b;i
size();i++){ - k+=r[i];
- }
- return k;
- }else{
- k+="0.";
- for(int i=0;i+t-b<0;i++){
- k+='0';
- }
- for(int i=0;i
size();i++){ - k+=r[i];
- }
- return k;
- }
- }
易错点:
1. 注意移动时候的小数点位置问题,计算出错就会出现各种测试点的错误;
代码:
- #include
- using namespace std;
-
- string turnback(string a,int b){
- int t = 0;string r;
- for(int i=0;i
size();i++){ - if(a[i]=='.'){
- t = i;
- continue;
- }else{
- r+=a[i];
- }
- }
- if(t+b>=r.size()){
- for(int i = r.size();i
- r+='0';
- }
- return r;
- }else{
- r = r.substr(0,t)+r.substr(t,b)+'.'+r.substr(t+b,r.size()-t-b);//测试点4
- return r;
- }
- }
-
- string turnfront(string a,int b){
- int t = 0;string r;
- for(int i=0;i
size();i++){ - if(a[i]=='.'){
- t = i;
- continue;
- }
- else{
- r+=a[i];
- }
- }
- string k;
- if(t-b>0){
- for(int i = 0;i
- k+=r[i];
- }
- k+='.';
- for(int i=t-b;i
size();i++){ - k+=r[i];
- }
- return k;
- }else{
- k+="0.";
- for(int i=0;i+t-b<0;i++){
- k+='0';
- }
- for(int i=0;i
size();i++){ - k+=r[i];
- }
- return k;
- }
- }
-
- int main(){
- string N;
- cin>>N;
- if(N[0]=='-'){
- int i = 1;string a;
- while(N[i]!='E'){
- a+=N[i];
- i++;
- }
- i++;
- int num = 0;
- if(N[i]=='+'){
- int t = i+1;
- for(int j = t;j
size();j++){ - num = num*10+stoi(N.substr(j,1));
- }
- cout<<"-"<<turnback(a,num);
- }else{
- int t = i+1;
- for(int j = t;j
size();j++){ - num = num*10+stoi(N.substr(j,1));
- }
- cout<<"-"<<turnfront(a,num);
- }
- }else{
- int i = 1;string a;
- while(N[i]!='E'){
- a+=N[i];
- i++;
- }
- i++;
- int num = 0;
- if(N[i]=='+'){
- int t = i+1;
- for(int j = t;j
size();j++){ - num = num*10+stoi(N.substr(j,1));
- }
- cout<<turnback(a,num);
- }else{
- int t = i+1;
- for(int j = t;j
size();j++){ - num = num*10+stoi(N.substr(j,1));
- }
- cout<<turnfront(a,num);
- }
- }
- return 0;
- }
-
相关阅读:
CSS BFC介绍
javaWeb使用spring框架时在配置上的编程技巧
高中生可发表论文的学术期刊涵盖TCR历史期刊
stm32f4dma串口收数卡死
从结构上浅谈FPGA实现逻辑的原理
达梦数据库学习操作记录
回溯算法的奥秘(LeetCode),组合77题、216题、40题
人机界面在石油钻井工程中的应用:如何搭建钻井工程参数监测系统?
SLAM中旋转向量(旋转轴/旋转角)、旋转矩阵、四元数、李代数的相互转化(附C++ Eigen库代码实例)
C++之函数模板、类模板、模板的特化
-
原文地址:https://blog.csdn.net/weixin_55202895/article/details/126940772
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU