UVa 10935 Throwing cards away I【队列】

题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌。

模拟队列的操作-------

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<queue>
 7 using namespace std;
 8
 9 queue<int>q;
10
11 int main()
12 {
13     int i,j,n,ans,a,b;
14     while(scanf("%d",&n)!=EOF&&n)
15     {
16         while(!q.empty()) q.pop();
17         for(i=1;i<=n;i++)
18         q.push(i);
19         printf("Discarded cards:");
20         while(q.size()!=1)
21         {
22             b=q.front();q.pop();
23             if(q.size()!=1) printf(" %d,",b);
24             else printf(" %d",b);
25             a=q.front();q.pop();
26             q.push(a);
27         }
28         printf("\n");
29         printf("Remaining card: %d\n",q.front());
30     }
31 }

时间: 2024-10-16 09:54:24

UVa 10935 Throwing cards away I【队列】的相关文章

UVa 10935 - Throwing cards away I

模拟队列操作.  注意当n == 1时第一行输出末尾没有空格.PE一次~~~ 代码  : import java.util.*; public class Main10935 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Queue<Integer> q = new LinkedList<Integer>(); while(true) { int n = scan.

UVA 10940 Throwing cards away II

题意略: 先暴力打表发现规律 N=1 ans=1N=2 ans=2N=3 ans=2N=4 ans=4N=5 ans=2N=6 ans=4N=7 ans=6N=8 ans=8N=9 ans=2N=10 ans=4N=11 ans=6N=12 ans=8N=13 ans=10N=14 ans=12N=15 ans=14N=16 ans=16N=17 ans=2N=18 ans=4N=19 ans=6N=20 ans=8N=21 ans=10N=22 ans=12N=23 ans=14N=24 an

UVA 10935 约瑟夫环

Throwing cards away I Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is per

Throwing cards away I

Throwing cards away I   Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck:  Throw away the top card and move t

紫书第五章训练3 D - Throwing cards away I

D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move

Throwing cards away I uva1594

 Throwing cards away I Given is an ordered deck of  n  cards numbered 1 to  n  with card 1 at the top and card  n  at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and

UVa---------10935(Throwing cards away I)

题目: Problem B: Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top car

UVA10940 Throwing cards away II【数学规律+约瑟夫环】

Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move the card that is now on th

UVa 10935 (水题) Throwing cards away I

直接用STL里的queue模拟即可. 1 #include <cstdio> 2 #include <queue> 3 using namespace std; 4 5 const int maxn = 60; 6 int discarded[maxn], cnt; 7 8 int main() 9 { 10 int n; 11 while(scanf("%d", &n) == 1 && n) 12 { 13 cnt = 0; 14 qu