#include <iostream> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long gcd(long long x,long long y){ if(x>=y){ if(x%y!=0) return gcd(y,x%y); else return y;} else { if(y%x!=0) return gcd(x,y%x); else return x;} } long long lcm(long long x,long long y){ return (x*y)/gcd(x,y); } int main(int argc, char** argv) { long long n; while(cin>>n){ if(n==0) break; long long a[n];cin>>a[0]>>a[1];long long w=lcm(a[0],a[1]); for(int i=2;i<n;i++){ cin>>a[i];w=lcm(w,a[i]); } cout<<w<<"\n"; } return 0; }
#include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long gcd(long long x,long long y){ if(x>=y){ if(x%y!=0) return gcd(y,x%y); else return y;} else { if(y%x!=0) return gcd(x,y%x); else return x;} } long long lcm(long long x,long long y){ return (x*y)/gcd(x,y); } int main(int argc, char** argv) { long long n; while(cin>>n){ if(n==0) break; long long a[n];cin>>a[0]>>a[1];long long w=lcm(a[0],a[1]); for(int i=2;i<n;i++){ cin>>a[i];w=lcm(w,a[i]); } cout<<w<<"\n"; } return 0; }
厲害 用遞迴寫輾轉相除法
!! 我沒想到用遞迴寫
#include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long gcd(long long x,long long y){ if(x>=y){ if(x%y!=0) return gcd(y,x%y); else return y;} else { if(y%x!=0) return gcd(x,y%x); else return x;} } long long lcm(long long x,long long y){ return (x*y)/gcd(x,y); } int main(int argc, char** argv) { long long n; while(cin>>n){ if(n==0) break; long long a[n];cin>>a[0]>>a[1];long long w=lcm(a[0],a[1]); for(int i=2;i<n;i++){ cin>>a[i];w=lcm(w,a[i]); } cout<<w<<"\n"; } return 0; }
厲害 用遞迴寫輾轉相除法
!! 我沒想到用遞迴寫
<algorithm>裡面有__gcd()
#include using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long gcd(long long x,long long y){ if(x>=y){ if(x%y!=0) return gcd(y,x%y); else return y;} else { if(y%x!=0) return gcd(x,y%x); else return x;} } long long lcm(long long x,long long y){ return (x*y)/gcd(x,y); } int main(int argc, char** argv) { long long n; while(cin>>n){ if(n==0) break; long long a[n];cin>>a[0]>>a[1];long long w=lcm(a[0],a[1]); for(int i=2;i<n;i++){ cin>>a[i];w=lcm(w,a[i]); } cout<<w<<"\n"; } return 0; }
厲害 用遞迴寫輾轉相除法
!! 我沒想到用遞迴寫
裡面有__gcd()
那東西很慢好嗎?
遞迴比較快