题目来源:POJ。。。
# include <stdio.h>
int gcd(int x, int y);
int main()
{
int x, y;
while (~scanf("%d%d", &x, &y))
printf("%d\n", x/gcd(x,y)*y);
return 0;
}
int gcd(int x, int y)
{
while (x != y)
if (x > y) x = x - y;
else y = y - x;
}
下面这个是不对的,原因是Runtime Error(INTEGER_DIVIDE_BY_ZERO)。
View Code
1 # include <stdio.h>
2
3 int gcd(int x, int y);
4
5 int main()
6 {
7 int x, y;
8
9 while (~scanf("%d%d", &x, &y))
10 printf("%d\n", x/gcd(x,y)*y);
11
12 return 0;
13 }
14
15 int gcd(int x, int y)
16 {
17 while (x != y)
18 if (x > y) x = x % y;
19 else y = y % x;
20 }