考递归思想,注意递归函数中的返回条件!!!
import
java.util.Scanner;
class
Test{
public
static
void
main(String[]
agrs
) {
int
x
,
y
;
Scanner
scanner
=
new
Scanner(System.
in
);
x
=
scanner
.nextInt();
y
=
scanner
.nextInt();
int
less
=Math.max(
x
,
y
);
int
big
=Math.min(
x
,
y
);
System.
out
.println(getnum(
less
,
big
));
}
static
int
getnum(
int
less
,
int
big
){
if
(
less
==0){
return
0;
}
if
(
less
==
big
){
return
4*
less
;
}
int
count
=
big
/
less
;
int
less_1
=
big
–
count
*
less
;
int
big_1
=
less
;
return
count
*4*
less
+getnum(
less_1
,
big_1
);
}
}
php实现如下:
<
?php
function
getZ
(
$
l
,
$
d
){
if
(
$
d
==
0
){
return
0
;
}
if
(
$
l
==$
d
){
return
4
*$
d
;
}
$
count
=
floor
(
$
l
/$
d
)
;
$
l2
=$
d
;
$
d2
=$
l
-$
count
*$
d
;
return
4
*$
d
*$
count
+
getZ
(
$
l2
,
$
d2
)
;
}
$
length
=
fopen
(
“php://stdin”
,
“r”
)
;
$
l
=
fgets
(
$
length
)
;
$
height
=
fopen
(
“php://stdin”
,
“r”
)
;
$
d
=
fgets
(
$
height
)
;
printf
(
“%d
\n
”
,getZ
(
$
l
,
$
d
))
;
?
>