给定两个字符串 s 和 t ,每个字符串代表一个非负有理数,只有当它们表示相同的数字时才返回 true 。字符串中可以使用括号来表示有理数的重复部分。
有理数 最多可以用三个部分来表示:整数部分 、小数非重复部分 和小数重复部分 <(><)>。数字可以用以下三种方法之一来表示:
例: 0 ,12 和 123
<.>
例: 0.5 , 1. , 2.12 和 123.0001
<.><(><)>
例: 0.1(6) , 1.(9), 123.00(1212)
十进制展开的重复部分通常在一对圆括号内表示。例如:
1 / 6 = 0.16666666... = 0.1(6) = 0.1666(6) = 0.166(66)
示例 1:
输入:s = “0.(52)”, t = “0.5(25)”
输出:true
解释:因为 “0.(52)” 代表 0.52525252…,而 “0.5(25)” 代表 0.52525252525…,则这两个字符串表示相同的数字。
示例 2:
输入:s = “0.1666(6)”, t = “0.166(66)”
输出:true
示例 3:
输入:s = “0.9(9)”, t = “1.”
输出:true
解释:“0.9(9)” 代表 0.999999999… 永远重复,等于 1 。[有关说明,请参阅此链接]
“1.” 表示数字 1,其格式正确:(IntegerPart) = “1” 且 (NonRepeatingPart) = “” 。
当然下述代码,基本解决了这个问题,但是对于0.999这种问题还是欠缺处理
bool isRationalEqual(char * s, char * t){
int i,j;
int poi=0,poj=0;
i=0;
j=0;
int r=0;
int num=0;
int poi2=0;
int poj2=0;
char st[10]="1.";
char st3[10]="1";
char st4[10]="1.0";
char st2[10]="0.9(9)";
char rz[10];
int tr[10];
if(strcmp(s,st)==0){
printf("l1 %s %s",s,st);
s=st2;
}
if(strcmp(t,st)==0){
printf("l2 %s %s",t,st);
t=st2;
}
if(strcmp(s,st3)==0){
printf("l1 %s %s",s,st);
s=st2;
}
if(strcmp(t,st4)==0){
printf("l2 %s %s",t,st);
t=st2;
}
if(strcmp(s,st4)==0){
printf("l1 %s %s",s,st);
s=st2;
}
if(strcmp(t,st3)==0){
printf("l2 %s %s",t,st);
t=st2;
}
while(s[i]!=')'if{
([s]i=='(')={
poi;i}
if
([s]i==')')={
poi2;i}
++
i;}
while
([t]j!=')'if({
[]t==j'(')=;{
poj}jif
(
[]t==j')')=;{
poj2}j++
;
j}int
,
; l=p+
l1poi;=;
pcharpoi2[
10 ttr];char[
10 pr]="0";int=
0 index;while(
<)lprintfp({
"zz %c ",[]s)l;[]
ttr=index[]s;l++;
index++;
l}[
]
ttr=index';'printf("zzl %s "
,);ifttr(strcmp
(,)==ttr0pr)[-1{
s]poi=[-1s]poi-1;[+1
s]poi='9';}=+
1
l;poj=;=
p0poj2;
indexwhile(<
)printfl(p"zz %c "{
,[])t;l[]=
ttr[index];t++l;++
index;}
l[]
=
ttr';'indexif(strcmp(
,)==0ttr)pr[-1]{
t=poj[-1]t-poj1;[+1]
t=poj'9';}=0;
=
i0;while
j([]
!='&&'s[i]!='&&'<t20j)if([num]=='('{
)++s;i}if([{
i]==
'('
)++t;j}if([{
j]==
')'
)=s+i1;}if{
i(poi[]==
')'
)=t+j1;}printf{
j(poj"%c %c ",[
]
,[])s;iif(t[j]!=[
])s=i1;tbreakj;}{
r++;++
;++
;
num}if
i(==
j1)
return
;}relsereturn;{
return false;
}
true
true
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)