题目原文
Consider the set of all reduced fractions between 0 and 1 inclusive with denominators less than or equal to N.
Here is the set when N = 5:
0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
Write a program that, given an integer N between 1 and 160 inclusive, prints the fractions in order of increasing magnitude.
PROGRAM NAME: frac1
INPUT FORMAT
One line with a single integer N.SAMPLE INPUT (file frac1.in)
5
OUTPUT FORMAT
One fraction per line, sorted in order of magnitude.SAMPLE OUTPUT (file frac1.out)
0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1
分析
题意很简单,给定一个N,要求求出所有的0~1之间(包括0和1)不可约的分数,并且分子分母都小于N,结果按从小到大的顺序输出。
最直观的方法是,通过一个双重循环,便利所有的可能性,然后把能约分的约掉就可以了,最后把所有记录的结果排序。大概是下面这样的意思:
vector record
for i=0~N
for j=i~N
record.push(cal(i/j))
sort(record)
按照这样的方法做是可以通过的,但是效率没那么高,尤其是循环中需要求最大公约数进行约分。
另一种高效一点的方法是这样的,假设N=5,那么要求得的数可以按下面的方式排列:
0/1 1/1 1/2 1/3 2/3 1/4 2/5 3/5 3/4 1/5 2/7 3/8 3/7 4/7 5/8 5/7 4/5通过下面的函数,就可以轻易的生成所有0/1和1/1除外符合要求的数
void genfrac(vector<fraction> &dst,const int &N,int n1,int d1, int n2,int d2)
{
if(d1+d2>N)
return;
genfrac(dst,N,n1,d1,n1+n2,d1+d2);
dst.push_back(fraction(n1+n2,d1+d2));
genfrac(dst,N,n1+n2,d1+d2,n2,d2);
}
提交代码如下
/*
ID: Aaron Cai
PROG: frac1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
string int2str(int a)
{
string out="";
if(a==0)
{
return "0";
}
while(a!=0)
{
out += char('0' + a%10);
a = (a - a%10)/10;
}
reverse(out.begin(),out.end());
return out;
}
int gcd(int a,int b)
{
if(a>b)
swap(a,b);
if(a==0)
return 1;
while(b % a != 0)
{
int m = b % a;
b = a;
a = m;
}
return a;
}
struct fraction
{
int up;
int down;
fraction(int _up,int _down):up(_up/ gcd(_up,_down)),down(_down/gcd(_up,_down))
{}
string toString()
{
return int2str(up) + "/" + int2str(down);
}
};
bool fractionCompare(fraction &f1,fraction &f2)
{
return f1.up * f2.down - f1.down*f2.up <= 0;
}
void genfrac(vector<fraction> &dst,const int &N,int n1,int d1, int n2,int d2)
{
if(d1+d2>N)
return;
genfrac(dst,N,n1,d1,n1+n2,d1+d2);
dst.push_back(fraction(n1+n2,d1+d2));
genfrac(dst,N,n1+n2,d1+d2,n2,d2);
}
int main()
{
ifstream fin("frac1.in");
ofstream fout("frac1.out");
int N;fin>>N;
vector<fraction> frac;
genfrac(frac,N,0,1,1,1);
fout << "0/1"<<endl;
for (int i=0;i!=frac.size();i++)
{
fout << frac[i].toString() << endl;
}
fout << "1/1" << endl;
return 0;
}
提交结果
TASK: frac1 LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.005 secs, 3500 KB] Test 2: TEST OK [0.005 secs, 3500 KB] Test 3: TEST OK [0.003 secs, 3500 KB] Test 4: TEST OK [0.005 secs, 3500 KB] Test 5: TEST OK [0.008 secs, 3500 KB] Test 6: TEST OK [0.008 secs, 3500 KB] Test 7: TEST OK [0.016 secs, 3500 KB] Test 8: TEST OK [0.038 secs, 3500 KB] Test 9: TEST OK [0.076 secs, 3500 KB] Test 10: TEST OK [0.121 secs, 3500 KB] Test 11: TEST OK [0.289 secs, 3500 KB] All tests OK.
官方参考答案
官方给出了直接法和优化后的方法两种答案,这里都贴一下
直接法
#include <fstream.h>
#include <stdlib.h>
struct fraction {
int numerator;
int denominator;
};
bool rprime(int a, int b){
int r = a % b;
while(r != 0){
a = b;
b = r;
r = a % b;
}
return(b == 1);
}
int fraccompare (struct fraction *p, struct fraction *q) {
return p->numerator * q->denominator - p->denominator *q->numerator;
}
int main(){
int found = 0;
struct fraction fract[25600];
ifstream filein("frac1.in");
int n;
filein >> n;
filein.close();
for(int bot = 1; bot <= n; ++bot){
for(int top = 0; top <= bot; ++top){
if(rprime(top,bot)){
fract[found].numerator = top;
fract[found++].denominator = bot;
}
}
}
qsort(fract, found, sizeof (struct fraction), fraccompare);
ofstream fileout("frac1.out");
for(int i = 0; i < found; ++i)
fileout << fract[i].numerator << '/' << fract[i].denominator << endl;
fileout.close();
exit (0);
}
优化的方法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int n;
FILE *fout;
/* print the fractions of denominator <= n between n1/d1 and n2/d2 */
void
genfrac(int n1, int d1, int n2, int d2)
{
if(d1+d2 > n) /* cut off recursion */
return;
genfrac(n1,d1, n1+n2,d1+d2);
fprintf(fout, "%d/%d\n", n1+n2, d1+d2);
genfrac(n1+n2,d1+d2, n2,d2);
}
void
main(void)
{
FILE *fin;
fin = fopen("frac1.in", "r");
fout = fopen("frac1.out", "w");
assert(fin != NULL && fout != NULL);
fscanf(fin, "%d", &n);
fprintf(fout, "0/1\n");
genfrac(0,1, 1,1);
fprintf(fout, "1/1\n");
}