-5

I'm trying to solve this problem from an online judge (Codeforces):

One day Deivis came across two Vectors of integers A and B, and wondered, could it be possible to form the number X by adding an element of A to another element of B?

More formally, it is possible to choose two indexes i and j such that Ai + Bj = x?

Input
The first entry line is two integers n and x. The second line contains n numbers, the vector A. The third and last line contains n numbers, vector B.

Output
Print 1 if it is possible to form the number x from a sum of one element of each vector, and 0 otherwise."

My problem is that I can not fill in the second vector, when the program runs on the site it fills the vector with zeros. I am using C ++, here's my code:

#include <bits/stdc++.h>

using namespace std;

#define MAX 10

int main()
{
    int n, x, i = 0, j = 0, resp = 0, sum;

    vector<int> vetA(MAX), vetB(MAX);

    cin >> n >> x;

    while (scanf("%d", &vetA[i]) == 1)
        i++;

    while (scanf("%d", &vetB[j]) == 1)
        j++;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            sum = vetA[i] + vetB[j];
            if (sum == x)
            {
                resp = 1;
                goto END;
            }
        }
    }

    END: printf("%d", resp);

    return 0;
}

I try to use getchar() after each while loop, but seems that on the site it does not do data capture like on a keyboard, and so the second vector isn't receiving any data. I've also tried to capture data as a std::string but that doesn't work.

Can someone help me?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Why do you think you need `#include `? It's probably going to waste more time when you compile it than the time it takes you to type a few specific `#include` lines. – eesiraed Apr 07 '18 at 18:06
  • If the "online judge" doesn't immediately reject `#include ` then it's a crap judge - how surprising is that? –  Apr 07 '18 at 18:08
  • @NeilButterworth Some of the code I see on competitive programming sites like these are just shocking. They abuse macros to an unimaginable degree. Such as defining a macro `fi` for `first`, besides macros for stuff like for loops. But of course, all the online judge cares about is if you're program prints the right results in time. – eesiraed Apr 07 '18 at 18:10
  • Also, why are you using C I/O? – eesiraed Apr 07 '18 at 18:13
  • @NeilButterworth I do competitive programming in the USA Computing Olympiad, but I definitely don't write junk code like that. – eesiraed Apr 07 '18 at 18:17
  • @FeiXiang One good apple doesn't save all the spoiled ones! But it will do I suppose... – DeiDei Apr 07 '18 at 18:19
  • @Fei Xiang Yes but I'm trying to learn some things and they gave me the idea to use this #include. – Andrei_Buslik Apr 07 '18 at 18:19
  • Like I said, throw away the `#include ` and the C I/O. Why don't you just have a loop that loops exactly `n` times for each vector? And then you can just do `cin >> vetA[i]` in the loop. – eesiraed Apr 07 '18 at 18:22
  • Oh, and also throw away the `goto` statements. – eesiraed Apr 07 '18 at 18:24
  • 1
    The OP asked a question about competitive programming. Comments criticizing common practices in competitive programming on account of being ill-advised in real programming are off-topic. – Brian Bi Apr 07 '18 at 18:26
  • @Fei Xiang I know that is not the case for this specific problem but the complexity of *printf* and *scanf* are linear like *cin* and *cout* but for very large C I/O are faster, no? I'll try this idea you gave. Thank you. – Andrei_Buslik Apr 07 '18 at 18:28
  • As far as I know, no, at least not enough to matter, especially if you turn off syncing with stdio. – eesiraed Apr 07 '18 at 18:30
  • 1
    Some good reading on that last comment: https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull – user4581301 Apr 07 '18 at 18:31
  • I don't suppose you have any idea what inputs the judge is using to trigger the error you are seeing? – user4581301 Apr 07 '18 at 18:32
  • 2
    The first `while` loop doesn't read `n` numbers as required by the problem statement. It reads numbers (ignoring whitespace) until it runs out of input. You are stuffing both input arrays into `vetA`, possibly writing past the end of the container. The second `while` loop has nothing left to read. – Blastfurnace Apr 07 '18 at 18:55
  • 1
    Don't cross the {i/o} streams. Use either C++ I/O or C I/O, don't mix them. – Thomas Matthews Apr 07 '18 at 19:04
  • @Blastfurnace Yes, i used the idea that Fei Xiang gave me, thanks – Andrei_Buslik Apr 07 '18 at 21:08
  • @Blastfurnace I thought I had no problem using the two together but I will not use it any more, thank you. – Andrei_Buslik Apr 07 '18 at 21:09

1 Answers1

1

Here are some hints/examples to compare your program to:

#include <iostream> //Include each standard library seperately
#include <vector> //#include <bits/stdc++.h> is bad practice

// Only declare variables as they are used. 
int n; // Better coding practice is one variable per line.
int x; // Competitions shouldn't care how many lines.

if (!(std::cin >> n >> x)) //This is basically the same as cin.fail()
{
  std::cerr << "Error inputting data.\n";
  return 1;
}

// Now create the vectors, after the size has read in.
std::vector<int> vetA(n);
std::vector<int> vetB(n);

// The number of elements is known, so use a "for" loop.
for (size_t i = 0; i < n; ++i)
{
  std::cin >> vetA[i];
}

for (size_t i = 0; i < x; ++i)
{
  std::cin >> vetB[i];
}

You should add in some error handling because your program will be given some invalid inputs.

The inputs and vector sizes are examples since you didn't specify the input format in your Post.

eesiraed
  • 4,626
  • 4
  • 16
  • 34
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154