PAT---B1024/A1073. 科学计数法 (20)

题目要求:
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]”.”[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数A,请编写程序按普通数字表示法输出A,并保证所有有效位都被保留。

输入格式:

每个输入包含1个测试用例,即一个以科学计数法表示的实数A。该数字的存储长度不超过9999字节,且其指数的绝对值不超过9999。

输出格式:

对每个测试用例,在一行中按普通数字表示法输出A,并保证所有有效位都被保留,包括末尾的0。

输入样例1:
+1.23400E-03
输出样例1:
0.00123400
输入样例2:
-1.2E+10
输出样例2:
-12000000000

解题思路:先将底数和指数分开,然后根据指数的大小对底数进行寻找相应的规律然后输出

参考代码:

#include <iostream>
using namespace std;
int main()
{
    string s;
    cin >> s;
    int E_place=0;
    if(s[0]== '-')
        cout <<s[0];
    while(s[E_place]!='E')  // E_place即为输入的字符串中E的位置
        E_place++;
    //使用exponent来存储E后的字符串换算成10进制的数,即用来存储科学计数法的指数
    //这步的计算先不论正负号,正负号稍后讨论
    int exponent = 0;
    for(int i = E_place+2;i<s.length();i++)
        exponent = exponent*10+(int)s[i]-'0';
    //如果指数是负数,则对刚刚计算的值取相反数
    if(s[E_place+1] == '-')
        exponent = -exponent;
    //根据指数为正负或者是0来判断该如何输出
    if(exponent == 0)
    {
        for(int i=1;i<E_place;i++)
            cout <<s[i];
    }
    else if(exponent <0)
    {
        cout <<"0.";
        for(int i=0;i<-exponent-1;i++)
            cout <<"0";
        cout <<s[1];
        for(int i=3;i<E_place;i++)
            cout <<s[i];
      }
    else 
    {
        for(int i=1;i<E_place;i++)
        {
            if(s[i] == '.')
                continue;
            cout <<s[i];
            //需要在exponent+2处打印小数点,但又不能在一个数的最后一位打印小数点(最后一位就是exponent等于小数的位数,小数的位数就是E_place-3)
            if((i == exponent+2)&&(E_place-3!=exponent))
                   cout <<'.';
        }
        for(int i=0;i<exponent-(E_place-3);i++)
            cout <<"0";
    }

    return 0;
}

附加:本人在网上又看到了另一种解题的方法,然而PAT系统检测有监测点错误,本人苦思冥想也无从解决,因此本人将这串代码在此贴出,望各位看官看到后能给予您宝贵的意见

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main()
{
    char str[11000];
    cin >>str;
    int length = strlen(str);
    int E_place = 0;
    if(str[0]=='-')
        cout <<"-";
    while(str[E_place]!='E')
        E_place++;
    int exponent = 0;  //exponent 为科学记数法的指数

    for(int i = E_place+2;i<length;i++)
        exponent = exponent*10+str[i]-'0';

    if(exponent == 0)
    {
        for(int i=1;i<E_place;i++)
        cout <<str[i];
    }
    else if(str[E_place+1]=='-')
    {
        cout <<"0.";
        for(int i=0;i<exponent-1;i++)
            cout <<"0";
        cout <<str[1];
        for(int i=3;i<E_place;i++)
            cout <<str[i];
    }
    else
    {
        for(int i=1;i<E_place;i++)
        {
            if(str[i] == '.')
                continue;
            cout <<str[i];
            if(i == E_place+2&&exponent-3!=E_place)
                cout <<".";
        }
        for(int i=0;i<exponent-(E_place-3);i++)
            cout <<"0";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenSSH_9.2p1 Debian-2, OpenSSL 3.0.9 30 May 2023 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files debug1: /etc/ssh/ssh_config line 21: Applying options for * debug1: Connecting to lxslc702.ihep.ac.cn [2401:de00:2:332::186] port 22. debug1: Connection established. debug1: identity file /home/fyf/.ssh/id_rsa type -1 debug1: identity file /home/fyf/.ssh/id_rsa-cert type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa_sk type -1 debug1: identity file /home/fyf/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /home/fyf/.ssh/id_ed25519 type -1 debug1: identity file /home/fyf/.ssh/id_ed25519-cert type -1 debug1: identity file /home/fyf/.ssh/id_ed25519_sk type -1 debug1: identity file /home/fyf/.ssh/id_ed25519_sk-cert type -1 debug1: identity file /home/fyf/.ssh/id_xmss type -1 debug1: identity file /home/fyf/.ssh/id_xmss-cert type -1 debug1: identity file /home/fyf/.ssh/id_dsa type -1 debug1: identity file /home/fyf/.ssh/id_dsa-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_9.2p1 Debian-2 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4 debug1: compat_banner: match: OpenSSH_7.4 pat OpenSSH_7.4* compat 0x04000006 debug1: Authenticating to lxslc702.ihep.ac.cn:22 as 'fanyufan' debug1: load_hostkeys: fopen /home/fyf/.ssh/known_hosts: No such file or directory debug1: load_hostkeys: fopen /home/fyf/.ssh/known_hosts2: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ssh-ed25519 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY Connection closed by 2401:de00:2:332::186 port 22
最新发布
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值