Advertisement
Mr_kindle

stickpuzzle.c

Dec 6th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | Source Code | 0 0
  1.  
  2. /*
  3.     Name: stickpuzzle.c
  4.     Copyright:
  5.     Author: Mr.Kindle
  6.     Date: 06-12-22 23:35
  7.     Description: Given 21 Matchsticks and 2 users, A and B (computer and user respectively).
  8.     Users and computer both can pick matchsticks not more than four at a time. The one who is forced to
  9.     pick the last matchstick loses.Make an algorithm  so that computer always win.
  10.      Given that user pickup the stick first.
  11.  
  12. */
  13.  
  14.  
  15. #include<stdio.h>
  16.  
  17.    
  18.  
  19. int main()
  20. {
  21.     int  stick,u,c;
  22.  
  23.     stick = 21;
  24.    
  25.     while(stick)
  26.     {
  27.        printf("number of stick: %d\n",stick);
  28.         if(stick == 1)
  29.            {    printf("\n*****************************\n\n");
  30.                 printf("\nYOU HAVE TO PICK UP THE LAST STICK\n");
  31.                 printf("YOU LOOSE BETTER LUCK NEXT TIME\n");
  32.                 break;
  33.             }
  34.        printf("You pickup stick (MAXIMUM 4): ");
  35.        scanf("%d",&u);
  36.        if(u > 4)
  37.             {
  38.                 printf("\nPlease pickup less than 5 stick only: \n\n\n");
  39.                 continue;
  40.             }
  41.        stick = stick-u;
  42.     /*Now computer will pickup the stick*/
  43.        c = 5-u;
  44.        printf("computer picked up: %d\n",c);
  45.        stick = stick - c;
  46. }
  47. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement