B - Can you find it? 与C - 4 Values whose Sum is 0

B:

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. 
InputThere are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers. 
OutputFor each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO". 
Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10
Sample Output
Case 1:
NO
YES
NO

C:


The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2  28 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5

思路:这两题的想法是一致的,先行将其中的几个数组合并为一个数组,将其排序,然后再循环的选择最后的那个数组的每一个数值,对排好序的数组进行二分判断。

注:在这里B题我碰到了一个问题,

我犯了一个很智障的问题,开始的时候我以为这两种的算法表示的时间复杂度是一样的所以就能用了。但是前面的那个tl了,后面而定ac,我很困惑,后来才知道第一个那样写会死循环,就比方说temp值我设置为9,然后a【4】=8,a【5】=10,然后mid值为4,a【mid】<temp,然后l还是4,就一直循环,就崩了。以后还是尽量写成第二种形式,防止出错。

B题代码:

#include <iostream>   
#include <algorithm>   
using namespace std;
int a[505];
int b[505];
int c[505];
int d[505 * 505];
int main()
{
int L, N, M, S;
int count = 1;
while (scanf("%d%d%d", &L, &N, &M) == 3)
{
for (int i = 0; i < L; i++)
cin >> a[i];
for (int i = 0; i < N; i++)
cin >> b[i];
for (int i = 0; i < M; i++)
cin >> c[i];
int len = 0;
for (int i = 0; i<L; i++)
{
for (int j = 0; j<N; j++)
{
d[len++] = a[i] + b[j];
}
}
sort(d, d + len);
cin >> S;
cout << "Case " << count++ << ":" << endl;
while (S--)
{
int x;
cin >> x;
int flag = 0;
for (int j = 0; j<M; j++)
{
int temp = x - c[j];
int left = 0, right = len - 1;
int mid;
while (left <= right)
{
mid = (left + right) / 2;
if (d[mid]>temp)
{
right = mid - 1;
}
else if (d[mid]<temp)
{
left = mid + 1;
}
else
{
flag = 1;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}

C题代码:

#include <iostream>    
#include <algorithm>  
using namespace std;
int a[4005][5], sum1[4005 * 4005], sum2[4005 * 4005];
int main()
{
int n, mid;
while (~scanf("%d", &n))
{
for (int i = 0; i<n; i++)
{
cin >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4];
}
int len1 = 0;
int len2 = 0;
for (int i = 0; i<n; i++)
for (int j = 0; j<n; j++)
{
sum1[len1++] = a[i][1] + a[j][2];
sum2[len2++] = a[i][3] + a[j][4];
}
sort(sum2, sum2 + len2);
int count = 0;
for (int i = 0; i<len1; i++)
{
int left = 0;
int right = len2 - 1;
while (left <= right)
{
mid = (left + right) / 2;
if (sum1[i] + sum2[mid] == 0)
{
count++;
for (int j = mid + 1; j<len2; j++)//由于这个数组是允许重复的,所以在找到一个符合题意的数值的时候
{
if (sum1[i] + sum2[j] != 0)
break;
else
count++;
}
for (int j = mid - 1; j >= 0; j--)
{
if (sum1[i] + sum2[j] != 0)
break;
else
count++;
}
break;
}
if (sum1[i] + sum2[mid]<0)
left = mid + 1;
else
right = mid - 1;
}
}
cout << count << endl;
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值