UVA 10935 - Throwing cards away I

 

Problem B: Throwing cards away I

Given is an ordered deck ofncards numbered 1 tonwith card 1 at the top and cardnat 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 the top of the deck to the bottom of the deck.

Your task is to find the sequence of discarded cards and the last, remaining card.

Each line of input (except the last) contains a numbern≤50. The last line contains 0 and this line should not be processed. For each number from the input produce two lines of output. The first line presents the sequence of discarded cards, the second line reports the last remaining card. No line will have leading or trailing spaces. See the sample for the expected format.

Sample input

7
19
10
6
0

Output for sample input

Discarded cards: 1, 3, 5, 7, 4, 2
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8
Remaining card: 4
Discarded cards: 1, 3, 5, 2, 6
Remaining card: 4

 

 

特殊情况:n=1时,

Discarded cards:<- No space here!!!
Remaining card: 1

 

Solution1:

 

//#define RUN
#ifdef RUN


#include<stdio.h>

const int MAXN = 50;
int queue[MAXN];


int main() {

#ifndef ONLINE_JUDGE
	freopen("10935.in", "r", stdin);
	freopen("10935.out", "w", stdout); 
#endif

  int n, front, rear;
  while(scanf("%d", &n)==1 && n!=0){
	  // 初始化队列
	  for(int i = 0; i < n; i++) 
		  queue[i] = i+1;
	  front = 0;	// 队首元素的位置
	  rear = n;		// 队尾元素的后一个位置

	  printf("Discarded cards:");
	  // 队列非空
	  while(front < rear-1) {
		  if(front == rear-2){
			  printf(" %d", queue[front]);
		  }
		  else{
			  printf(" %d,", queue[front]);	
		  }
		  // 输出并抛弃原队首元素,front指向新队首元素
		  front++;
		  queue[rear] = queue[front];	// 队首元素转移至队尾,同时更新队首和队尾元素
		  front++;
		  rear++;
	  }
	  printf("\n");
	  printf("Remaining card: %d\n", queue[front]);

	  //printf("\nFinal front: %d\n", front);
	  //printf("Final rear: %d\n", rear);
  }
  
  
  return 0;
}

#endif



Solution2:

 

#define RUN
#ifdef RUN

#include<cstdio>
#include<queue>

using namespace std;

queue<int> q;

int main() {

#ifndef ONLINE_JUDGE
	freopen("10935.in", "r", stdin);
	freopen("10935.out", "w", stdout); 
#endif


  int n, front, rear;

  while(scanf("%d", &n)==1 && n!=0){

	  // 初始化队列
	  for(int i = 0; i < n; i++) 
		  q.push(i+1);

	  printf("Discarded cards:");
	  while(q.size() > 1) {

		  if(q.size() == 2){
			  printf(" %d", q.front());	// 打印队首元素
		  }
		  else{
			  printf(" %d,", q.front());	// 打印队首元素
		  }
		  

		  q.pop();			// 抛弃队首元素
		  q.push(q.front());	// 把队首元素加入到队尾
		  q.pop();			// 抛弃队首元素
	  }
	  printf("\n");
	  printf("Remaining card: %d\n", q.front());
	  q.pop();
  }
  
  return 0;
}

#endif



2025年抖音面试:商业生态与安全质量运营技能要求及面试 在商业生态与安全岗的质量运营工作中,规则运营职责占据着极为重要的地位,它是保障内容风控质量的关键环节。 阅读详情

相关推荐

Windows 11下manage-bde命令实战:BitLocker锁了别慌,手把手教你用命令行找回数据

本文详细介绍了在Windows 11系统下使用manage-bde命令解锁BitLocker加密驱动器的实战方法。通过命令行诊断、恢复密钥获取、解锁流程演示及高级故障排除,帮助用户在系统锁定后快速找回数据,特别适合IT技术人员和遇到紧急情况的用户。

weixin_30902251的博客 329

