This is an easy problem from uva judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3391
Adhoc, Complete Search approach.
How I did it
- Read the number of balls and the remaining balls.
- Read all the remaining balls and insert them into a vector.
- Generate all the posible cambination with two elements.(Two nested for)
- Insert in a set the absolute value of the subtraction of the two possible elements.
- If the “set size” is equal to “number of balls +1” print Y, otherwise print N.
Hints
- We can use the same ball to generete a number. ( 5, 5 = 0).
Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <set> using namespace std; int main() { int number, remain, val, tmp; while(scanf("%d %d", &number, &remain)) { vector<int> vec; set<int> mySet; if(!number && !remain) break; for(int x=0; x<remain; x++) { scanf("%d", &val); vec.push_back(val); } for(int a=0; a<vec.size(); a++) for(int b=0; b<vec.size(); b++) { tmp = abs(vec[a] - vec[b]); mySet.insert(tmp); } if(mySet.size() == number+1) printf("Y\n"); else printf("N\n"); } return 0; }