mardi 28 juin 2016

c program- NIMBLE GAME

Two people are playing Nimble! The rules of the game are:

The game is played on a line of squares, indexed from 0 to N-1 . Each square contains coins. The players move in alternating turns. During each move, the current player must remove exactly one coin from I square and move it to J square if and only if 0<=j < i . The game ends when all coins are in 0 square and nobody can make a move. The first player to have no available move loses the game. Given the value of and the number of coins in each square, determine whether the person who wins the game is the the first or second person to move. Assume both players move optimally

Input Format

The first line contains an integer,T , denoting the number of test cases. Each of the subsequent 2T lines defines a test case. Each test case is described over the following two lines:

An integer,N , denoting the number of squares. space-separated integers,C0,C1,C2..... , where each describes the number of coins at square .

For each test case, print the name of the winner on a new line (i.e., either FIRST or SECOND )

Sample Input

2

5

0 2 3 0 6

4

0 0 0 0

Sample Output

First

Second the program is as follows :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include<time.h>

int main() {
         int t=0,count=0, n, a[100],i,j,sum=0,moves=0;
    scanf( "%d",&t);
  do {        
                  count++;
               scanf("%d",&n);
                for(j=0;j<n;j++)
               { scanf("%d",&a[j]);
                  sum += a[j];
               }

      while( 1 )

         { 
             srand((unsigned)time(NULL));
             i= rand() % n ;
               j= rand() % (n-1);     
          if (i!=j && j<i && a[i]>0)

           { a[i]--;
             a[j]++;
             moves++;
           }
          if (a[0]==sum)
             break;
        }
      if(moves%2!=0)
          printf("nFirst");
      else
          printf("nSecond");

     }while(count<t);   


    return 0;
}

its gets executed bt wid error : TIME LIMIT EXCEEDED... What should i do about it? can someone suggest me ways to improvise the code or why time limit exceeds? how it make it more time efficient?

Aucun commentaire:

Enregistrer un commentaire