HAACM第六届程序设计大赛

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/85078618

A.Card Trick

题目

http://poj.org/problem?id=3032

描述 Description
The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
1.The top card is moved to the bottom of the pack. The new top card is dealt face up onto the table. It is the Ace of Spades.
2.Two cards are moved one at a time from the top to the bottom. The next card is dealt face up onto the table. It is the Two of Spades.
3.Three cards are moved one at a time…
4.This goes on until the nth and last card turns out to be the n of Spades.
This impressive trick works if the magician knows how to arrange the cards beforehand (and knows how to give a false shuffle). Your program has to determine the initial order of the cards for a given number of cards, 1 ≤ n ≤ 10^4.
输入格式 Input Format
On the first line of the input is a single positive integer, telling the number of test cases to follow. Each case consists of one line containing the integer n.
输出格式 Output Format
For each test case, output a line with the correct permutation of the values 1 to n, space separated. The first number showing the top card of the pack, etc…
样例输入 Sample Input
2
4
5
样例输出 Sample Output
2 1 4 3
3 1 4 5 2
时间限制 Time Limitation
3s
注释 Hint
01-02. in K<=10 N<=20
03-04. in K<=10 N<=50
05-08. in K<=100 N<=100
09-10. in K<=500 N<=500
11-14. in K<=1000 N<=1000
15-20. in N<=10000
友情提供翻译:
【问题描述】
有一位超级幻术师,他拿了一包总数为N张的卡片,他把这些卡片倒扣,进行一下操作步骤:
1.把顶部的卡片移到最底部。拿走并翻开此时最上层的卡片,它是1号卡片。
2.把顶部的卡片移到最底部,执行两遍。拿走并翻开此时最上层的卡片,它是2号卡片。
3.把顶部的卡片移到最底部,执行三遍······
······
4.这样下去直到最后一张卡片被翻开,它是N号卡片。
超级幻术师想知道如何安排这些卡片才能展示给观众这令人印象深刻的魔法。
【输入】
第一行,一个正整数K,表示有K组测试方案;
接下来的K行,每行一个正整数N,表示该测试方案的卡片总数。
【输出】
K行,每行输出卡片从1到N的恰当排列,以空格隔开。第一个数字表示顶部的卡片。
来源 Source
第六届河南省程序设计大赛
翻译&数据:郑懿鸣

代码

原题STD

这个代码为n<=13时的STD,但在本校OJ上只能拿85分(因为本校OJ数据太过强悍(n<=104),但是在POJ上AC,POJ上是原题)。

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48), ch=getchar();
	return num*f;
}
deque<int>q;
int main()
{
    int t=read();
    while(t--)
    {
        int n=read();
        q.push_back(n);
        for(int i=n-1;i>=1;--i)
        {
            q.push_back(i);
            for(int j=1;j<=i;++j)
            {
                int x=q.front();
                q.push_back(x);
                q.pop_front();
            }
        }
        while(!q.empty())
        {
            printf("%d ",q.back());
            q.pop_back();
        }
        printf("\n");
    }
    return 0;
}

可过n<=104的数据的代码

链表写法:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+10;
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48), ch=getchar();
	return num*f;
}
int prev[maxn],next[maxn],ans[maxn];
int main()
{
	int t=read();
	while (t--)
	{
		int n=read();
		for (int i=1;i<=n;++i)
			prev[i]=i-1,next[i]=i+1;
		prev[1]=n,next[n]=1;
		for (int i=1,cur=1;i<=n;++i,cur=next[cur])
		{
			for (int j=1;j<=i;++j)
				cur=next[cur];
			ans[cur]=i;
			prev[next[cur]]=prev[cur];
			next[prev[cur]]=next[cur];
		}
		for (int i=1;i<=n;++i)
			printf("%d ",ans[i]);
		printf("\n");
	}
	return 0;
}

树状数组写法

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn=11000;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)) { if(ch=='-') f=-1; ch=getchar(); }
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
	return x*f;
}
int n,tree[maxn],a[maxn];
int lowbit(int x)
{
	return x & -x;
}
void add(int x,int k)
{
    while (x<=n)
    {
    	tree[x]+=k;
		x+=lowbit(x);
	}
}
ull sum(int x)
{
	int ans=0;
    while (x)
    {
        ans+=tree[x];
        x-=lowbit(x);
    }
    return ans;
}
bool check(int x,int v)
{
	return x-sum(x)>=v;
}
int Divide(int x)//i-sum(i)==x的位置 
{
	int l=1,r=n;
	while (l+1<r)
	{
		int mid=(l+r)>>1;
		if (check(mid,x)) r=mid;
		else l=mid;
	}
	if (check(l,x)) return l;
	else return r;
}
int main()
{
	int t=read();
	while (t--)
	{
		n=read();
		memset(a,0,sizeof(a));
		memset(tree,0,sizeof(tree));
		int now=0;
		for (int i=1;i<=n;++i)
		{
			int k=i+1,l=n-now-sum(n)+sum(now);
			if (k>l)
			{
				k-=l+1;
				k%=(n-sum(n));
				now=Divide(k+1);
			}
			else
				l=now-sum(now),now=Divide(l+k);
			a[now]=i;
			add(now,1);
		}
		for (int i=1;i<=n;++i)
			printf("%d ",a[i]);
		printf("\n");
	}
	return 0;
}

