zoj-1003

#include <iostream>
using namespace std;
void Dfs(int to,long int numa,long int numb);
int flagA,flagB;
int main()
{
long int numA,numB;
while(cin >> numA >> numB)
{
if(numA < numB)
swap(numA,numB);
flagA=0;
flagB=0;
Dfs(100,numA,numB);
if(flagA)
cout << numA << endl;
else
{
if(flagB)
cout << numB << endl;
else
cout << numA << endl;
}
}
return 0;


}


void Dfs(int to,long int numa,long int numb)
{
if(numb==1)
{
flagB=1;
if(numa==1)
flagA=1;
}
if(to==1 || flagA==1 && flagB==1)
return ;
if(numa%to==0)Dfs(to-1,numa/to,numb);
if(numb%to==0)Dfs(to-1,numa,numb/to);
Dfs(to-1,numa,numb);
}

你可能感兴趣的:(zoj-1003)