Subtraction with #define

Problem: http://www.codechef.com/problems/TLG
I have written a solution to solve the problem in the link above. I have #define-d as in the codes below to get the bigger and the smaller numbers. I tested it with the sample input in the link but it did not output as my expectation. The sample output is: 1 58, but mine was: 1 140. So I tried to debug it, here what I got:

As you can see, the second line of the input is: 140 82
So si == 140 and ti == 82
max(si, ti) == si == 140
min(si, ti) == ti == 82
but max(si, ti) - min(si, ti) == 140 //max minus min
Why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define lead(a,b) a>b?1:2

using namespace std;

int main()
{
   unsigned int n, si, ti, W, L = 0;
   cin >> n;
   for (int i = 0; i < n; i++) {
       cin >> si >> ti;
       int l = max(si,ti) - min(si,ti); \\problem here
       if (l > L) {
           W = lead(si, ti);
           L = l;
       }
   }
   
   cout << W << ' ' << L;
   
   return 0;
}


I used
1
2
3
int max(int a, int b) return a>b?a:b;

int min(int a, int b) return a<b?a:b;

instead of #define-ing and it worked but still need to know why? (The question up there)
Last edited on
is there any reason you are trying to use macro's? They are evil (mostly).
It just popped up in my head when I was coding.
well i think this is what your line 14 evaluates to after preprocessing (i think):

si>ti?si:ti-si<ti?si:ti

Not nice. I'm not even sure what the minus is actually doing in this case.

edit: have a read of this:
http://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives
Last edited on
So

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
#include <iostream>

using namespace std;

int max(int a, int b) {
	if (a>b) return a;
	return b;
}

int min(int a, int b) {
	if (a<b) return a;
	return b;
}

int lead(int a, int b) {
	if (a>b) return 1;
	return 2;
}

int main()
{
   int n, si, ti, W, L = 0;
   cin >> n;
   for (int i = 0; i < n; i++) {
       cin >> si >> ti;
       int l = max(si,ti) - min(si,ti);
       if (l > L) {
           W = lead(si, ti);
           L = l;
       }
   }
   
   cout << W << ' ' << L << '\n';
   
   return 0;
}


Submitted
Got wrong answer
Sucks
if i type

1
140 40


my output is

1 100


That looks right to me, so check your looping.
Are you meant to be summing the results for each player to determine the winner? The example has this:


140 82
89 134
90 110
112 106
88 90

but says player one is the winner? So i guess it's just looking at the highest value regardless of the number of 'games'?


edit: okay, i've read it properly now sorry.

You want to store the results and the difference in a vector or something.
then when you've got them all search that vector for the highest value.
Last edited on
OP: bit of a rush job and needs tidying, but...

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
#include <iostream>
#include <tuple>
#include <vector>



int getMax(int a, int b) {
	return a>b?a:b;
}

int getMin(int a, int b) {
	return a<b?a:b;
}

std::tuple<int,int,int> GetElementWithBiggestDifference(std::vector<std::tuple<int,int,int>> scores)
{
	std::tuple<int,int,int> maxValue(std::make_tuple(-1, -1, -1));
	std::vector<std::tuple<int,int,int>>::iterator it;

	for(it = scores.begin(); it!= scores.end();it++)
	{
		std::tuple<int,int,int> rtn = *it;
		int difference = std::get<2>(rtn);

		if(abs(difference) > abs(std::get<2>(maxValue)))
		{
			maxValue = *it;
		}
	}

	return maxValue;
}



int main()
{
	std::vector<std::tuple<int,int,int>> scores;

	int n, si, ti;
	std::cin >> n;

	for (int i = 0; i < n; i++) {		
		
		std::cin >> si >> ti;
		scores.push_back(std::make_tuple(si, ti, si-ti)); 		
	}


	std::tuple<int,int,int> elementOfInterest = GetElementWithBiggestDifference(scores);


	int playerWon = (std::get<2>(elementOfInterest) > 0) ? 1 : 2;
	int playerOneScore = std::get<0>(elementOfInterest);
	int playerTwoScore = std::get<1>(elementOfInterest);

	// calc difference & print to screen.

	return 0;
}
Last edited on
Reading it and seeing what tuple is.
http://www.cplusplus.com/reference/tuple/tuple/

Update:
It's kinda complex. I have to leave now. I'll come back tomorrow. See ya.
Last edited on
it's simpler than yours :)
For example, why are you even calculating min and max of the scores?
You don't need to use tuples at all, you could use a normal array.
Last edited on
Yesterday, I realized that I had read the problem without regard. The scores of a round are sums of all previous scores (in all previous rounds). So:
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
#include <iostream>
#include <cmath>

using namespace std;

int lead(int a, int b) {
	if (a>b) return 1;
	return 2;
}

int main()
{
   int n, si, ti, Si = 0, Ti = 0, W, L = 0;
   cin >> n;
   for (int i = 0; i < n; i++) {
       cin >> si >> ti;
       Si = si += Si; Ti = ti += Ti;
       int l = abs(si-ti);
       if (l > L) {
           W = lead(si, ti);
           L = l;
       }
   }
   
   cout << W << ' ' << L << '\n';
   
   return 0;
}

solve the problem. Thanks for your help.
Topic archived. No new replies allowed.