Logical question

closed account (13bSLyTq)
Hi,

As you guys know I was trying to improve my logic and problem solving through programming and so I decided to take on Olympiad challenges for Informatics. The question I could not get is in:
http://www.olympiad.org.uk/papers/2007/bio/bio07exam.pdf Question 1: Cards

I understand the first part of the problem as it basically counting the pairs however the second part of the problem is quite tough in my opinion as it requires algorithm to find a combination of numbers that add up to 15. This is where I get stumped.

Does anyone here have a clear logical idea on how to solve this programatically, it would be much appricated if you could explain logic behind any code posted as I am eager to learn from you much experienced technocrats! :D

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
#include <iostream>
using namespace std;

int main()
{
     int cards[5];
     for(int i = 0; i < 5; i++)
     {
          cout << "Enter a number from 1 to 10: ";
          cin >> cards[i];
          while(cards[i] < 1 || cards[i] > 10)
          {
               cout << "\nInvalid value. Enter another value: ";
               cin >> cards[i];  
          }
     }
     const int max = cards[0] + cards[1] + cards[2] + cards[3] + cards[4];
     int points = 0;
     if(max == 15)
          points++;
     for(int i = 0; i < 5; i++)
     {
          if(max - cards[i] == 15)
               points++;
     }
     for(int i = 0; i < 4; i++)
     {
          for(int a = 1; a < 5; a++)
          {
               int x = cards[i] + cards[a];
               if(x == 15)
                    points++;
               if(max - x == 15)
                    points++;
               if(cards[i] == cards[a])
                    points++;
          }
     }
     //After some testing I realized this program returns double the points so we use this and all is well
     points = points / 2;
     bool cheated = false;
     if((cards[0] == cards[1] && cards[0] == cards[2]) && (cards[0] == cards[3] && cards[4]))
          cheated = true;
     if(cheated)
          cout << "Cheaters never win.\nYou get 0 points";
     else
          cout << "Nice job you got " << points;
     return 0;
}

This program will output the resulting number of points for the given input. I believe I understood the problem correctly though feel free to correct me on any part of this program.

The program shortens the amount of combinations. It first checks the total of all of them. It then checks group of four by subtracting each value. It then simultaneously finds the groups of three and pairs by searching for a pair checking them and then subtracting the value of the pair. It checks for two cards with the same values at this time also by checking if the pairs have the same value.

If there are any more questions feel free to ask and I'll try my best to clear it up.
Last edited on
Topic archived. No new replies allowed.