PAT甲级1060 Are They Equal【模拟】

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

题意

给定两个数,表示成0.xxxxx*10^k次这样的科学记数法之后,判断小数点后的n位是否相同

思路

分类大讨论。细节要考虑清楚。因为最多是100位所以要用字符串处理。

首先我们要知道这两个数的小数点在什么位置(digita,digtb),如果没有小数点就默认在最后一位。

然后我们还需要知道有没有前导零(firsta,firstb),比如0.001,这样的数小数点也是要挪的。

所以*10^k中的k应该是$digita - firsta$,如果结果是负数还应该加一。

然后是输出前面的东西。要特别判断一下他们是不是0.

把输出的小数点之后的数字重新存储一下,如果不到n位就在后面补0

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<map>
  4 #include<set>
  5 #include<iostream>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<vector>
  9 #include<cmath> 
 10 #include<stack>
 11 #include<queue>
 12 
 13 #define inf 0x7fffffff
 14 using namespace std;
 15 typedef long long LL;
 16 typedef pair<string, string> pr;
 17 
 18 int n;
 19 char a[105], b[105]; 
 20 
 21 int main()
 22 {
 23     scanf("%d", &n);
 24     scanf("%s%s", a, b);
 25     int tmpa[105], tmpb[105];
 26     int diga = -1, digb = -1;
 27     int lena = strlen(a), lenb = strlen(b);
 28     int da = 0, db = 0;
 29     int firsta = -1, firstb = -1;
 30     for(int i = 0; i < lena; i++){
 31         if(a[i] == '.'){
 32             diga = i;
 33             continue;
 34         }
 35         if(firsta == -1 && a[i] != '0'){
 36             firsta = i;
 37         }
 38         if(diga != -1 && firsta != -1)break; 
 39     }
 40     if(diga == -1)diga = lena;
 41     if(firsta != -1){
 42         diga -= firsta;
 43         if(diga < 0)diga++;
 44     }
 45 //    if(diga < 0){
 46 //        while(da < abs(diga)){
 47 //            tmpa[da++] = 0;
 48 //        }
 49 //    }
 50     for(int i = max(firsta, 0); i < lena; i++){
 51         if(a[i] == '.'){
 52             continue;
 53         }
 54         tmpa[da++] = a[i] - '0';
 55     }
 56     while(da < n){
 57         tmpa[da++] = 0;
 58     }
 59     
 60     
 61     for(int i = 0; i < lenb; i++){
 62         if(b[i] == '.'){
 63             digb = i;
 64             continue;
 65         }
 66         if(firstb == -1 && b[i] != '0'){
 67             firstb = i;
 68         }
 69     }
 70     if(digb == -1){
 71         digb = lenb;
 72     }
 73     if(firstb != -1){
 74         digb -= firstb;
 75         if(digb < 0)digb++;
 76     }
 77 //    if(digb < 0){
 78 //        while(db < abs(digb)){
 79 //            tmpb[db++] = 0;
 80 //        }
 81 //    }
 82     for(int i = max(0, firstb); i < lenb; i++){
 83         if(b[i] == '.')continue;
 84         tmpb[db++] = b[i] - '0';
 85     }
 86     while(db < n){
 87         tmpb[db++] = 0;
 88     }
 89     
 90     //cout<<firsta<<endl<<firstb<<endl;
 91     //cout<<diga<<endl<<digb<<endl;
 92     
 93     if(diga != digb && (firsta != -1 || firstb != -1) && (firsta < n || firstb < n)){
 94         printf("NO 0.");
 95         if(firsta == -1 || firsta >= n){
 96             for(int i = 0; i < n; i++){
 97                 printf("0");
 98             }
 99             printf("*10^0 0.");
100         }
101         else{
102             for(int i = 0; i < n; i++){
103                 printf("%d", tmpa[i]);
104             }
105             printf("*10^%d 0.", diga);
106         }
107         if(firstb == -1 || firstb >= n){
108             for(int i = 0; i < n; i++){
109                 printf("0");
110             }
111             printf("*10^0
");
112         }
113         else{
114             for(int i = 0; i < n; i++){
115                 printf("%d", tmpb[i]);
116             }
117             printf("*10^%d
", digb);
118         }    
119     }
120     else{
121         bool flag = true;
122         for(int i = 0; i < n; i++){
123             if(tmpa[i] != tmpb[i]){
124                 flag = false;
125                 break;
126             }
127         }
128         if(firsta == -1 && firstb == -1)flag = true;
129         if(flag){
130             printf("YES 0.");
131             if(firsta == -1){
132                 for(int i = 0; i < n; i++){
133                     printf("0");
134                 }
135                 printf("*10^0
");
136             }
137             else{
138                 for(int i = 0; i < n; i++){
139                     printf("%d", tmpa[i]);
140                 }
141                 printf("*10^%d
", diga); 
142             }
143         }
144         else{
145             printf("NO 0.");
146             if(firsta == -1){
147                 for(int i = 0; i < n; i++){
148                     printf("0");
149                 }
150                 printf("*10^0 0.");
151             }
152             else{
153                 for(int i = 0; i < n; i++){
154                     printf("%d", tmpa[i]);
155                 }
156                 printf("*10^%d 0.", diga);
157             }
158             if(firstb == -1){
159                 for(int i = 0; i < n; i++){
160                     printf("0");
161                 }
162                 printf("*10^0
");
163             }
164             else{
165                 for(int i = 0; i < n; i++){
166                     printf("%d", tmpb[i]);
167                 }
168                 printf("*10^%d
", digb);
169             }    
170         }
171     }
172     
173     return 0;
174 }
原文地址:https://www.cnblogs.com/wyboooo/p/10448366.html