B.异形卵

题目

描述 Description
我们探索宇宙,是想了解浩瀚星空的奥妙,但我们却很少意识到宇宙深处藏匿的危险,它们无时无刻不紧盯着我们的地球。如果外星人拜访我们,结果可能与哥伦布当年踏足美洲大陆不会有什么两样,这是历史,也是现实。
在ZDM-777星球上发现的休眠异形卵,其外表与常见的卵不同,表面被一层石墨覆盖。当人走近时,那层石墨开始消融,能看到里面的异形卵正在活动,异形卵是活物,具备一些热量或压力传感器这些基本的中枢神经系统,通过感知周围的热量,选择热量最大处寄生。不过,假如周围有不适合被寄生处,异形卵就选择休眠。
周围的热量可以用一串整数a[1],a[2],……,a[n]来表示,异形卵具有一定的长度L,异形卵总是选择a[i]+a[i+1]+…+a[i+L-1]达到最大值处寄生。若周围的热量低于0,异形卵则选择休眠。
异形卵是如何感知它的寄生处呢?
输入格式 Input Format
第一行: K 表示有多少组测试数据。
接下来对每组测试数据有2行
第1行: L N
第2行:a1 a2 …… aN
输出格式 Output Format
对于每组测试数据,输出一行:异形卵能寄生的起始位置。若有多处可以寄生,则选择小的起始位置。若无处可以寄生,则输出0。
样例输入 Sample Input
2
3 5
30 0 100 -30 100
3 5
-100 80 -80 -100 80
样例输出 Sample Output
3
0
时间限制 Time Limitation
1s
注释 Hint
2≤K≤5 L≤N, 1≤L≤10 1≤N≤1000 -100≤ai≤100
数据之间有一个空格。
来源 Source
第六届河南省程序设计大赛
数据:郑懿鸣

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48),ch=getchar();
	return num*f;
}
int a[maxn],sum[maxn];
int main()
{
	int k=read();
	while (k--)
	{
		int l=read(),n=read();
		for (int i=0;i<n;++i)
			a[i]=read();
		int maxx=0,id=0;
		for (int i=0;i<=n-l;++i)
		{
			sum[i]=0;
			for (int j=i;j<i+l;++j)
				sum[i]+=a[j];
			if (sum[i]>maxx)
				maxx=sum[i],id=i+1;
		}
		printf("%d\n",id);
	}
	return 0;
}

C.外星人的供给站

时间限制:1000 ms | 内存限制:65535 KB
难度:3
描述
外星人指的是地球以外的智慧生命。外星人长的是不是与地球上的人一样并不重要,但起码应该符合我们目前对生命基本形式的认识。比如,我们所知的任何生命都离不开液态水,并且都是基于化学元素碳(C)的有机分子组合成的复杂有机体。
42岁的天文学家Dr. Kong已经执著地观测ZDM-777星球十多年了,这个被称为“战神”的红色星球让他如此着迷。在过去的十多年中,他经常有一些令人激动的发现。ZDM-777星球表面有着明显的明暗变化,对这些明暗区域,Dr. Kong已经细致地研究了很多年,并且绘制出了较为详尽的地图。他坚信那些暗区是陆地,而亮区则是湖泊和海洋。他一直坚信有水的地方,一定有生命的痕迹。Dr. Kong有一种强烈的预感,觉得今天将会成为他一生中最值得纪念的日子。
这天晚上的观测条件实在是空前的好,ZDM-777星球也十分明亮,在射电望远镜中呈现出一个清晰的暗红色圆斑。还是那些熟悉的明暗区域和极冠,不过,等等,Dr. Kong似乎又扑捉到曾看到过的东西,那是什么,若隐若现的。他尽可能地睁大了眼睛,仔细地辨认。哦,没错,在一条直线上,又出现了若干个极光点连接着星球亮区,几分钟后,极光点消失。
Dr. Kong大胆猜想,ZDM-777星球上的湖泊和海洋里一定有生物。那些极光点就是ZDM-777星球上的供给站,定期给这些生物提出维持生命的供给。
不妨设,那条直线为X轴,极光点就处在X轴上,N个亮区P1,P2,…Pn就分布在若干个极光点周围。
接着,Dr. Kong 又有惊人的发现,所有的亮区Pi都处在某个半径为R的极光点圆内。去掉一个极光点就会有某些亮区Pj不处在覆盖区域内。
Dr. Kong想知道,至少需要多少个极光点才能覆盖所有的湖泊和海洋。
输入
第一行: K 表示有多少组测试数据。
接下来对每组测试数据:
第1行: N R
第2~N+1行: PXi PYi (i=1,……,N)
【约束条件】
2≤K≤5 1≤R≤50 1≤N≤100 -100≤PXi PYi≤100 | PYi | ≤ R
R, PXi PYi都是整数。数据之间有一个空格。
输出
对于每组测试数据,输出一行: 最少需要的极光点数。
样例输入
2
3 2
1 2
-3 1
2 1
1 5
5 5
样例输出
2
1

