• 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

    字符串模拟,需要分类讨论各种情况:

    1)指数为正

            a)指数大于等于小数位数

                    后面可能要补零

            b)指数小于小数位数

                    注意原数个位为零和不为零的情况不同。

    2)指数为负 

            需要补零

    3)指数为零

            直接提取输出

    1. #include
    2. #include
    3. using namespace std;
    4. int main() {
    5. string a, b = "";
    6. int ex = 0, flag, pos;
    7. cin >> a;
    8. if (a[0] == '-') {
    9. cout << '-';
    10. }
    11. for (int i = 1; i < a.size(); i++) {
    12. if (a[i] == 'E') {
    13. pos = i;
    14. if (a[i + 1] == '+') {
    15. flag = 1;
    16. } else if (a[i + 1] == '-') {
    17. flag = -1;
    18. }
    19. for (int j = i + 2; j < a.size(); j++) {
    20. ex = ex * 10 + a[j] - '0';
    21. }
    22. break;
    23. }
    24. }
    25. if (ex == 0) {
    26. flag = 0;
    27. }
    28. if (flag == 1) {
    29. int num = pos - 3; //统计小数点后有几位
    30. if (num <= ex) {
    31. for (int i = 1; i < pos; i++) {
    32. if (a[i] != '.') {
    33. cout << a[i];
    34. }
    35. }
    36. for (int i = 0; i < ex - num; i++) {
    37. cout << 0;
    38. }
    39. } else {
    40. int t = 0;
    41. if (a[1] != '0') {
    42. b += a[1];
    43. }
    44. for (int i = 3; i < pos; i++) {
    45. b += a[i];
    46. t++;
    47. if (t == ex) {
    48. b += '.';
    49. }
    50. }
    51. bool f = 0;
    52. for (int i = 0; i < b.size(); i++) {
    53. if (b[i] == '0' && b[i + 1] == '.' || b[i] != '0') {
    54. f = 1;
    55. }
    56. if (f == 1) {
    57. cout << b[i];
    58. }
    59. }
    60. }
    61. } else if (flag == -1) {
    62. cout << "0.";
    63. for (int i = 1; i < ex; i++) {
    64. cout << 0;
    65. }
    66. for (int i = 1; i < pos; i++) {
    67. if (a[i] != '.') {
    68. cout << a[i];
    69. }
    70. }
    71. } else if (flag == 0) {
    72. for (int i = 1; i < pos; i++) {
    73. cout << a[i];
    74. }
    75. }
    76. return 0;
    77. }

     

  • 相关阅读:
    安装keras、tensorflow
    【eolink】一个http访问例子
    JAVA JDBC 概述
    Java OutputStream.write()的功能简介说明
    深入了解 JavaScript 语法错误以及如何防止它们
    深度剖析C语言指针笔试题 Ⅱ
    Mysql 中如何导出数据?
    高防CDN安全防护系统在业务方面的应用
    01-简历设计(模板1)
    区块链与农业溯源上的利与弊
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126482656