fork(1) download
  1. #include <iostream>
  2.  
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. int getWrongMoves(int n, int k)
  7. {
  8. int result = 1;
  9. for (int i = 1; i < n - k; ++i)
  10. {
  11. result = 2 * result + i;
  12. }
  13. return result;
  14. }
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. int n = 5;
  19. int k = 2;
  20.  
  21. if (argc > 2)
  22. {
  23. n = atoi(argv[1]);
  24. k = atoi(argv[2])+1;
  25. }
  26. if (n < 1 || n > 100)
  27. {
  28. std::cout << "Usage: zenik <N>, <K>\n"
  29. " where 1 <= N, K <= 100" << std::endl;
  30. return 0;
  31. }
  32.  
  33. double allMoves = std::pow(2, n-1);
  34. double wrongMoves = 0;
  35.  
  36. if (k == 1)
  37. {
  38. wrongMoves = allMoves - 1;
  39. }
  40. else if (k >= n)
  41. {
  42. wrongMoves = 0;
  43. }
  44. else
  45. {
  46. wrongMoves = getWrongMoves(n, k);
  47. }
  48.  
  49. std::cout << "all moves: " << allMoves << std::endl;
  50. std::cout << "wrong moves: " << wrongMoves << std::endl;
  51. std::cout << "corect moves: " << allMoves - wrongMoves << std::endl;
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 4340KB
stdin
Standard input is empty
stdout
all moves: 16
wrong moves: 8
corect moves: 8