代码

#include<bits/stdc++.h> 
using namespace std;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)) { if(ch=='-') f=-1; ch=getchar(); }
	while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
	return x*f;
}
struct radar 
{  
    double left,right;  
}a[1005];
bool comp(radar a1,radar a2) 
{
    return a1.left<a2.left;
}
int main()  
{  
    double len;
    int t=read(); 
    while(t--)
    {
        int n=read(),r=read();
        for(int i=0;i<n;i++)
        {
            int x=read(),y=read();
            len=sqrt((double)r*r-(double)y*y);
            a[i].left=x-len;
            a[i].right=x+len;
        }
        sort(a,a+n,comp);
        double p=a[0].right;
        int count=1;
        for(int i=1;i<n;++i)
        {
            if(a[i].left>p)
                count++,p=a[i].right;
            else if(a[i].right<p)
            	p=a[i].right;
        }
        printf("%d\n",count);
    }
    return 0;
}

D.River Crossing

题目

参见 https://www.luogu.org/problemnew/show/P2904

描述 Description
Afandi is herding N sheep across the expanses of grassland when he finds himself blocked by a river. A single raft is available for transportation.
Afandi knows that he must ride on the raft for all crossings, but adding sheep to the raft makes it traverse the river more slowly.
When Afandi is on the raft alone, it can cross the river in M minutes When the i sheep are added, it takes Mi minutes longer to cross the river than with i-1 sheep (i.e., total M+M1 minutes with one sheep, M+M1+M2 with two, etc.).
Determine the minimum time it takes for Afandi to get all of the sheep across the river (including time returning to get more sheep).
输入格式 Input Format
第一行: K 表示有多少组测试数据。
接下来对每组测试数据有2行,第1行: N,M
第2行:t1 t2 …… tN
输出格式 Output Format
For each test case, output a line with the minimum time it takes for Afandi to get all of the sheep across the river.
样例输入 Sample Input
2
2 10
3
5
5 10
3
4
6
100
1
样例输出 Sample Output
18
50
时间限制 Time Limitation
1s
注释 Hint
2≤K≤5 1≤N≤1000 , 1≤M≤500. 1≤ti≤1000
数据之间有一个空格。
来源 Source
第六届河南省程序设计大赛
数据:郑懿鸣

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48),ch=getchar();
	return num*f;
}
int f[maxn];
int main()
{
	int k=read();
	while (k--)
	{
		int n=read(),m=read();
		for (int i=1;i<=n;++i)
			f[i]=1e9+1e8;//初始化 
		int tim=m;//一定会消耗的时间 
		for (int i=1;i<=n;++i)
		{
			int t=read();
			tim+=t;//带上这只羊 
			for (int j=i;j<=n;++j)
				f[j]=min(f[j],f[j-i]+tim+m);
			//f[j-i]+tim+m,没带这只羊的时间加上带这只羊的时间和一次返回的时间			
		}
		printf("%d\n",f[n]-m);//去掉一次返回的时间 
	}
	return 0;
}

E.沙漠迷宫

题目

