-1

I am attempting to solve https://vjudge.net/problem/UVA-10106 which is a problem where you need to multiply two big numbers.

The code shows the correct answer when run on my computer but the online judge gives "wrong answer".

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);

    string a, b;
    cin >> a >> b;
    vector<int> c, d;
    for (int i = a.size() - 1; i >= 0; i--) {
        int z = a[i] - 48;
        c.push_back(z);
    }
    for (int i = b.size() - 1; i >= 0; i--) {
        int x = b[i] - 48;
        d.push_back(x);
    }
    if (c.size() > d.size()) {
        c.swap(d);
    }
    int l = c.size() + d.size();
    vector<int> s(l, 0);
    int carry = 0, sum = 0;
    for (int i = 0; i < c.size(); i++) {
        int t;
        for (int j = 0; j < d.size(); j++) {
            sum = c[i] * d[j] + carry + s[i + j];
            carry = sum / 10;
            s[i + j] = sum % 10;
            t = i + j;
            sum = 0;
        }
        if (carry != 0) {
            s[t + 1] = carry;
            carry = 0;
        }
    }

    for (int i = l - 1; i >= 0; i--) {
        if (s[i] != 0) {
            break;
        }
        else {
            l--;
        }
    }
    for (int i = l - 1; i >= 0; i--) {

        cout << s[i];
    }
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
lacker01
  • 11
  • 1
  • 1
    What is uva 10106? You probably know, but we don't. Please describe more detailed what the code is supposed to do. Also you should read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Lukas-T Aug 04 '20 at 05:52
  • what did you expect to happen? ... what did happen? – jsotola Aug 04 '20 at 05:53
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please learn how to [edit] your questions to improve them. – Some programmer dude Aug 04 '20 at 05:55
  • 1
    On a more personal note, please don't use online competition/judge sites as a learning or teaching resource. All they teach are very bad habits (some which are already mentioned). First please learn one or more languages from the ground up, then learn the basic algorithms and data-structures, and multiple variants of them and how to implement and use them in multiple languages. Then use such sites as a *training* resource to keep your knowledge fresh. – Some programmer dude Aug 04 '20 at 05:57
  • And another note to improve your question or future questions: Compilers might not care about spaces or variable names, but humans do. Add some spaces between operators and operands and use meaningfull variable names. It will make your code way easier to read, understand and debug for you and us. – Lukas-T Aug 04 '20 at 06:00
  • 2
    [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) / [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058) – Jesper Juhl Aug 04 '20 at 06:54
  • You only read one set of inputs, the online judge is expecting you to process multiple inputs? – Alan Birtles Aug 04 '20 at 07:13

1 Answers1

0

I think the problem is that you only ever handle one query. But it seems like your code should handle multiple queries. You can put all of your code in while-loop with the added benefit of validating the input. And secondly you don't print a newline to seperate your outputs.

Something like this might work:

#include <vector>
#include <string>

int main()
{
    std::string a, b;
    while (std::cin >> a >> b) // loops as long as there are two strings to read
    {
        /* Rest of your code goes here */

        std::cout << '\n'; //end output of query with new line
    }
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30