uva 10935 throwing cards away <queue>

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 t...

weixin_34167819的博客 129

AI编程助手Cursor如何革新经济学研究:从计量模型到可视化全流程实战

在数据科学和人工智能技术蓬勃发展的今天,如何高效地将前沿AI工具融入专业领域的研究工作流,成为提升科研效率的关键。其核心原理在于通过自然语言交互,将研究者的专业问题转化为机器可执行的指令,从而实现人机协作的范式转移。这一技术的核心价值在于大幅降低非计算机背景研究者的编程门槛,将精力从繁琐的代码实现中解放出来,更聚焦于学科本身的思想与逻辑。尤其在经济学、金融学等高度依赖数据分析和计量建模的领域,AI编程助手能够快速处理数据清洗、模型估计、可视化及结果解释等任务。具体到应用场景,研究者可以利用结构化提示词工程,

weixin_42531579的博客 296

UVa 10935 Throwing cards away I(queue:队列)

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 awa...

H4ppyD0g的博客 376

手把手教你用Microsoft AEC Challenge数据集训练自己的回声消除模型(附完整代码)

本文详细介绍了如何使用Microsoft AEC Challenge数据集训练专业的回声消除模型,包括环境准备、数据清洗、模型架构设计、训练流程和实际部署注意事项。通过完整的代码示例和实战指南,帮助开发者快速掌握AEC技术,提升语音通信系统的回声消除效果。

weixin_27697385的博客 456

用Playwright MCP让AI帮你完成所有浏览器自动化

Playwright MCP(Model Context Protocol)是微软基于 Playwright 框架推出的 AI 浏览器自动化新协议。它通过结构化快照数据和自然语言指令结合大型语言模型(如 GPT),实现无需编码的网页交互。无论你是要测试登录流程、抓取价格数据,还是用 AI 远程控制浏览器,Playwright MCP 都能大幅降低门槛,让“说人话”也能驱动自动化脚本。本篇将从功能介绍、技术架构、使用方法到实际场景,带你全面理解这一自动化革命。

todoitbo的博客 3万+

UVa10935-Throwing cards away I-卡片游戏-队列的运用

题目链接:https://vjudge.net/problem/UVA-10935 桌上有n(n≤50)张牌,从第一张牌(即位于顶面的牌)开始,从上往下依次编号为1~ n。当至少还剩下两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠 牌的最后。输入每行包含一个n,输出每次扔掉的牌以及最后剩下的牌。 分析:直接上队列。参考 STL 中队列的使用 头文件:#inclu

话歪之地的博客 509

全功能tgbot/Telegram机器人多功能有后台版源码

trx兑换 闪兑 关键字监控 群管 usdt监控 余额查询 推广分享 等几乎常见的tg机器人功能都有。全功能tgbot/telegram机器人多功能有后台版源码-打包好的可以直接上手。

百创科技 2701

《算法竞赛入门经典(第2版)》习题5-3卡片游戏 (Throwing cards away I ,UVa 10935)

AC代码,先说算法: 1、输入,比如输入4,然后做个循环1~4,存到队列里,队列里现在是1234;如果输入1,直接按1的结果输出。 2、当队列的size()>1时, 1)先存队首元素1到vector里,vector现在是1 2)队首出队,queue里是234, 3)保存队首元素2,队首2出队,push2到队里,队现在是342 以此类推就行。 3、清空队列和vector,这个很重要。 3、输出...

Themess的博客 229

C3 模块与普通 Bottleneck 堆叠区别

维度普通 Bottleneck 堆叠C3 模块设计理念残差学习CSP + 特征分治信息路径单一路径双路径(主干 + bypass)融合方式相加(Add)拼接(Concat)+ 卷积计算效率较低高(通道减半)特征保留弱(多次非线性)强(直连保留原始信息)梯度传播单一主路径多样化、更稳定任务适配通用(分类为主)专为目标检测优化可扩展性有限极强(即插即用各种模块)实际性能基准线显著优于(尤其在小目标和效率方面)

