1个回答
展开全部
public
class
CommonDivisor
{
public
static
void
main(String[]
args)
{
int
n1
=
21;
int
n2
=
14;
System.out.println("最大公约数是"
+
fun(n1,
n2));
}
public
static
int
fun(int
n1,
int
n2)
{
int
m
=
1;
if
(n1
<
n2)
{
int
t
=
n1;
n1
=
n2;
n1
=
t;
}
for
(int
i
=
n2;
i
>
1;
i--)
{
if
((n1
%
i
==
0)
&&
(n2
%
i
==
0))
{
return
i;
}
}
return
m;
}
}
//
the
end
class
CommonDivisor
{
public
static
void
main(String[]
args)
{
int
n1
=
21;
int
n2
=
14;
System.out.println("最大公约数是"
+
fun(n1,
n2));
}
public
static
int
fun(int
n1,
int
n2)
{
int
m
=
1;
if
(n1
<
n2)
{
int
t
=
n1;
n1
=
n2;
n1
=
t;
}
for
(int
i
=
n2;
i
>
1;
i--)
{
if
((n1
%
i
==
0)
&&
(n2
%
i
==
0))
{
return
i;
}
}
return
m;
}
}
//
the
end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询