• 【PAT(甲级)】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. //往后移动
    2. string turnback(string a,int b){
    3. int t = 0;string r;
    4. for(int i=0;isize();i++){
    5. if(a[i]=='.'){
    6. t = i;
    7. continue;
    8. }else{
    9. r+=a[i];
    10. }
    11. }
    12. if(t+b>=r.size()){
    13. for(int i = r.size();i
    14. r+='0';
    15. }
    16. return r;
    17. }else{
    18. r = r.substr(0,t)+r.substr(t,b)+'.'+r.substr(t+b,r.size()-t-b);//测试点4
    19. return r;
    20. }
    21. }

    往前移动如下:

    1. //往前移动:
    2. string turnfront(string a,int b){
    3. int t = 0;string r;
    4. for(int i=0;isize();i++){
    5. if(a[i]=='.'){
    6. t = i;
    7. continue;
    8. }
    9. else{
    10. r+=a[i];
    11. }
    12. }
    13. string k;
    14. if(t-b>0){
    15. for(int i = 0;i
    16. k+=r[i];
    17. }
    18. k+='.';
    19. for(int i=t-b;isize();i++){
    20. k+=r[i];
    21. }
    22. return k;
    23. }else{
    24. k+="0.";
    25. for(int i=0;i+t-b<0;i++){
    26. k+='0';
    27. }
    28. for(int i=0;isize();i++){
    29. k+=r[i];
    30. }
    31. return k;
    32. }
    33. }

    易错点:

    1. 注意移动时候的小数点位置问题,计算出错就会出现各种测试点的错误;

    代码:

    1. #include
    2. using namespace std;
    3. string turnback(string a,int b){
    4. int t = 0;string r;
    5. for(int i=0;isize();i++){
    6. if(a[i]=='.'){
    7. t = i;
    8. continue;
    9. }else{
    10. r+=a[i];
    11. }
    12. }
    13. if(t+b>=r.size()){
    14. for(int i = r.size();i
    15. r+='0';
    16. }
    17. return r;
    18. }else{
    19. r = r.substr(0,t)+r.substr(t,b)+'.'+r.substr(t+b,r.size()-t-b);//测试点4
    20. return r;
    21. }
    22. }
    23. string turnfront(string a,int b){
    24. int t = 0;string r;
    25. for(int i=0;isize();i++){
    26. if(a[i]=='.'){
    27. t = i;
    28. continue;
    29. }
    30. else{
    31. r+=a[i];
    32. }
    33. }
    34. string k;
    35. if(t-b>0){
    36. for(int i = 0;i
    37. k+=r[i];
    38. }
    39. k+='.';
    40. for(int i=t-b;isize();i++){
    41. k+=r[i];
    42. }
    43. return k;
    44. }else{
    45. k+="0.";
    46. for(int i=0;i+t-b<0;i++){
    47. k+='0';
    48. }
    49. for(int i=0;isize();i++){
    50. k+=r[i];
    51. }
    52. return k;
    53. }
    54. }
    55. int main(){
    56. string N;
    57. cin>>N;
    58. if(N[0]=='-'){
    59. int i = 1;string a;
    60. while(N[i]!='E'){
    61. a+=N[i];
    62. i++;
    63. }
    64. i++;
    65. int num = 0;
    66. if(N[i]=='+'){
    67. int t = i+1;
    68. for(int j = t;jsize();j++){
    69. num = num*10+stoi(N.substr(j,1));
    70. }
    71. cout<<"-"<<turnback(a,num);
    72. }else{
    73. int t = i+1;
    74. for(int j = t;jsize();j++){
    75. num = num*10+stoi(N.substr(j,1));
    76. }
    77. cout<<"-"<<turnfront(a,num);
    78. }
    79. }else{
    80. int i = 1;string a;
    81. while(N[i]!='E'){
    82. a+=N[i];
    83. i++;
    84. }
    85. i++;
    86. int num = 0;
    87. if(N[i]=='+'){
    88. int t = i+1;
    89. for(int j = t;jsize();j++){
    90. num = num*10+stoi(N.substr(j,1));
    91. }
    92. cout<<turnback(a,num);
    93. }else{
    94. int t = i+1;
    95. for(int j = t;jsize();j++){
    96. num = num*10+stoi(N.substr(j,1));
    97. }
    98. cout<<turnfront(a,num);
    99. }
    100. }
    101. return 0;
    102. }

  • 相关阅读:
    2023上海初中生古诗文大会复赛12月2日举行,关键事项为您划重点
    图扑软件 3D 组态编辑器,低代码零代码构建数字孪生工厂
    Python语言学习实战-内置函数sorted()的使用(附源码和实现效果)
    【AGC】目前云调试对于RPK软件包是否支持、素材测试服务如何使用、崩溃服务启用接入异常
    【具身智能模型2】RT-1: Robotics Transformer for Real-World Control at Scale
    Debezium系列之:使用逻辑命名空间为不同的表做个性化设置
    Laravel 响应对象深度解析:构建动态 HTTP 响应
    温度及pH敏感性聚乙烯醇/羧甲基壳聚糖水凝胶/金银花多糖/薄荷多糖/O-羧甲基壳聚糖水凝胶
    盲盒网站遭遇DDoS攻击,高防ip是如何起到安全防护的?
    DPDK Ring
  • 原文地址:https://blog.csdn.net/weixin_55202895/article/details/126940772