1427 determine whether x = a + B exists

Source: Internet
Author: User


Judge whether there are two integers A and B in the Set S, and the sum of them is integer x.


An integer in the first row indicates the number of groups (multiple groups of data). For each group of data, the first row has two integers n (1 ≤ n ≤ 10000 ), M (1 ≤ m ≤ 100 ). The second row is the set of n different integers. The third row has m Integers to be verified.


Output a line for each integer to be verified. If the set S contains two integers and the sum of the Integers to be verified, output "yes"; otherwise, output "no ".

Solution: this problem can be implemented using an array. The input number is considered as the subscript of the array, and all given numbers are marked as 1 in the array, that is, result [A [I] = 1; then, for each X, subtract a [I] to determine whether the remaining number is marked in the array. Considering that A and B are interchangeable, you only need to find half of the elements.


# Include <stdio. h> main () {int number, Te; int I, J, K, M; int number1, number2; int A [10000]; int result [100000]; int B; int flag; scanf ("% d", & number); For (TE = 1; te <= number; Te ++) {for (k = 0; k <100000; k ++) result [k] = 0; scanf ("% d", & number1, & number2); for (I = 1; I <= number1; I ++) {scanf ("% d", & A [I]); Result [A [I] = 1 ;}for (I = 1; I <= number2; I ++) {flag = 0; scanf ("% d", & B); For (j = 1; j <= number1/2; j ++) {If (result [B-A [J] = 1) Flag = 1; elsecontinue;} If (flag = 1) printf ("Yes \ n"); else printf ("NO \ n ");}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.