`
hellobin
  • 浏览: 65553 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

UVA 10935 - Throwing cards away I

    博客分类:
  • UVA
 
阅读更多

 

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



分享到:
评论

相关推荐

    Angular-and-meteor-are-throwing-a-party

    标题“Angular-and-meteor-are-throwing-a-party”暗示了这个项目是关于结合AngularJS和Meteor框架来构建一个交互式、实时的Web应用程序。在这个场景中,AngularJS作为一个强大的前端MVC(Model-View-Controller)...

    throwing-function, 已经检查的异常使用Java 8 功能接口 适配器.zip

    throwing-function, 已经检查的异常使用Java 8 功能接口 适配器 函数已经检查的异常使用了 Java 8 功能接口 适配器 提供了用于解决 Java 8检查的异常的快捷方式。你可以定义抛出checked异常的函数:ThrowingFunction...

    ImmutableSortedMapFauxverideShim.rar_The Exception

    "Overrides" the ImmutableMap static methods that lack ImmutableSortedMap equivalents with deprecated, exception-throwing versions.

    dart throwing采样

    关于dart throwing进行采样的思想,我只是按照自己的理解做的。可能理解有偏差,或者算法有错误,还请大神指教。 采样:我的理解是,对散点图进行采样,就是从一堆点中选出一部分点,使得这些选出的点满足特定的要求...

    throwing-function:启用检查的异常的Java 8+功能接口+适配器

    启用了检查异常的Java 8+功能接口和适配器基本原理由于没有throws ...子句,因此标准的java.util.function Function Interfaces不被检查为异常友好的,这导致通过添加try-catch样板来处理它们的繁琐而冗长的必要性。...

    struts_2.3.12GA_API文档(chm版本)

    Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return the specified result, such as Action.SUCCESS, Action.INPUT, etc. void ...

    应用Spring AOP(五)-------XML配置方式

    &lt;aop:after-throwing method="afterThrowingAdvice" throwing="ex" pointcut-ref="myPointcut"/&gt; ``` `throwing`属性指定了异常对象传递到通知方法的参数名。 5. 配置环绕通知(Around Advice) 环绕通知最为强大,...

    Throwing Animations v1.1.unitypackage

    Throwing Animations v1.1

    throwing-lambdas:抛出检查异常以用于函数式接口的methodinterfaceslambdas 的包装器

    compile( group : " com.github.fge " , name : " throwing-lambdas " , version : " 0.5.0 " ); 示例用法 例如,假设您想对文件树中所有常规文件的大小求和。 JDK 提供来遍历文件树; 因此,您可以过滤该流以仅列...

    dart Throwing on surfaces

    ### Dart Throwing on Surfaces:关键技术点解析 #### 摘要 本论文提出了用于在三维表面上生成最大泊松圆盘点集的飞镖投掷算法。这些算法通过有效地排除已被现有飞镖覆盖的区域来优化飞镖投掷过程。在三角网格的...

    Spring_AOP开发

    }/**//异常通知@AfterThrowing(pointcut="interceptorMethod()", throwing="ex")public void doThrowing(Exception ex){System.out.println("This is Throwing Advice\t"+ex.getMessage());} *//* 定义环绕通知,...

    如何通过XML方式配置AOP过程解析

    使用aop:before、aop:after-returning、aop:after-throwing和aop:after标签配置通知类型,例如: ``` &lt;!-- 配置通用切入点表达式 --&gt; (* com.atguigu.account.service.impl.*.*(..))" /&gt; &lt;!-- 配置前置通知:...

    Apache-Tomcat-8.5.5(Linux )

    解决方法见:http://stackoverflow.com/questions/26893297/tomcat-8-throwing-org-apache-catalina-webresources-cache-getresource-unable-to 尊重他人成果, 转载于http://www.jmatrix.org/day/1186.html

    unit5I’mwatchingTV知识点.pdf

    - 现在分词练习:catch-catching, think-thinking, drink-drinking, learn-learning, clean-cleaning, throw-throwing, draw-drawing, read-reading, wait-waiting, tell-telling, eat-eating, carry-carrying, ...

    基于框架的Web开发-基于XML配置的AOP.doc

    同样,使用&lt;aop:after-throwing&gt;标签,配置demandRefund方法在"performance"切点抛出异常时执行。 9. **环绕通知(Around Advice)**: 如果需要在方法调用前后都进行操作,并且可以控制方法是否执行,就需要使用...

    新陕旅版六年级下册小学英语 Unit 4 第1课时同步练习(一课一练).docx

    3. 第三题询问是否擅长跳远,否定回答应用"No, I'm not.",所以正确答案是A. I’m not。 4. 第四题考查现在分词作定语,修饰race,应为running race,所以正确答案是C. running。 5. 第五题表达我能试试跳高,high ...

    Fast knife throwing game-crx插件

    【Fast knife throwing game-crx插件】是一款专为喜爱挑战速度和精准度的玩家设计的扩展程序,其核心是模拟真实刀投掷游戏的体验。该插件采用英文界面,旨在带给用户一种紧张刺激的游戏感受,同时提升反应速度和判断...

Global site tag (gtag.js) - Google Analytics