码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 6275 Joint Venture(查找等值)

时间:2014-07-06 09:56:53      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:数学

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题意:

大意:问是否存在两块乐高的长度等于上面给出的长度;


一、二分查找版;

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,num[1000047];
int main()
{
	int i, j, x;
	
	while(scanf("%d",&x)!=EOF)
	{
		memset(num,0,sizeof(num));
		scanf("%d",&n);
		for( i = 1; i <= n; i++)
		{
			scanf("%d",&num[i]);
		}
		sort(num+1,num+n+1);
		int ll = x*10000000, t,a,b;
		int l, r, mid;
		int flag = 0;
		for(i = 1; i <= n; i++)
		{	
			l = i+1, r = n, mid = 0;
			t = ll-num[i];
			while(l<=r)
			{
				mid = (l+r)/2;
				if(t > num[mid])
					l = mid+1;
				else if(t < num[mid])
					r = mid-1;
				else
				{
					flag = 1;
					a = num[i];
					b = num[mid];
					break;
				}
			}
			if(flag == 1)
				break;
		}
		if(flag == 0)
			printf("danger\n");
		else
			printf("yes %d %d\n",a,b);
	}
	return 0;
}


二、非二分查找版:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,num[1000047];
int main()
{
	int i, j, x;
	
	while(scanf("%d",&x)!=EOF)
	{
		memset(num,0,sizeof(num));
		scanf("%d",&n);
		for( i = 1; i <= n; i++)
		{
			scanf("%d",&num[i]);
		}
		sort(num+1,num+n+1);
		int ll = x*10000000, t,a,b;
		int l, r, mid;
		int flag = 0;
		j = n;
        i = 1;
        while(i<j)
        {
            if(num[i] + num[j] == ll)
            {
                flag = 1;
                a = num[i];
                b = num[j];
                break;
            }
            else if(num[i] + num[j] < ll)
                i++;
            else if(num[i] + num[j] > ll)
                j--;
        }
		if(flag == 0)
			printf("danger\n");
		else
			printf("yes %d %d\n",a,b);
	}
	return 0;
}


UVALive 6275 Joint Venture(查找等值),布布扣,bubuko.com

UVALive 6275 Joint Venture(查找等值)

标签:数学

原文地址:http://blog.csdn.net/u012860063/article/details/36905763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!