• 【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. }

  • 相关阅读:
    StrictMode分析Activity泄漏-StrictMode原理(3)
    Linux命令(96)之seq
    两个栈实现队列
    【Spring】Spring的创建和使用手术刀剖析
    Android逆向题解 攻防世界难度4- Android2.0
    Gartner:55%的组织,正在试用ChatGPT等生成式AI
    jQuery对于链和捕获的实战研究
    2 .项目构建(实时计算框架和监控kafka,flink的工具)
    云计算-存算一体-EDA-技术杂谈
    读书郎通过上市聆讯:平板业务毛利率走低,2021年利润同比下滑11%
  • 原文地址:https://blog.csdn.net/weixin_55202895/article/details/126940772