Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. /*
  2. Two players, S and T, are playing a game where they make alternate moves. S plays first.
  3. In this game, they start with an integer N. In each move, a player removes one digit from the
  4. integer and passes the resulting number to the other player. The game continues in this fashion until
  5. a player finds he/she has no digit to remove when that player is declared as the loser.
  6. With this restriction, its obvious that if the number of digits in N is odd then S wins otherwise T
  7. wins. To make the game more interesting, we apply one additional constraint. A player can remove a
  8. particular digit if the sum of digits of the resulting number is a multiple of 3 or there are no digits left.
  9. Suppose N = 1234. S has 4 possible moves. That is, he can remove 1, 2, 3, or 4. Of these, two of
  10. them are valid moves.
  11. • Removal of 4 results in 123 and the sum of digits = 1 + 2 + 3 = 6; 6 is a multiple of 3.
  12. • Removal of 1 results in 234 and the sum of digits = 2 + 3 + 4 = 9; 9 is a multiple of 3.
  13. The other two moves are invalid.
  14. If both players play perfectly, who wins?
  15. */
  16.  
  17. #include<iostream>
  18. #include<algorithm>
  19. #include<string>
  20. #include<vector>
  21.  
  22. using namespace std;
  23.  
  24. bool prob(string &str1)
  25. {
  26. string str2 = str1;
  27. string str3 = str2;
  28. if (str1 == "") return false;
  29. for(int i=0; i<str2.size(); i++)
  30. {
  31. int counter = 0;
  32. str2.erase(i, 1);
  33. for (int j = 0; j < str2.size(); j++)
  34. counter = counter + str2[j] - '0';
  35. if (counter % 3 == 0) {
  36. str1 = str2; return true;
  37. }
  38. else str2 = str1;
  39. }
  40. return false;
  41. }
  42.  
  43. int main()
  44. {
  45. int testcases; cin >> testcases;
  46. for (int i = 0; i < testcases; i++)
  47. {
  48. cout << "Case " << i+1 << ":";
  49. int arr[2] = { 0,1 };
  50. string str; cin >> str;
  51. while (prob(str))
  52. {
  53. arr[0] = 1 - arr[0]; arr[1] = 1 - arr[1];
  54. }
  55. if (arr[0] == 1)
  56. cout << "S\n";
  57. else cout << "T\n";
  58. }
  59.  
  60.  
  61. return 0;
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement