天天看點

洛谷T300217獨幕喜劇的AMCPlus第一章題解1.最小公倍數2最大公因數3整合總體程式

題目要求就是ab的最小公倍數和cd的最大公因數

一看題目,這裡就2個要點,3個思路

第一,最小公倍數

第二,最大公因數

第三,算出結果

1.最小公倍數

我們可以用Python解決他:

def LLO(a,b):
    i=a
    while 1:
        if i%a==0 and i%b==0:
            return i
           

但是這樣會RE無傳回,是以我們加好對1的判斷

def LLO(a,b):
    if a==1 or b==1:
        return 1
    i=a
    while 1:
        if i%a==0 and i%b==0:
            return i
           

我們可以用一個函數求出它(c++)

long LLO(long a,long b){
	long i=a;
	while (1){
		if(i%a==0 and i%b==0)return i;
		i++;
	}
}
           

2最大公因數

def GCF(a,b):
    for i in range(1,a):
        if(a%i==0 and b%i==0):
            c=i
    return c
           

一樣的,一個函數就能解決掉(Python)

但是這樣會RE無傳回,是以我們加好對1的特判

def GCF(a,b):
    if a==1 or b==1:
        return 1
    for i in range(1,a):
        if(a%i==0 and b%i==0):
            c=i
    return c
           

(c++函數)

long GCF(long a,long b){
	for(long long i=a;i>0;i--)if(a%i==0 and b%i==0)return i;
}
           

3整合總體程式

再講一下,我們還有一個算式,都一樣:LLO(a,b)*GCF(c,d)

上代碼(Python)

def LLO(a,b):
    if a==1 or b==1:
        return 1
    i=a
    while 1:
        if i%a==0 and i%b==0:
            return i
def GCF(a,b):
    if a==1 or b==1:
        return 1
    for i in range(1,a):
        if(a%i==0 and b%i==0):
            c=i
    return c
l=input().split(' ')#輸入去空格
a=int(l[0])
b=int(l[1])
c=int(l[2])
d=int(l[3])
print(LLO(a,b)*GCF(c,d))
           

上c++代碼

#include<iostream>
using namespace std;
long LLO(long a,long b){
	long i=a;
	while (1){
		if(i%a==0 and i%b==0)return i;
		i++;
	}
}
long GCF(long a,long b){
	for(long long i=a;i>0;i--)if(a%i==0 and b%i==0)return i;
}
int main(){
	long a,b,c,d;
	cin>>a>>b>>c>>d;
	cout<<LLO(a,b)*GCF(c,d);
	return 0;
}
           
洛谷T300217獨幕喜劇的AMCPlus第一章題解1.最小公倍數2最大公因數3整合總體程式

 AC Yeah通過