描述 Description
传说jzyz大沙漠中有一个M*N迷宫,里面藏有许多宝物。某天,Teacher Qin找到了迷宫的地图,他发现迷宫内处处有宝物,最珍贵的宝物就藏在右下角,迷宫的进出口在左上角。当然,迷宫中的通路不是平坦的,到处都是陷阱。Teacher Qin决定让Mr.Jiang探险。
但Mr.Jiang从左上角走到右下角时,只会向下走或者向右走。从右下角往回走到左上角时,只会向上走或者向左走,而且他不走回头路。(即:一个点最多经过一次)。当然Mr.Jiang也会顺手也拿走沿路的每个宝物。
Teacher Qin希Mr.Jiang尽量多地带出宝物。请你编写程序,帮助Teacher Qin计算一下,Mr.Jiang最多能带出多少宝物。
输入格式 Input Format
第一行: K 表示有多少组测试数据。
接下来对每组测试数据:
第1行: 两个整数 M N
第2~M+1行: 每行N个整数 Ai1 Ai2 ……AiN (i=1,……,m)
输出格式 Output Format
对于每组测试数据,输出一行:Mr.Jiang携带出最多价值的宝物数
样例输入 Sample Input
2
2 3
0 10 10
10 10 80
3 3
0 3 9
2 8 5
5 7 100
样例输出 Sample Output
120
134
时间限制 Time Limitation
1s
注释 Hint
30% k≤2 1≤M,N≤20
60% k≤4 1≤M,N≤30
100% k≤5 1≤M,N≤50
来源 Source
第六届河南省程序设计大赛
特别特别厚道的数据数据:郑懿鸣

题解

此题和noip2008的传纸条是一样的。。。。。。。。。
https://www.luogu.org/problemnew/show/P1006 传送门
(为什么ACM这么多题都和noip一样哩?)

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=55;
inline ll read()
{
	ll f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48), ch=getchar();
	return num*f;
}
ll a[maxn][maxn],f[maxn*2][maxn][maxn];
int main()
{
	ll t=read();
	while (t--)
	{
		ll m=read(),n=read();
		for (int i=1;i<=m;++i)
			for (int j=1;j<=n;++j)
				a[i][j]=read();
		//F[sum][i][j]=max{F[sum-1][i][j]+F[k-1][i][j-1]+F[k-1][i-1][j]+F[k-1][i-1][j-1];
		memset(f,-1,sizeof(f));//赋初值为-1 (原因在后面) 
		f[2][1][1]=0;//最初的点,在左上角,好感度为0 
		for (int k=3;k<m+n;++k)
			for (int i=1;i<n;++i)
				for (int j=i+1;j<=n;++j)
				{
					ll x=f[k][i][j];
					x=max(f[k-1][i][j],x);
					x=max(f[k-1][i][j-1],x);
					x=max(f[k-1][i-1][j],x);
					x=max(f[k-1][i-1][j-1],x);
					if (x==-1) continue;//当s为-1时,说明四种情况都不能到该点,故不存在。
					f[k][i][j]=x+a[k-i][i]+a[k-j][j];
					//该点的值为最大的前一个值与当前F[k][i][j]表示两点的值的和。
				}
		printf("%lld\n",f[m+n-1][n-1][n]+a[m][n]);//因为i永远小于j,所以右下角的点不会求到,
		//但是到右下角只有一种情况,就是在右下角的上面和右下角的左边,直接输出就好了。
	}
	return 0;
}

F.能源公司([haoi2010]工厂选址)

题目

https://www.luogu.org/problemnew/show/P2514

题解

很好的贪心题。
1.首先枚举建立工厂的位置。
任意一个煤矿的煤要么运到原来的工厂,要么运到新建的工厂。
我们设sum代表煤矿i的煤运到新建的工厂的费用,sum+val【i】为煤矿i的煤运到原有工厂的费用。
因此对于每一单位的煤,它的运费要么是sum,要么是sum+val【i】。
由于所有煤的运费中都包含 项sum, 有b单位煤的运费中包含 项val【i】,
所以我们只需要使项val【i】尽量小。所以我们只需要预先计算出所有的val【i】,
然后排序, 就可以统计出答案。

代码

#include<bits/stdc++.h>
using namespace std;
const int N=55,M=5e4+5;
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48),ch=getchar();
	return num*f;
}
int a[M],h[N],c[N][M];
int val[M],id[M];
int k,cost=0x3f3f3f3f,m,b,n;
inline bool Orz(const int &x,const int &y)
{
	return val[x]<val[y];
}
int main()
{
	m=read(),b=read(),h[0]=read(),n=read();
	for (int i=1;i<=m;++i)
		a[i]=read();
	for (int i=1;i<=n;++i)
		h[i]=read();
	for (int i=0;i<=n;++i)
		for (int j=1;j<=m;++j)
			c[i][j]=read();
	for (int i=1;i<=n;++i)
	{
		int sum=h[i],tmp=b;
		for (int j=1;j<=m;++j)
		{
			sum+=c[i][j]*a[j];
			id[j]=j;
			val[j]=c[0][j]-c[i][j];
		}
		sort(id+1,id+m+1,Orz);
		for (int j=1;j<=m && tmp;++j)
			if (tmp>=a[id[j]])
				sum+=val[id[j]]*a[id[j]],tmp-=a[id[j]];
			else
				sum+=val[id[j]]*tmp,tmp=0;
		if (sum<cost)
			cost=sum,k=i;
	}
	printf("%d\n%d\n",k,cost+h[0]);
	return 0;
}

