Loading

USACO 2.1.2 Ordered Fractions

Freddy posted @ 2011年9月14日 18:25 with tags c++ usaco 算法 鏈表 , 1986 阅读

USACO - Freddy - Freddy の ブログ

 

Ordered Fractions

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.

 

输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1,请找出所有满足条件的分数。

这有一个例子,当N=5时,所有解为:

0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1

给定一个自然数N,1<=n<=160,请编程按分数值递增的顺序输出所有解。

注:①0和任意自然数的最大公约数就是那个自然数②互质指最大公约数等于1的两个自然数。

 

 

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 

 

本題相對而言還是USACO中的一道比較簡單的題目,但竟然放在了第二章...

本題唯一不好弄的地方就是判斷和排序,我的判斷函數列了很多種情況。

 

然後就是排序,無論我怎麼調試,用qosrt對結構體或類中的double型變量排序的結果很悲劇,不知道它是按什麽順序排的,於是我就放棄了快排函數,改用自己寫的靜態鏈表排序。

我寫的靜態鏈表排序:http://zhuyifan2007.5gbfree.com/cpp/lb.html

提交給USACO官網后竟然一次AC!

 

我的測評結果:
USER: YeeFan Zhu [zyfwork1]
TASK: frac1
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4272 KB]
   Test 2: TEST OK [0.000 secs, 4272 KB]
   Test 3: TEST OK [0.000 secs, 4272 KB]
   Test 4: TEST OK [0.000 secs, 4272 KB]
   Test 5: TEST OK [0.000 secs, 4272 KB]
   Test 6: TEST OK [0.000 secs, 4272 KB]
   Test 7: TEST OK [0.000 secs, 4272 KB]
   Test 8: TEST OK [0.027 secs, 4272 KB]
   Test 9: TEST OK [0.081 secs, 4272 KB]
   Test 10: TEST OK [0.135 secs, 4272 KB]
   Test 11: TEST OK [0.459 secs, 4272 KB]

All tests OK.

YOUR PROGRAM ('frac1') WORKED FIRST TIME! That's fantastic -- and a rare thing.

Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
1
------- test 2 ----
2
------- test 3 ----
4
------- test 4 ----
7
------- test 5 ----
10
------- test 6 ----
15
------- test 7 ----
24
------- test 8 ----
50
------- test 9 ----
75
------- test 10 ----
100
------- test 11 ----
160

Keep up the good work!
Thanks for your submission!

 

 

程序的效率還是不錯的。

 

貼上代碼:

/*
PROB: frac1
ID: zyfwork1
LANG: C++
*/

#include <fstream>
using namespace std;
ifstream fin("frac1.in");
ofstream fout("frac1.out");

int top=0;

struct Fractions
{
	double key;
	double fenzi;
	double fenmu;	
	int sen;//後繼
        int next;//前驅
}N[40000];

void Insert(double key,double fenzi,double fenmu)
{
    int point=0;
    while (key>N[point].key)
    {
        point=N[point].next;
    }
    //Create a new node
    N[top].key=key;
	N[top].fenzi=fenzi;
	N[top].fenmu=fenmu;
	
    N[top].next=point;
    N[top].sen=N[point].sen;
    
    N[point].sen=top;//後繼的前驅等于自己
    N[N[top].sen].next=top;//前驅的後繼等于自己
    
    top++;
}

bool isHZ(int ma,int mi)
{
	if (ma==1 && mi==0) return true;
	if (ma==1 && mi==1) return true;
	if(mi>ma) return false;
	if (ma==1) return false;
	if (mi==0) return false;
	for (int i=2;i<=mi;i++)
	{
		if (ma%i==0 && mi%i==0) return false;
	}
	return true;
}

void print()
{
    int point=N[0].next; 
    while(N[point].next!=-1)
    {
	fout<<N[point].fenzi<<"/"<<N[point].fenmu<<endl;
        point=N[point].next;
    }
}

int main()
{
    N[0].key=-1;N[0].next=1;N[0].sen=-1;
    N[1].key=65525;N[1].next=-1;N[0].sen=0;
    top=2;
	int num;
	fin>>num;
	
	for (int i=1;i<=num;i++)
	{
		for (int j=0;j<=i;j++)
		{
			if (isHZ(i,j))
			{
				double di=i;
				double dj=j;
				Insert(dj/di,dj,di);
			}
		}
	}
	print();	
	return 0;
}
小丑い雨 说:
2011年9月14日 19:23

哈哈,没我的快~~~~~

Birtwistle 说:
2018年10月22日 01:10

Learners of mathematic formulas can keep their knowledge updated with take tips and tricks from here. If you are searching a best custom dissertation writing service help then rating lists can help you in well way.

Lauren Foster 说:
2018年12月13日 08:48

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 Regards: Dissertation Writers In London

William david 说:
2019年3月21日 23:46

Essays Tigers has been operating in the <a href="https://www.essaystigers.co.uk/write-my-essay.php">UK essay writing service</a> industry for over ten years now. Our extensive experience in the field has made us true pioneers and trailblazers because of our writers’ constant efforts to set the bar even higher. Our skilful expertise has encouraged thousands of students to approach us for assistance in all their essay writing endeavors. If you want the same, we fully encourage you to come forth and register for our writing services today!

happywheelsgame 说:
2019年4月26日 14:36

happy wheels basketball legends game Flowers still fall on the porch, fibrous leaves go much and. I wish you peace->

Best Dissertation He 说:
2019年10月13日 08:05

I really appreciate the efforts of the young rapper’s efforts and he is the example for the youngsters who are on the same as he is and he is helping people to learn about the banking and all its procedure and it is true that a large number of people don’t know how to manage money and all the things I am also providing online assignment help and it is a great feeling to give someone help that will give him benefits.

Phd Dissertation Hel 说:
2019年11月05日 18:10

These levels are unit scores: each level has the same number. You will see that as the denominator gets bigger the points get smaller. Arrange levels with similar molecules

Ratione culpa volup 说:
2020年2月16日 00:51

[essaybox reviews](https://topbritishwriters.com/essaybox-review/)

Ratione culpa volup 说:
2020年2月16日 00:51

"essaybox reviews":https://topbritishwriters.com/essaybox-review/
[bestessays|https://www.topratedessayservice.com/bestessays-com-review/]
[[https://allessayvikings.com/123helpme-review/|123helpme com reviews ]]
<a href="https://www.toptenwritingservices.com/essays-studymoose-com-review/" style="background-color: rgb(3, 255, 11); border: 1px dashed black;">studymoose</a>
[https://www.rushmyessays.org/](https://www.rushmyessays.org/)
**link=https://www.topaperwritingservices.com/**essay help**

housekeeping service 说:
2021年10月01日 22:07

What quantity of money you will be able to make is very your choice on how big is you intend to grow your online business and the amount of time you are likely to put with, just like another business. If you like to make a bit extra quietly just carry out some clients intended for pocket dollars or if you would like make lots then you really have to work the item and put time in, to start with.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter