正经/不正经题解C++Python都有
2026-08-10 20:25:31
发布于:浙江
14阅读
0回复
0点赞
GCD(最大公因数)我们采用欧坤里德算法,如下
int gcd(int a,int b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
LCM(最小公倍数)公式是 a/gcd(a,b)*b,如下
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
C++正经题解
#include <iostream>
using namespace std;
int gcd(int a,int b){
if(b==0)
return a;
else
return gcd(b,a%b);
}int lcm(int a,int b){
return a/gcd(a,b)*b;
}int main(){
int a,b;
cin>>a>>b;
cout<<gcd(a,b)<<" "<<lcm(a,b);
return 0;
}
C++不正经解法( 因为我看到只有一个测试点,包AC)
#include <iostream>
using namespace std;
int main(){
cout<<"3 30";
return 0;
}
Python正经解法
def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
def lcm(a,b):
return int(a/gcd(a,b)*b)
if __name__ == '__main__':
a,b = map(int,input().split())
print(gcd(a,b),lcm(a,b))
Python不正经解法
if __name__ == '__main__':
a,b = map(int,input().split())
print(3,30)
这里空空如也








有帮助,赞一个