zoukankan      html  css  js  c++  java
  • 科学计数法

    链接:http://www.nowcoder.com/pat/6/problem/4050

    题目描述

    科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分

    只有1位,小数部分至少有1位,该数字及其指数部分的正负号即使对正数也必定明确给出。



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

    输入描述:

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



    输出描述:

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

    输入例子:

    +1.23400E-03

    输出例子:

    0.00123400

     1 #include "iostream"
     2 #include <iomanip>
     3 #include <string.h>
     4 #include <string>
     5 #include <vector>
     6 #include <cmath>
     7 #include <cctype>
     8 #include <algorithm>
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     string str1, str2;
    14     cin >>str1;
    15     int i=0, posDot=0, count=0, flag=1;
    16     if(str1[0] == '-')    flag=0;
    17     for(i=1; i<str1.length(); ++i)
    18     {
    19         if(str1[i] == '.')    posDot = i;
    20         else    if(str1[i] == 'E')    {++i;    break;}
    21                 else    str2 += str1[i];
    22     }
    23     for(int j=i+1; j<str1.length(); ++j)
    24     {
    25         count = count*10+str1[j]-'0';
    26     }
    27     if(str1[i] == '-')
    28     {
    29         count = -count;
    30     }
    31     posDot += count;
    32     if(posDot < 0)
    33     {
    34         if(flag == 0) cout <<'-';
    35         cout <<"0.";
    36         posDot = -posDot;
    37         for(int i=0; i<(-count)-1; ++i)    cout <<0;
    38         cout <<str2 <<endl;
    39     }
    40     else
    41     {
    42         if(flag == 0) cout <<'-';
    43         int i;
    44         for(i=0; i<str2.length(); ++i)
    45         {
    46             if(i == posDot-1)    cout <<".";
    47             cout <<str2[i];
    48             
    49         }
    50         while(i < posDot-1) 
    51         {
    52             cout <<0;
    53             ++i;
    54         }
    55         cout <<endl;
    56     }
    57     return 0;
    58 }

    注意:当输入 -1.2E+10时,输入为-12000000000

  • 相关阅读:
    第六周
    第五周
    第四周
    第二周学习记录
    实验一 Linux初步认识
    java实验四
    java实验三
    为什么无密码认证能够有效
    关于父元素,子元素,同级元素的DOM操作技巧
    高效设计构建软件的十三条建议
  • 原文地址:https://www.cnblogs.com/mtc-dyc/p/4621784.html
Copyright © 2011-2022 走看看