Last active
August 29, 2015 14:20
-
-
Save rogerioagjr/500bd6296bda8e4bf733 to your computer and use it in GitHub Desktop.
Cards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#define MAXT 1000100 // defino o tamanho limite da fila como 1000100 | |
int fila[MAXT], n, ini=1, fim; // declaro as variáveis que vou usar | |
// funções da fila | |
int front(){ return fila[ini]; } | |
int back(){ return fila[fim]; } | |
int size(){ return fim-ini+1; } | |
void push(int x){ fila[++fim]=x; } | |
void pop(){ if(size()>0) ini++; } | |
void clear(){ ini=1; fim=0;} | |
int main(){ | |
while(scanf("%d", &n) && n!=0){ // enquanto a entrada não for zero | |
clear(); // limpe a fila | |
for(int i=1; i<=n; i++) push(i); // coloque os inteiros de 1 a n nela | |
printf("Discarded cards:"); // imprima "Discarded cards:" | |
while(size()>=2){ // enquanto houver mais de uma carta na fila | |
printf(" %d", front()); // imprima a primeira carta | |
pop(); // tire ela da fila | |
push(front()); // insira nova primeira carta no fim da fila | |
pop(); // e a retire do começo novamente | |
if(size()>=2) printf(","); // se ainda houver mais de um elemento na fila, imprima a vírgula | |
} | |
printf("\nRemaining card: %d\n", front()); // imprima a carta restante | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment