1415 - C语言-公约公倍
输入两个正整数m和n,求其最大公约数和最小公倍数。
Input
两个整数
Output
最大公约数,最小公倍数
Examples
Input
5 7
Output
1 35
Solution C
#include<stdio.h> int main() { int a,b,max,min,c,d,temp,t; scanf("%d%d",&a,&b); c=a,d=b; for(;;) { if(a<b) temp=a,a=b,b=temp; if(a%b==0) { max=b; break; } else t=a%b,a=b,b=t; } min=(c*d)/max; printf("%d %d\n",max,min); return 0; }
Solution C++
#include<iostream> #include<fstream> #include<string> #include<cstring> #include<cmath> using namespace std; int gcd(int x,int y) { int temp; if(x<y) {temp=x;x=y;y=temp;} if(y==0) return x; gcd(y,x%y); } int go(int n,int m) { return (n/gcd(m,n)*m); } int main() { //ifstream cin("aaa.txt"); int i,j,n,m; cin>>n>>m; cout<<gcd(n,m)<<" "<<go(n,m)<<endl; return 0; }