跳到正文

个人记录

排序

yimeiyingbi约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年8月5日
更新
2026年8月8日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
struct fre{
	int a;
	int b;
	bool operator==(const fre& other) const {
		return a == other.a && b == other.b;
	}
}; 
int gcd(int n,int m){
    if(m>n)swap(m,n);
    int t=n%m;
    while(t!=0){
        n=m;
        m=t;
        t=n%m;
    }
    return m;
}
bool cmp(fre a,fre b){
	return a.a*b.b<a.b*b.a;
}
int main(){
    int n;
    vector<fre>v;
    cin>>n;
    for(int i=1;i<=n;i++){
    	for(int j=1;j<i;j++){
    		int g=gcd(i,j);
    		fre m;
    		m.a=j/g;
    		m.b=i/g;
    		v.push_back(m);
		}
	}
	sort(v.begin(),v.end(),cmp);
	auto it=unique(v.begin(),v.end());
	v.erase(it,v.end());
	
	for(auto vec:v){
		cout<<vec.a<<" "<<vec.b<<'\n';
	}
    return 0;
}
原文