UVa 700

Hi, this is the solution for UVa 700, I'm not sure why it is returning Wrong Answer on the judge all the time, could someone tell me what I'm missing?
It works fine in my testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>

using namespace std;

int findYear(int n, int arr[][3]) {
    bool existYear[n][10000];
    bool atLeast1Year=false;
    int sum=arr[0][0];
    int increment=arr[0][2]-arr[0][1];
    while(sum<10000) {
        existYear[0][sum]=true;
        sum=sum+increment;
    }
    for (int loopN=1; loopN<n-1; loopN++) {
        atLeast1Year=false;
        sum=arr[loopN][0];
        increment=arr[loopN][2]-arr[loopN][1];
        while (sum<10000) {
            if (existYear[loopN-1][sum]==true) {
                existYear[loopN][sum]=true;
                atLeast1Year=true;
            }
            sum+=increment;
        }
        if (atLeast1Year==false) {
            return -1;
        }

    }
    atLeast1Year=false;
    sum=arr[n-1][0];
    increment=arr[n-1][2]-arr[n-1][1];
    while (sum<10000) {
    if (existYear[n-2][sum]==true) {
        existYear[n-1][sum]=true;
        return sum;
    }
    sum+=increment;
    }
    if (atLeast1Year==false) {
        return -1;
    }
}

int n;
int caseNum=1;
int returnNum;
bool spaceBefore=false;

int main()
{
    while (1) {
        cin >> n;
        if (n==0) return 0;
        int arr[n][3];
        for (int loop=0;loop<n;loop++) {
            for (int loop2=0;loop2<3;loop2++) {
                cin>> arr[loop][loop2];
            }
        }
        if (spaceBefore) {
            cout << endl;
        }
        if (n==1) {
            cout << "Case #" << caseNum << ":" << endl << "The actual year is " <<
            arr[0][0]<< "." << endl;
            caseNum++;
            spaceBefore=true;
            continue;
        }
        returnNum=findYear(n, arr);
        if (returnNum==-1) {
            cout << "Case #" << caseNum << ":" << endl << "Unknown bugs detected." << endl;
            caseNum++;
            spaceBefore=true;
            continue;
        }
        else {
            cout << "Case #" << caseNum << ":" << endl << "The actual year is " <<
            returnNum << "." << endl;
            caseNum++;
            spaceBefore=true;
            continue;
        }



    }
    return 0;
}
Last edited on
The first problem you have is compile errors. Fix them first.

(Set your compiler to use C++14 as strictly as possible. That means you'll have to get rid of those VLAs — you really don't need them — and fix the drop-through bug in findYear().)

Hope this helps.
I found the problem, it is when I initialized arrays I assumed all the values are false, but although it is on my compiler it isn't on the judge's compiler. I fixed this by initializing a 2D array using this;
1
2
3
4
bool** existYear=new bool*[n];
for (int loop=0; loop<n; loop++) {
existYear[loop]=new bool[10000](); // () needed for initializing w/ all elements set to false in all compilers
}
You may just be lucky on your own compiler too. Only global variables are guaranteed initialized to zeros.

Glad you found it!

(Also, I enjoyed doing the problem myself. :O)
Topic archived. No new replies allowed.