using set and pairs solve container handling two values but manipulating one
//2 boxers with max. strength in league need 2 fight in a match. One will
max strength will win the match with remaining strength of difference
between two boxer strength. The boxers with 0 difference in strength will
get eliminated. The loser will be eliminated. find winner?
typedef pair pairs; //pair of boxer's strngth and index (tydef for
declairing pairs at default data type) pairs boxer[4]; set s;
set :: iterator it; //iterator to do operation of set elements pairs max,
min, v; //pairs to save min and second max strength
boxer[0].first= (22); //boxer 1's strength = 23
boxer[0].second = (1); //boxer 1's index =1
boxer[1].first= (20); //boxer 2's strength = 20
boxer[1].second = (2); //boxer 2's index = 2
boxer[2].first= (44); //boxer 3's strength
boxer[2].second = (3); //boxer 3's index
boxer[3].first= (84); //boxer 4's strength
boxer[3].second = (4); //boxer 4's index
//you can add more
int size = sizeof(boxer)/sizeof(boxer[0]); //getting size of boxers
for (int i =0; i<size; i++){
s.insert(boxer[i]); //inserting boxers' identity in set // set always
sort while insertinf
}
while (s.size()!=1){ //terminate when only one boxer left in league
min = *(--(--s.end())); //boxer with second max strength in set
max = *(--(s.end())); //boxer with max strngth in set
s.erase(--(--s.end()), s.end()); //erase last two enteries (which is max,
and second max)
max.first -= min.first;
if (max.first ==0){
continue;// escpae to next operation as boxers with same strength will
get eliminated
}
s.insert(max); //again insert winner of the match in the set
}
for (it = s.begin(); it!=s.end(); it++){
v = *it; //save set element in pair object
cout<<"Boxer with index " <<v.second<<" won the league with remaining
strength " << v.first << endl;
}
No comments:
Post a Comment