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] != '\0'; i++){
12         if(num[i] == 'E'){
13             pos = i;
14             num[i] = '\0';
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] = '\0';
31          }
32          if(num[strlen(num) - 1] == '.')
33              num[strlen(num) - 1] = '\0';
34          if(c_sign == '-')
35             printf("%c", c_sign);
36          for(j = 0; num[j] != '\0'; j++)
37              if(num[j] == '0' && num[j + 1] != '0' || num[j] != '0')
38                  break;
39          for( ; num[j] != '\0'; 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] != '\0'; 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型存储,底数用字符串存储。再分为小数点左移与右移来分类处理。其中向右移动时,如果小数点移动后位于最后一位,则不输出小数点。

转载于:https://www.cnblogs.com/zhuqiwei-blog/p/8441588.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Q21: Which of the following is a valid user-defined output stream manipulator header? a. ostream& tab( ostream& output ) b. ostream tab( ostream output ) c. istream& tab( istream output ) d. void tab( ostream& output ) Q22: What will be output by the following statement? cout << showpoint << setprecision(4) << 11.0 << endl; a. 11 b. 11.0 c. 11.00 d. 11.000 Q23: Which of the following stream manipulators causes an outputted number’s sign to be left justified, its magnitude to be right justified and the center space to be filled with fill characters? a. left b. right c. internal d. showpos Q24: Which of the following statements restores the default fill character? a. cout.defaultFill(); b. cout.fill(); c. cout.fill( 0 ); d. cout.fill( ' ' ); Q25: When the showbase flag is set: a. The base of a number precedes it in brackets. b. Decimal numbers are not output any differently. c. "oct" or "hex" will be displayed in the output stream. d. Octal numbers can appear in one of two ways. Q26: What will be output by the following statements? double x = .0012345; cout << fixed << x << endl; cout << scientific << x << endl; a. 1.234500e-003 0.001235 b. 1.23450e-003 0.00123450 c. .001235 1.234500e-003 d. 0.00123450 1.23450e-003 Q27: Which of the following outputs does not guarantee that the uppercase flag has been set? a. All hexadecimal numbers appear in the form 0X87. b. All numbers written in scientific notation appear the form 6.45E+010. c. All text outputs appear in the form SAMPLE OUTPUT. d. All hexadecimal numbers appear in the form AF6. Q28: Which of the following is not true about bool values and how they're output with the output stream? a. The old style of representing true/false values used -1 to indicate false and 1 to indicate true. b. A bool value outputs as 0 or 1 by default. c. Stream manipulator boolalpha sets the output stream to display bool values as the strings "true" and "false". d. Both boolalpha and noboolalpha are “sticky” settings.
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值