Tuesday, June 6, 2017

uva problem solution 10935 - Throwing cards away I

problem link:


Discuss: in this problem you have to print first element from queue and pop second element and then push it again in queue. and print the last element.

if you know queue then it's simple problem for you.clear queue using while loop


try yourself before see the code


 #include<bits/stdc++.h>
using namespace std;
int main()
{

   int i,j,cnt,flag=0,n,a;
   queue<int>que;
   while(cin >>n)
   {
   if(n==0)
   break;
      while(!que.empty())
      que.pop();

   for(i=1;i<=n;i++)
   {
      que.push(i);
   }
   cout <<"Discarded cards:";
   for(i=1;i<n;i++)
   {
     cout <<' '<<que.front();
     if(i!=n-1)
     cout<<',';
     que.pop();
     a=que.front();
    que.pop();
     que.push(a);
   }
   cout <<endl;
   cout <<"Remaining card: "<<que.front()<<endl;


}
return 0;
}



No comments:

Post a Comment