两个正整数M和N,M <= N,令0 < a < M,M <= b <= N,分别输出以a为分子b为分母的可约和不可约分数
#include <iostream>
#include <vector>
using namespace std;
int gcd(int m, int n)
{
if (n == 0)
{
return m;
}
else
{
return gcd(n, m % n);
}
}
int main()
{
int m, n;
vector<string> canReduce, cannotReduce;
while (cin >> m >> n)
{
if (m >= n)
{
break;
}
for (int i = 1; i < m; i++)
{
for (int j = m; j <= n; j++)
{
char str[10];
sprintf(str, "%d/%d", i, j);
if (gcd(i, j) == 1)
{
cannotReduce.push_back(str);
}
else
{
canReduce.push_back(str);
}
}
}
for (int i = 0; i < canReduce.size(); i++)
{
cout << canReduce.at(i).c_str() << " ";
}
cout << endl;
for (int i = 0; i < cannotReduce.size(); i++)
{
cout << cannotReduce.at(i).c_str() << " ";
}
cout << endl;
}
return 0;
}