zoukankan      html  css  js  c++  java
  • PAT甲级——A1073 Scientific Notation

    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

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int main()
     5 {
     6     string str, f1, num1, num2, f2, num3, res = "";
     7     cin >> str;
     8     int nDot, E, Exp = 0;
     9     f1 = str[0];
    10     nDot = str.find('.');
    11     num1 = str[nDot - 1];//第一位数字
    12     E = str.find('E');
    13     num2.assign(str.begin() + nDot + 1, str.begin() + E);//小数点后面的数字
    14     f2 = str[E + 1];
    15     num3.assign(str.begin() + E + 2, str.end());//指数
    16     for (int i = 0; i < num3.length(); ++i)//计算指数
    17         Exp = Exp * 10 + num3[i] - '0';
    18     if (f1 == "-")
    19         res += "-";
    20     if (f2 == "-")
    21     {
    22         res += "0.";
    23         res.insert(res.end(), Exp - 1, '0');//中间插入0
    24     }
    25     else if (f2 == "+")
    26     {
    27         if (num2.length() <= Exp)//小数位不足,则直接末尾加0;
    28             num2.insert(num2.end(), Exp - num2.length(), '0');
    29         else//小数位多余幂次,则小数点后移
    30             num2.insert(num2.begin() + Exp, 1, '.');
    31     }
    32     res += num1 + num2;
    33     cout << res << endl;
    34     return 0;
    35 }
  • 相关阅读:
    Dan Gillmor总结微软付费平息纠纷历史
    从一份GIS试题谈GIS开发人员的技能和招聘
    转载:赵勖予,废旧电脑配件的新用法next
    百度败诉MP3搜索遭封杀
    给人也装一个中断系统
    ArcGIS Server 9.2的一些新特征
    基于MA和CA的交通空间决策模型
    Modeling Our World 读书笔记
    卸载了NewsGator,开始使用Gougou
    OpenOffice.org 2.0即将发布
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11313511.html
Copyright © 2011-2022 走看看