浩瀚之水的专栏 107

066、AR 应用中虚拟物体漂移抖动?IMU 融合 + 光流追踪的视觉里程计优化方案

AR应用中虚拟物体漂移抖动优化方案 摘要:针对AR应用中虚拟物体漂移抖动问题,提出一种IMU融合与光流追踪的视觉里程计优化方案。关键点包括:1) 采用IMU预积分技术处理高频噪声,通过中值积分计算帧间相对运动约束;2) 使用金字塔LK光流追踪像素运动,通过中值偏差法剔除异常值;3) 构建10帧滑动窗口的紧耦合优化框架,将IMU预积分约束和光流重投影约束联合优化。实验表明,该方法有效解决了时间戳未对齐导致的"跳舞箭头"现象,在手机端实现稳定30fps的AR定位精度,漂移误差降低至厘米级。特别提醒:IMU与视觉

ai技术增长的博客 161

uva 10940 Throwing cards away II

我打印出了1 - 100     n = 1, ans = 1 n = 2 ans = 2 n = 3 ans = 2 n = 4 ans = 4 n = 5 ans = 2 n = 6 ans = 4 n = 7 ans = 6 n = 8 ans = 8 n = 9 ans = 2 n = 10 ans = 4 n = 11 ans = 6 n = 12 ans = 8

Kizuki 1786

华为OD新系统机试真题 2026.5.10 - 美观的灯笼

华为OD新系统机试真题【美观的灯笼】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【美观的灯笼】

qq_45776114的博客 640

UVA - 10935 Throwing cards away I 【queue】

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 performed as long as there are at least two cards in the d...

一定要站在自己热爱的生活里闪闪发光 3万+

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

UVa 10935 - Throwing cards away I

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=836&page=show_problem&problem=1876 思路:按照要求更改,打印数组即可,注意n=1依旧需要"Discarded cards:"。 #include #include #include

migo_dab的博客 260

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

UVa 10935 Throwing cards away I

题目大意: 题目链接 注释代码: 无注释代码: 单词解释: trailing:

源代码 621

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Throwing cards away I UVA - 10935

题目链接:https://cn.vjudge.net/problem/UVA-10935 为什么出这么奇怪的测试点。。。。 代码: #include &amp;lt;bits/stdc++.h&amp;gt; using namespace std; int main() { freopen(&quot;i.txt&quot;, &quot;r&quot;, stdin); int n; while(cin &a

qq_43185391的博客 150

UVa 10935 Throwing cards away I 卡片游戏 题解

英文 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 performed as long as there are at least two cards in ...

ZJ_MRZ的博客 542

Uva 10935 Throwing cards away I

Throwing cards away I Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description Given is an ordered deck of n cards numbered 1to n with

菜鸟起飞的平台 375

Throwing cards away I(UVA 10935)

似乎也没必要专门写一个函数。倒是输出格式一直没搞好。

Fool256353的博客 415

UVA10935 Throwing cards away I【模拟+queue+循环队列】

Given is an ordered deck of n cards numbered 1to 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:        Thr...

海岛Blog 2532

UVA 10935 Throwing cards away I

题目链接:https://vjudge.net/problem/UVA-10935 题意:桌子上有n(n Sample Input 7 19 10 6 0 Sample Output Discarded cards: 1, 3, 5, 7, 4, 2 Remaining card: 6 Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 1

CS33sun的博客 372

UVA10935 Throwing cards away I 卡片游戏 解题报告

桌上有n(n≤50)张牌,从第一张牌(即位于顶面的牌)开始,从上往下依次编号为1~n。当至少还剩下两张牌时进行以下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后。输入每行包含一个n,输出每次扔掉的牌以及最后剩下的牌。题目怎么说你就怎么做,非常直球,很显然是维护一个队列。

qq_45554473的博客 493
上一篇: 新手任务
下一篇: UVA 10935 - Throwing cards away I
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值