历届试题—核桃的数量

    科技2025-05-30  39

    常规解法

    #include <iostream> #include <algorithm> using namespace std; int main() { int a,b,c;cin>>a>>b>>c; for(int i=1;i<100000;i++){ if(i%a==0 && i%b==0 && i%c==0){ cout<<i<<endl; return 0; } } }

    借助最大公约数求解(最大公约数乘以最小公倍数等于这两个数的乘积)

    #include <iostream> #include <algorithm> using namespace std; int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y); } int main() { int a,b,c;cin>>a>>b>>c; int x1 = (a*b)/(gcd(a,b));//ab的最小公倍数 int x2 = (x1*c)/(gcd(x1,c));//abc的最小公倍数 cout<<x2<<endl; }
    Processed: 0.009, SQL: 8