zoukankan      html  css  js  c++  java
  • 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 file 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<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<string.h>
     5 using namespace std;
     6 int main(){
     7      char c_sign, p_sign, num[20000];
     8      int pos, tag = 0, power = 0;
     9      scanf("%c", &c_sign);
    10      scanf("%s", num);
    11      for(int i = 0; num[i] != ''; i++){
    12         if(num[i] == 'E'){
    13             pos = i;
    14             num[i] = '';
    15             p_sign = num[++i];
    16             tag = 1;
    17             continue;
    18         }
    19         if(tag == 1)
    20             power = power * 10 + num[i] - '0';
    21      }
    22      if(p_sign == '+'){
    23          int j;
    24          for(j = 1; j < pos - 1 && j <= power; j++)
    25             swap(num[j], num[j + 1]);
    26          if(j <= power){
    27             while(j <= power){
    28                 num[j++] = '0';
    29             }
    30             num[j] = '';
    31          }
    32          if(num[strlen(num) - 1] == '.')
    33              num[strlen(num) - 1] = '';
    34          if(c_sign == '-')
    35             printf("%c", c_sign);
    36          for(j = 0; num[j] != ''; j++)
    37              if(num[j] == '0' && num[j + 1] != '0' || num[j] != '0')
    38                  break;
    39          for( ; num[j] != ''; j++)
    40              printf("%c", num[j]);
    41      }
    42      if(p_sign == '-'){
    43         int j;
    44         if(c_sign == '-')
    45             printf("%c", c_sign);
    46         if(power != 0){
    47             swap(num[0], num[1]);
    48             printf("0.");
    49             for(j = 0; j < power - 1; j++)
    50                 printf("0");
    51             for(j = 1; num[j] != ''; j++)
    52                 printf("%c", num[j]);
    53         }else{
    54             printf("%s", num);
    55         }
    56      }
    57      cin >> num;
    58      return 0;
    59 }
    View Code

    总结:

    1、首先由题意可知,底数有可能非常长,最终得到的数字长度可达9999B,为保证能全部输出,只能采用字符串处理的方式,而不能直接用long型计算得出。

    2、基本思路:将整个数字分为四部分,底数的符号,指数的符号,底数,指数。其中指数范围较小,可以用int型存储,底数用字符串存储。再分为小数点左移与右移来分类处理。其中向右移动时,如果小数点移动后位于最后一位,则不输出小数点。

  • 相关阅读:
    1.python的一些规范
    linux 命令总结
    【背包专题】D
    【算法入门竞赛经典】【7.2枚举排列】
    【练习赛补题】问题 E: 花生采摘 【模拟】
    【背包专题】B
    【背包专题】A
    【ACM对拍程序~】
    【背包专题】E
    河南省第七届大学生程序设计竞赛 问题 A: 物资调度【简单dfs】
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8441588.html
Copyright © 2011-2022 走看看