1073. Scientific Notation (20)

缺乏、安全感 2022-05-30 01:56 206阅读 0赞

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. +1.23400E-03

Sample Output 1:

  1. 0.00123400

Sample Input 2:

  1. -1.2E+10

Sample Output 2:

  1. -12000000000

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. char arr[100001];
  4. int main()
  5. {
  6. int i,j,n,m,k,e,p;
  7. scanf("%s",arr);
  8. n=strlen(arr);
  9. for(i=0;i<n;i++)
  10. {
  11. if(arr[i]=='E')
  12. break;
  13. }
  14. e=0;
  15. p=i;
  16. for(j=p+2;j<n;j++)
  17. {
  18. e=(e*10+(arr[j]-'0'));
  19. }
  20. if(arr[p+1]=='-')
  21. {
  22. e=-e;
  23. }
  24. //printf("%d\n",e);
  25. if(arr[0]=='-')
  26. printf("-");
  27. if(e<0)
  28. {
  29. printf("0.");
  30. while(e<-1)
  31. {
  32. printf("0");
  33. e++;
  34. }
  35. for(i=1;i<p;i++)
  36. {
  37. if(arr[i]!='.')
  38. printf("%c",arr[i]);
  39. }
  40. }
  41. else if(e>0)
  42. {
  43. printf("%c",arr[1]);
  44. for(i=3;i<p&&e>0;i++)
  45. {
  46. printf("%c",arr[i]);
  47. e--;
  48. }
  49. if(i<p)
  50. {
  51. printf(".");
  52. for(;i<p;i++)
  53. {
  54. printf("%c",arr[i]);
  55. }
  56. }
  57. else
  58. {
  59. while(e>0)
  60. {
  61. printf("0");
  62. e--;
  63. }
  64. }
  65. }
  66. else
  67. {
  68. for(i=1;i<p;i++)
  69. {
  70. printf("%c",arr[i]);
  71. }
  72. }
  73. return 0;
  74. }

发表评论

表情:
评论列表 (有 0 条评论,206人围观)

还没有评论,来说两句吧...

相关阅读