当前位置: 代码迷 >> 综合 >> [PAT B1024/A1073]Scientific Notation
  详细解决方案

[PAT B1024/A1073]Scientific Notation

热度:50   发布时间:2023-12-15 06:23:07.0

[PAT B1024/A1073]Scientific Notation

本题是甲级1073和乙级1024题,所以先放英文题目,如果是要做乙级题的话,可以不看英文,直接看下面的中文题

题目描述

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.

输入格式

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

+1.23400E-03

输出样例1

0.00123400

输入样例2

-1.2E+10

输出样例2

-12000000000

--------------------------中文原题---------------------------

题目描述

1024 科学计数法 (20 分)科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [±][1-9].[0-9]+E[±][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。
现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。

输入格式

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

输出格式

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

解析

  1. 题目解释,就是输入一个科学计数法表示的数,要我们以非科学计数法的方式输出它,并保证所有的有效位都保留
  2. 主要是涉及到了一些关于字符串处理的技巧及函数,关于字符串处理的有些函数可以参考我的博客
    工欲善其事必先利其器——盘点PAT中非常好用的工具(持续更新)
    3.具体代码如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int pos = 1, index;string str, subs, ans;cin >> str;while (str[pos++] != 'E');//截取到E为止的字符串的子串pos--;subs = str.substr(1, pos - 1);index = stoi(str.substr(pos + 1));  //stoi函数,将string类型转换成int类型if (index < 0) {
    if (str[0] == '-') cout << "-";  //如果是负数,首先输出-cout << "0.";for (int i = 0; i < abs(index) - 1; i++) cout << '0';int i = 0;while (i < subs.length()) {
    if (subs[i] != '.') cout << subs[i];i++;}}else {
    if (str[0] == '-') cout << "-";if (index < subs.length() - 2) {
    cout << subs[0];for (int i = 2; i < 2 + index; i++) cout << subs[i];cout << ".";for (int i = 2 + index; i < subs.length(); i++) cout << subs[i];}else {
    int i = 0;while (i < subs.length()) {
    if (subs[i] != '.') cout << subs[i];i++;}for (int i = 0; i < index - subs.length() + 2; i++) cout << "0";}}return 0;
}

水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!