zoukankan      html  css  js  c++  java
  • 1073 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

    题意:

      将数字由科学计数法表示改为普通数字表示方法。

    思路:

      模拟。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     string in;
     7     cin >> in;
     8     if (in[0] == '-') cout << '-';
     9     int pos = in.find('E');
    10     string coef = in.substr(1, pos - 1);
    11     string exp = in.substr(pos + 2);
    12     int e = stoi(exp);
    13     int c = stoi(coef);
    14     int len = coef.length();
    15     if (in[pos + 1] == '+') {
    16         if (len - 2 > e) {
    17             cout << coef[0];
    18             for (int i = 2; i < len; ++i) {
    19                 if (i == e + 2) cout << '.';
    20                 cout << coef[i];
    21             }
    22             cout << endl;
    23         } else {
    24             cout << coef[0];
    25             for (int i = 2; i < len; ++i) cout << coef[i];
    26             for (int i = 0; i < e - len + 2; ++i) cout << '0';
    27         }
    28     } else {
    29         if (e == 0) cout << 1 << endl;
    30         for (int i = 0; i < e; ++i) {
    31             cout << '0';
    32             if (i == 0) cout << '.';
    33         }
    34         cout << coef[0];
    35         for (int i = 2; i < len; ++i) cout << coef[i];
    36         cout << endl;
    37     }
    38     return 0;
    39 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    如何应对一些无语的面试题
    5W随想
    操作系统-文件的结构以及文件管理
    计算机网络--第二章--物理层笔记
    第一章计算机网络概述---OSI七层网络模型
    IDEA使用Maven创建webapp骨架无法创建Servlet文件与无法使用@WebServlet实现注解问题解决
    RabbitMQ常用的几种消息模型
    算法入门(二)队列
    Java线程安全与锁优化,锁消除,锁粗化,锁升级
    Centos7安装RabbitMQ详细教程
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12819836.html
Copyright © 2011-2022 走看看