0%

PAT A1060 Are They Equal

我的解法

解题思路:此题应该分两步走,分别确定指数和转为有效数位标准格式,确定指数应该根据小数点的位置来确定。小数点前面有几个整数,指数就为几;小数点后面要看第一个整数前面有几个0,有几个0就为负几。确定有效数位,从第一个整数开始,数n个数,如果缺数,用0补齐。

需要注意的c++语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//-->1、e不能直接作为if判断条件,正负为true,0位false
int f;
if(e>0) f=p-e;
else f=p-e+1;
cout<<"f:"<<f<<endl;
//-->2、string的append添加字符的时候有两个参数,//string也可以直接+=字符!
res.append(1,'0');
res+='0';
//-->3、数字转字符串在devc++中不能用to_string,可以用下面的方法
//整数转string
// char chs[100];
// sprintf(chs,"%d",e);
// string st(chs,chs+strlen(chs));
stringstream ss;
ss<<e;
string st=ss.str();
res+=st;
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
#include <cstring>
#include <sstream>

using namespace std;

//确定指数
int findP(string s){
int l=s.length();
int res=0;
//如果小数点存在,根据小数点确定指数
if(s.find('.')!=string::npos){

int p=s.find('.');
int i=0;
bool beforeHas=false;//判断小数点前面有无整数
bool afterHas=true;//判断小数点后面有无整数
while(i<p){//找出小数点前面的第一个整数位置
if(s[i]!='0'){
beforeHas=true;
break;
}
else i++;
}
// cout<<p<<endl<<i<<endl;
res=p-i;
i=p+1;
if(!beforeHas){
while(i<l){//找出小数点后面第一个整数前面0的位数
if(s[i]=='0'){
res--;
afterHas=false;
}
else{
afterHas=true;
break;
}
i++;
}
}
if(!afterHas){
res=0;
}
}else{
//如果没有小数点,就找第一个整数位置
int j=0;

while(j<l){
if(s[j]!='0') break;
else j++;
}
res=l-j;
}
return res;
}
//转为标准格式
string changeTo(string s,int n,int e){
string res="0.";
int len=s.length();
//判断有无小数点
if(s.find('.')!=string::npos){
int p=s.find('.');
//找到第一个有效位数
int f;
if(e>0) f=p-e;//小数点前后开始位置不同
else f=p-e+1;
// cout<<"f:"<<f<<endl;

for(int i=0;i<n;){
if(f<len){
if(isdigit(s[f])){
res.append(1,s[f]);
f++;
i++;
}else{
f++;
}
}else{
res.append(1,'0');
i++;
}
}
}else{
int j=0,t=0;
while(j<len&&t<n){
if(s[j]!='0'){
res.append(1,s[j]);
j++;
t++;
break;
}
j++;
}
while(t<n){
if(j<len){
res.append(1,s[j]);
j++;
}else{
res.append(1,'0');
}
t++;
}

}
res+="*10^";
//整数转string
// char chs[100];
// sprintf(chs,"%d",e);
// string st(chs,chs+strlen(chs));
stringstream ss;
ss<<e;
string st=ss.str();
res+=st;
return res;
}

int main(){
int n;
string s1,s2;
cin>>n>>s1>>s2;

int e1=findP(s1);
int e2=findP(s2);

string ss1=changeTo(s1,n,e1);
string ss2=changeTo(s2,n,e2);

if(ss1==ss2){
cout<<"YES "<<ss1;
}else{
cout<<"NO "<<ss1<<" "<<ss2;
}
return 0;
}

算法笔记解法

解题思路:巧妙利用string的erase方法,将前导零和小数点去掉。也是寻找指数和有效位,先根据小数点寻找指数,最后确定有效位数,在寻找指数的过程中将小数点和零从字符串中清除。

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
#include <iostream> 
#include <string>
using namespace std;
int n;


string deal(string s,int& e){
int k=0;
while(s.length()>0&&s[0]=='0') s.erase(s.begin());//删除前导0
if(s[0]=='.'){//如果下一个是小数点,该数小于1,e为负的
s.erase(s.begin());//删除小数点
while(s.length()>0&&s[0]=='0'){//删除小数点后面的0,有几个零e为负几
e--;
s.erase(s.begin());
}
}else{//如果下一个是整数,则找到小数点,判断小数点前面有几个整数,确定e
while(k<s.length()&&s[k]!='.'){
k++;
e++;
}
if(k<s.length()) s.erase(s.begin()+k);//删除小数点

}
if(s.length()==0) e=0;//全部为零

//找出有效位,不够的补0
int num=0;
k=0;
string res="";
while(num<n){
if(k<s.length()) res+=s[k++];
else res+='0';
num++;
}
return res;
}


int main(){
string s1,s2,s3,s4;
int e1=0,e2=0;
cin>>n>>s1>>s2;
s3=deal(s1,e1);
s4=deal(s2,e2);

if(e1==e2&&s3==s4){
cout<<"YES 0."<<s3<<"*10^"<<e1;
}else{
cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2;
}

return 0;
}

本文标题:PAT A1060 Are They Equal

文章作者:GavinYGM

发布时间:2020年08月14日 - 16:08

最后更新:2020年08月14日 - 17:08

原始链接:http://www.gavinygm.cn/2020/08/14/PAT-A1060-Are-They-Equal/

许可协议: 转载请保留原文链接及作者。