G.Adjacent Bit Counts

题目

描述 Description
For a string of n bits x1, x2, x3, …, xn, the adjacent bit count of the string is given by   Fun(x) = x1x2 + x2x3 + x3x 4 + … + xn-1x n
which counts the number of times a 1 bit is adjacent to another 1 bit. For example:
   Fun(011101101) = 3
   Fun(111101101) = 4
   Fun (010101010) = 0
Write a program which takes as input integers n and p and returns the number of bit strings x of n bits (out of 2^n) that satisfy Fun(x) = p.
For example, for 5 bit strings, there are 6 ways of getting fun(x) = 2:
11100, 01110, 00111, 10111, 11101, 11011
输入格式 Input Format
On the first line of the input is a single positive integer k, telling the number of test cases to follow. Each case is a single line that contains a decimal integer giving the number (n) of bits in the bit strings, followed by a single space, followed by a decimal integer § giving the desired adjacent bit count.
输出格式 Output Format
For each test case, output a line with the number of n-bit strings with adjacent bit count equal to p.
数字可能很大,只要输出对应的ULL结果就行了,不要用高精度,爆掉不用管
样例输入 Sample Input
2
5 2
20 8
样例输出 Sample Output
6
63426
时间限制 Time Limitation
1s
注释 Hint
1 ≤ k ≤ 10   1 ≤ n , p ≤ 100
来源 Source
第六届河南省程序设计大赛
数据:郑懿鸣

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=101;
inline ll read()
{
	ll f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48), ch=getchar();
	return num*f;
}
ll f[maxn][maxn][2];//f[达到的和][位置][0或1];
void work()
{
	f[0][1][0]=f[0][1][1]=1;
	for (int i=2;i<maxn;++i)
	{
		f[0][i][0]=f[0][i-1][0]+f[0][i-1][1];
		f[0][i][1]=f[0][i-1][0];
	}
	for (int i=1;i<maxn;++i)
		for (int j=1;j<maxn;++j)
		{
			f[i][j][0]=f[i][j-1][0]+f[i][j-1][1];
			f[i][j][1]=f[i][j-1][0]+f[i-1][j-1][1];
		}
	return ;
}
int main()
{
	work();
	ll t=read();
	while (t--)
	{
		ll n=read(),p=read();
		printf("%lld\n",f[p][n][0]+f[p][n][1]);
	}
	return 0;
}

H.最舒适的路线

题目

http://codevs.cn/problem/1001/

代码

#include<bits/stdc++.h>
using namespace std;
const int inf=1<<30;
const int N=5006;
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
	while (isdigit(ch)) num=(num<<1)+(num<<3)+(ch^48), ch=getchar();
	return num*f;
}
////////////并查集/////////////////
const int M=506;
int fa[M];
void init()
{
    for (int i=0;i<M;++i)
        fa[i]=i;
}
int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y)
{
    int root1=find(x);
    int root2=find(y);
    if(root1==root2)	return;
    fa[x]=y;
}
////////////并查集/////////////////
int n,m;
struct Node
{
    int a,b,v;
}node[N];
bool cmp(Node x,Node y)
{
    return x.v<y.v;
}
int gcd(int x,int y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
	{
        for(int i=0;i<m;++i)
			node[i].a=read(),node[i].b=read(),node[i].v=read();
        sort(node,node+m,cmp);
        int _max=inf;
        int _min=1;
        int st=read(),ed=read();
        for(int i=0;i<m;++i)
		{
            init();
            for(int j=i;j>=0;--j)
			{
                merge(node[j].a,node[j].b);
                if(find(st)==find(ed))
				{
                    if( (_max*1.0/_min) > (node[i].v*1.0/node[j].v) )
                        _max=node[i].v,_min=node[j].v;
                    break;
                }
            }
        }
        int r=gcd(_max,_min);
        if(_max==inf && _min==1)
		{
            printf("IMPOSSIBLE\n");
            continue;
        }
        _max/=r,_min/=r;
        if(_min==1)
            printf("%d\n",_max);
        else
            printf("%d/%d\n",_max/r,_min/r);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/85078618