题目本意是求一系列数的最小公倍数,我在博客中的一篇文章中已经写过一篇总结的文章,这里就不再赘述。
本题需要注意的就是,在题目输入的要求中有一句话:All integers will be positive and lie within the range of a 32-bit integer.
所以在求最小公倍数的函数中,要做些小手脚。经测试,如果按照以下方式写:
int lcm(int a,int b)
{
    return a*b/gcd_recursive(a,b);
}
得到的是Wrong Answer.原因是a*b可能会超出32位的int所能表示的范围。所以要修改成如下代码:
int lcm(int a,int b)
{
    return a*(b/gcd_recursive(a,b));
}
多加一个括号后就能避免这个问题了.
AC代码如下:

[HODJ]1019. Least Common Multiple//算法描述
[HODJ]1019. Least Common Multiple
//前提:a > b > 0
[HODJ]1019. Least Common Multiple
//返回:a和b的最大公约数
[HODJ]1019. Least Common Multiple
#include <iostream>
[HODJ]1019. Least Common Multiple
using namespace std;
[HODJ]1019. Least Common Multiple
int gcd_recursive(int a,int b)

 

相关文章:

  • 2022-03-09
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2018-03-21
猜你喜欢
  • 2022-01-03
  • 2021-07-19
  • 2021-10-01
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案