#UVa:10935-Throwing cards away I

灆洢 2012-07-22 20:31:46

利用Queue去照著題目做,即可得解。

C++(0.008)

/*******************************************************/
/* UVa 10935 Throwing cards away I                     */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2012/07/22                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

int main(){
  int n;
  queue<int> cards;
  bool comma;
  while( scanf( "%d", &n ) != EOF && n != 0 ){
    for( int i = 1 ; i <= n ; i++ )
      cards.push(i);
    printf( "Discarded cards:" );
    comma = false;
    while( cards.size() != 1 ){
      if( comma ) printf( "," );
      comma = true;
      printf( " " );
      printf( "%d", cards.front() );
      cards.pop();
      cards.push(cards.front());
      cards.pop();
    }
    printf( "\n" );
    printf( "Remaining card: %d\n", cards.front() );
    cards.pop();
  }
  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料