吉大考研真题-所有真分数排序
作者:互联网
题目:输入一个整数,然后输出分母小于等于他的所有真分数并排序就是例如输入5的话 (1/5,2/5,3/5,4/5,1/4,2/4,3/4....)
并且排序,除重后输出.
#include <iostream>
using namespace std;
int gcd(int m, int n) //找公因数
{
if (m == n)
return m;
m > n ? gcd(m - n, n) : gcd(m, n - m);
}
void change(double a[], int i)
{
double tmp;
tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
}
int main()
{
double a[100] = {0}, b[100] = {0}, c[100] = {0};
int n = 0;
int i = 0;
cin >> n;
int k = (n - 1) * (n - 2) / 2;
for (int y = 2; y <= n; y++) //所有真分数
for (int x = 1; x < y; x++, i++)
{
a[i] = x;
b[i] = y;
c[i] = a[i] / b[i];
}
for (i = 0; i < k; i++) //冒泡排序
for (int j = 0; j < k - i; j++)
{
if (c[j] > c[j + 1]) //C数组排序的时候 A B同时换位置
{
change(a, j);
change(b, j);
change(c, j);
}
}
for (i = 0; i < k; i++) //输出 a/公因子 / b/公因子
{
if (c[i] == c[i + 1])
continue;
cout << a[i] / gcd(a[i], b[i]) << "/" << b[i] / gcd(a[i], b[i]) << endl;
}
return 0;
}
标签:tmp,真分数,gcd,真题,int,吉大,100,排序,change 来源: https://blog.csdn.net/qq_41854291/article/details/101367850