博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ_ACM_Can you find it?
阅读量:6716 次
发布时间:2019-06-25

本文共 3803 字,大约阅读时间需要 12 分钟。

Problem 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 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.
 
Output
For 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 31 2 31 2 31 2 331410
 
Sample Output
Case 1:NOYESNO
 
Code
1 #include 
2 #include
3 #define N 500 4 int a[N + 5], b[N + 5], c[N + 5], sumAB[N * N + 5]; 5 int compare(const void *a, const void *b) 6 { 7 return *(int*)a - *(int*)b; 8 } 9 void main()10 {11 int countA, countB, countC, countAB, countX;12 int i, j, x, target;13 int min, max, mid;14 int result, n;15 n = 1;16 while (scanf("%d %d %d", &countA, &countB, &countC) != EOF)17 {18 for (i = 0; i < countA; i++)19 scanf("%d", &a[i]);20 for (i = 0; i < countB; i++)21 scanf("%d", &b[i]);22 for (i = 0; i < countC; i++)23 scanf("%d", &c[i]);24 for (i = 0; i < countA; i++)25 for (j = 0; j < countB; j++)26 sumAB[i * countB + j] = a[i] + b[j];27 countAB = countA * countB;28 qsort(sumAB, countAB, sizeof(int), compare);29 scanf("%d", &countX);30 for (i = 0; i < countX; i++)31 {32 scanf("%d", &x);33 for (j = 0; j < countC; j++)34 {35 target = x - c[j];36 result = 0;37 min = 0;38 max = countAB - 1;39 while (min <= max)40 {41 mid = (min + max) / 2;42 if (sumAB[mid] == target)43 {44 result = 1;45 break;46 }47 else if (sumAB[mid] > target)48 max = mid - 1;49 else50 min = mid + 1;51 }52 if (result == 1)53 break;54 }55 if (i == 0)56 printf("Case %d:\n", n++);57 if (result)58 printf("YES\n");59 else60 printf("NO\n");61 }62 }63 }

 

 
Key Points
Firstly of all, I should be careful. I take a whole afternoon to find the fault which is missing the ":".
Secondly, I should proficient in the binsearch algorithm, whose range is [], and the condition is ≥, and u must add -1 or +1.
Thirdly,  in C, u can only use qsort. and if u want use the function, u must add the head file "#include <stdlib.h>", and the "compare" function.
Fourthly, if u want use function, such strcpy, u should add "#include <string.h>", which is belong to C. And if u want use the type of "string", u should add "#include <string> using namespace std;". Those head files are really different.
Last but not the least, if u want debug, u can remember those short key, likes  Ctrl+F10, which means u can run until the cursor, Shift+F5, which means stop.
 
Recommend
威士忌
 

转载于:https://www.cnblogs.com/chuanlong/archive/2013/03/11/2954429.html

你可能感兴趣的文章
手机3D游戏开发:自定义Joystick的相关设置和脚本源码
查看>>
window.frames["detailFrm"].isSubmitting = true;//?起什么作用
查看>>
ASCII表
查看>>
idea之debug
查看>>
什么是真正的流程管理?流程管理的是与不是。
查看>>
洛谷P1613 跑路
查看>>
python各种模块,迭代器,生成器
查看>>
微信小程序 watch监听数据变化 类似vue中的watch
查看>>
服务器端推送技术
查看>>
使用Formik轻松开发更高质量的React表单(三)<Formik />解析
查看>>
修改SQL Server 的排序规则
查看>>
Windows 8部署系列PART2:部署先决条件准备
查看>>
微软私有云分享(R2)18Windows Azure Pack 命令行安装
查看>>
基于ArcGIS10.0和Oracle10g的空间数据管理平台十五(C#开发)-空间数据导出
查看>>
DB2 应用
查看>>
第十六章 为什么说张清“虎头蛇尾”
查看>>
ShiftOperators.cs
查看>>
C#中的预处理命令
查看>>
Assistance Required(打表)
查看>>
使用Ajax的Time实现倒计时功能
查看>>