题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805413520719872

又是科学计数法的题,这种题是真的麻烦。。

思路

  1. 不管原来的数怎么样,最后一定是变成0.XXXXX*10^X的形式,这样我们只要把主体和指数求出来就行了。
  2. 然后又分为两种情况,一种是整数部分大于0的情况,另外一种是整数部分为0的情况,然后还要根据情况,去除先导零
    1. 整数部分为0时,本体就是小数点后面n位非零位,指数部分就是非零位到小数点的0的个数
    2. 整数部分不为0时,本体是前n位非零位,指数则是小数点之前非零位的个数

代码

  1. #include<string>
  2. #include<algorithm>
  3. #include<iostream>
  4. using namespace std;
  5. int n;
  6. string change(string a, int &e){
  7. //去除前导零
  8. int k = 0;
  9. while(a.size() > 0 && *a.begin() == '0'){
  10. a.erase(a.begin());
  11. }
  12. ////////////////////////////////////////////////////////////
  13. if(a[0] == '.'){
  14. //整数部分为0
  15. a.erase(a.begin());
  16. while(a.length() > 0 && a[0] == '0'){//去掉小数点后面的0
  17. a.erase(a.begin());
  18. e--;
  19. }
  20. }
  21. ////////////////////////////////////////////////////////////
  22. else {
  23. while(k < a.length() && a[k] != '.'){//去除前导零以后寻找后面的小数点
  24. k++;
  25. e++;
  26. }
  27. if(k < a.length()){//遇到了小数点
  28. a.erase(a.begin() + k);//把小数点擦了
  29. }
  30. }
  31. ////////////////////////////////////////////////////////////
  32. if(a.length() == 0){//擦了0和小数点以后没有了,说明原来的数就是0
  33. e = 0;
  34. }
  35. int num = 0;
  36. k = 0;
  37. string res;
  38. while(num < n){
  39. if(k < a.length()) res += a[k++];
  40. else res += '0';
  41. num++;
  42. }
  43. return res;//res是本体部分
  44. }
  45. int main(){
  46. string a, b, ac, bc;
  47. scanf("%d", &n);
  48. cin>>a>>b;
  49. int ea = 0, eb = 0;
  50. ac = change(a, ea);
  51. bc = change(b, eb);
  52. if (ac == bc && ea == eb){
  53. cout<<"YES 0."<<ac<<"*10^"<<ea<<endl;
  54. }
  55. else cout<<"NO 0."<<ac<<"*10^"<<ea<<" 0."<<bc<<"*10^"<<eb<<endl;
  56. return 0;
  57. }