咋一看很不好做,其实先人早有算法。。
#include<iostream> using namespace std; int main() { int N; cin>>N; while(N--) { int a,b,GY,GB; cin>>a>>b; GB=a*b; while(a!=b) { if(a>b) a-=b; else b-=a; } GY=a; GB=GB/GY; cout<<GY<<" "<<GB<<endl; } }
原文:http://blog.csdn.net/jingweihu/article/details/24175963