• 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 }
  • 相关阅读:
    myBatis之事务管理
    关于Spring事务回滚的问题
    mysql中的多行查询结果合并成一个
    mybatis传递参数到mapping.xml
    EasyUI 中 DataGrid 控件 列 如何绑定对象中的属性
    写给java开发的运维笔记
    SpringMVC学习系列(11) 之 表单标签
    eclipse 安装svn插件
    linux(centos)搭建SVN服务器
    JavaServer Faces 2.0 can not be installed解决方案
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11313511.html
Copyright © 2020-2023  润新知