hash
来源:互联网 发布:法律大数据 编辑:程序博客网 时间:2024/06/05 06:10
Description
现在给出A,B,C,D四个集合,每个集合中元素的个数n都是相同的,现在从每个集合中任取出一个数,记为a,b,c,d,现在要求统计有多少组不同的(a,b,c,d)使得a+b+c+d=0。
Input
第一行为各集合中元素的个数n;
第二行到第n+1行,每行有4个数字,依次为A,B,C,D中的一个元素。
a,b,c,d的绝对值小于1000000,n小于500。
Output
一行,为不同的(a,b,c,d)使得a+b+c+d=0的组数。
Sample Input
6-45 22 42 -16-41 -27 56 30-36 53 -37 77-36 30 -75 -4626 -38 -10 62-32 -54 -6 45
Sample Output
5
#include <iostream>using namespace std;#define maxlen 500 int main(){ int n; while (cin >> n) { int groupnum = 0; int A[maxlen]; int B[maxlen]; int C[maxlen]; int D[maxlen]; int AB[maxlen*maxlen]; int CD[maxlen*maxlen]; int hashtable[2 *maxlen* maxlen]; int i, j,count=0; //初始化hashtable for (i = 0; i < 2 * maxlen*maxlen; i++) hashtable[i] = 1000002; for (i=0; i < n; i++) cin >> A[i] >> B[i] >> C[i] >> D[i]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { AB[count] = A[i] + B[j]; count++; } count = 0; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { CD[count] = C[i] + D[j]; count++; } //以上数组均正确 //hash for (i = 0; i < n*n; i++) { int temp = CD[i] % maxlen; while (hashtable[temp] != 1000002) temp++; hashtable[temp] = CD[i]; } for (i = 0; i < n*n; i++) { int templ =( -AB[i]) % maxlen; while (hashtable[templ] != 1000002) { if ((AB[i]+ hashtable[templ])==0) { groupnum++; break; } templ++; } } cout << groupnum << endl; } return 0;}/************************************************************** Problem: **** User: **** Language: C++ Result: Accepted Time:4 ms Memory:5124 kb****************************************************************/
0 0
- hash
- Hash
- hash
- hash
- Hash
- hash
- Hash
- HASH
- hash
- hash
- HASH
- hash
- hash
- Hash
- hash
- hash
- Hash...
- hash
- Mybatis中运用小技巧(二) like的使用
- javascript正则RegExp.test()需要注意:不具有可重入性
- 欢迎使用CSDN-markdown编辑器备份
- 带头双向循环链表的插入和删除
- Socket简单实例(简单实现调用accpet()方法开始监听)
- hash
- 字符串匹配的BM算法讲解(不含代码)
- python文本分类准备工作-扩展库的安装
- PHP 安全性漫谈 Linux+Apache+Mysql+PHP
- Volley(1)—发送get和post请求
- js+css实现回到顶部按钮(back to top)
- getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的作用
- 在当前文件夹下打开cmd命令
- LeetCode | 1.Two Sum