hdu2141Can you find it?
给你四个集合。要你从这四个集合中
各取出一个数出来,推断,取出的前三个数的和
是否等于第四个数。
数据比較大。我的做法是将
前两个集合全部数全部和的情况取出来,
然后二分查找第四个集合和第三集合取出的数的差值。
我比較懒。用了stl中的bsearch实现二分查找。
代码例如以下:
#include<iostream>
#include<cstdlib>
using namespace std;
int num_a,num_b,num_c,a[510],b[510],c[510],ab[250010];
int cmp(const void *a,const void *b)
{
return *(int *)a - *(int *)b;
}
void init()
{
int num_x,x,*p,tmp;
for(int i=0;i<num_a;i++)
scanf("%d",&a[i]);
for(int i=0;i<num_b;i++)
scanf("%d",&b[i]);
for(int i=0;i<num_a;i++)
for(int j=0;j<num_b;j++)
ab[i*num_b+j]=a[i]+b[j];
for(int i=0;i<num_c;i++)
scanf("%d",&c[i]);
qsort(ab,num_a*num_b,sizeof(int),cmp);
scanf("%d",&num_x);
while(num_x--)
{
scanf("%d",&x);
for(int i=0;i<num_c;i++)
{
tmp=x-c[i];
p=(int*)bsearch(&tmp,ab,num_a*num_b,sizeof(int),cmp);
if(p!=NULL)
{
printf("YES\n");
break;
}
}
if(p==NULL)
printf("NO\n");
}
}
int main()
{
int exp=0;
while(scanf("%d%d%d",&num_a,&num_b,&num_c)!=EOF)
{
printf("Case %d:\n",++exp);
init();
}
}
hdu2141Can you find it?的更多相关文章
- hdoj-2141-Can you find it?(二分查找)
题目链接 /* Name:HDU-2141-Can you find it? Copyright: Author: Date: 2018/4/12 17:15:46 Description: 暴力,复 ...
随机推荐
- centos7 mariadb mysql max_connections=214 无法修改的问题
centos7 mariadb mysql max_connections=214 无法修改的问题 /etc/my.cnf.d/mariadb-server.cnf [mysqld] max_conn ...
- swiper单屏滚动
.swiper-slide { overflow: auto; } 1. 排除某些屏,不滚动 var startScroll, touchStart, touchCurrent; var aSlide ...
- 秀秀的照片(photo)
秀秀的照片(photo) 题目描述 华华在和秀秀视频时有截很多图.华华发现秀秀的每一张照片都很萌很可爱.为什么会这样呢?华华在仔细看过秀秀的所有照片后,发现秀秀的照片都具有一个相同的性质. 设秀秀的分 ...
- codechef May Challenge 2016 CHSC: Che and ig Soccer dfs处理
Description All submissions for this problem are available. Read problems statements in Mandarin Chi ...
- 关于在生命周期当中进行setState操作的问题
当需要监听组件的属性改变的时候有两个生命周期componentWillReceiveProps和componentWillUpdate 当使用componentWillUpdate的时候可以获取到将要 ...
- 【UVA10561】Treblecross(SG函数)
题意:有n个格子排成一行,其中一些格子里面有字符X.两个游戏者轮流操作,每次可以选一个空格,在里面放上字符X. 如果此时有3个连续的X出现,则该游戏者赢得比赛.初始条件下不会有3个X连续出现. 判断先 ...
- [LeetCode] Remove Duplicates from Sorted List 链表
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- python 保留两位小数方法
原博客连接:https://blog.csdn.net/Jerry_1126/article/details/85009810 保留两位小数,并做四舍五入处理 方法一:使用字符串格式化 a = 12. ...
- centos7下mysql双主+keepalived
一.keepalived简介 keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测 ...
- jenkins下脚本权限问题
在jenkins环境下,执行需要root权限的脚本,报错. 修改方法: 1. centos环境下,在/etc/sudoers.d/ 目录下,增加一个 jenkins文件,内容如下: Defaults: ...