hdu2141

试题链接:点击这里

在我看来,对于这道题的解法,有很多常规的方法,但是都是使用二分。但是这段代码我第一眼看见是非常的短的,当时很怀疑,因为自己写的东西还是在细节方面处理的非常的糟糕。然后看见这段代码相对于网上的其他代码来讲是非常的短的。仔细看了看,思想是非常的精妙的,和以前自己处理的一个问题有异曲同工之美。

好了,先把他的代码贴出来,然后最后在自己写一些常规的代码吧。希望以后能够将模板完全的敲出来。

#include <iostream>
#include <algorithm>

using namespace std;

int a[505],b[505],c[505];
int sab[250005];

bool find( int a, int l , int h )
{
 if( l > h )
  return false;
 int mid = ( l + h ) / 2;
 if ( sab[mid] == a )
  return true;
 else if( sab[mid] > a)
  find ( a, l, mid-1);
 else
  find ( a, mid+1, h);
}

//二分法本身就是利用递归的思想来实现的,
//现在的这种情况自己看看是否是用for循环好还是通过函数的递归调用实现好

int main()
{
 int l,n,m;
 int i,j,k;
 int s,sum,cnt = 1;

 while( cin >> l >> n >> m )
 {
  for( i = 0; i < l; i++)
   cin >> a[i];
  for( i = 0; i < n; i++)
   cin >> b[i];
  for( i = 0; i < m; i++)
   cin >> c[i];                                 //在这里是先将所有的数据都输入到数组中保存
  for(k = 0, i = 0;i < l; i++)
   for( j = 0; j < n; j++)
    sab[k++] = a[i] + b[j];                     //这个的思路是将两个合并,保证在运行的过程中程序是不会
  sort( sab, sab + k );
  cin >> s;
  cout << "Case " << cnt++ << ":\n";
  for( i = 0; i < s; i++ )
  {
   cin >> sum;
   for( j = 0; j < m; j++)
    if ( find( sum - c[j] , 0 , k-1) ) //查找是否有满足的和
     break;
   if( j == m)
    cout << "NO\n";
   else
    cout << "YES\n";
  }
 }
 return 0;
}

个人评价:

在写二分法的时候常常会用到函数递归的调用方式,但是个人还是不习惯使用递归,比较想通过循环的方式写出来。后面还是会努力试试其他的方法,并且找到最合适的一种留下来使用。

hdu2141

时间: 2024-08-25 01:53:59

hdu2141的相关文章

hdu-2141

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 29184    Accepted Submission(s): 7286 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

HDU2141——二分——Can you find it?

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case is described as foll

HDU2141【hash】

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case is described as foll

hdu-2141(二分查找+暴力)

Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case is descr

hdu 2141 Can you find it?(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. 1 /* 2 3 x^2+6*x-7==y 4 输入y 求x 精确度为10^-5 5 0=<x<=10000 6 7 */ 8 #include <iostream> 9 #include <cstdio> 10 using namespace std; 11 int main (void) 1

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小时连题解都出来了··· A-烤肉拌饭 ( uva 572) 就是求联通块的数量啊,刚学dfs的时候做的那种! 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <ios