Fork me on GitHub

PAT(甲级)渡劫(二十)-Are They Equal(25)


PAT(甲级)渡劫(二十)-Are They Equal(25)

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int maxn = 200;
char s1[maxn],s2[maxn];
int n;

void solve(char *str,char *ans,int n,int &e){
int len = strlen(str);
int point = len; // 小数点的索引
for(int i = 0 ; i < len ; i++){
if(str[i] == '.'){
point = i;
break;
}
}
int not0 = len; // 第一个非0的索引
for(int i = 0 ; i < len ; i++){
if(str[i] != '.' && str[i] != '0'){
not0 = i;
break;
}
}
// 数字为0
if(not0 == len){
e = 0;
ans[0] = '0';
ans[1] = '.';
for(int i = 0 ; i < n ; i++){
ans[2+i] = '0';
}
ans[n+2] = '\0';
}else{
e = point>not0?point-not0:point-not0+1; // 指数
ans[0] = '0';
ans[1] = '.';
for(int i = 0,j = not0 ; i < n ; i++,j++){
if(j < len){
if(str[j] != '.'){
ans[2+i] = str[j];
}else
i--;
}else
ans[2+i] = '0';
}
ans[2+n] = '\0';
}
}
int main(){
freopen("in.txt","r",stdin);
scanf("%d %s %s",&n,s1,s2);
char ans1[maxn],ans2[maxn];
int e1,e2;
solve(s1,ans1,n,e1);
solve(s2,ans2,n,e2);
if(strcmp(ans1,ans2) == 0 && e1 == e2){
printf("YES ");
printf("%s*10^%d\n",ans1,e1);
}else{
printf("NO ");
printf("%s*10^%d %s*10^%d\n",ans1,e1,ans2,e2);
}

return 0;
}

运行结果:

坚持原创技术分享,您的支持将鼓励我继续创作
-------------本文结束感谢您的阅读-------------
0%