`
hellobin
  • 浏览: 62699 次
  • 性别: 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

    我们在名单上无需前端更改即可从实施社交应用。

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

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

    jsp编程技术复习题

    10.IoC和DI概念理解? IOC(Inverse of Control控制反转):从字面上理解就是控制反转了,将对在自身对象中的一个内置对象的控制反转,反转后...答:Before、After-returning、After-throwing、Around、Introduction;

    ImmutableSortedMapFauxverideShim.rar_The Exception

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

    dart throwing采样

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

    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源码:从原理到实现,全方位掌握Java AOP编程精髓

    Spring AOP支持不同类型的通知,如前置通知(Before)、后置通知(After)、返回后通知(After-returning)、抛出异常后通知(After-throwing)和环绕通知(Around)。这种机制使得开发者可以在运行时动态地应用这些...

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

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

    论文研究-改进纯追踪模型的农业机械地头转向控制方法.pdf

    提出了一种改进的叶脉建模...作为整体算法中的一步,采用一种新的dart-throwing算法得到均匀分布的营养点集,有效地提高了整个算法的绘制速度。实验结果表明,改进后的方法能够以更快的速度生成更加逼真的叶脉模型。

    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

    Spring AOP配置源码

    &lt;aop:after-throwing method="exception" pointcut-ref="pointCut"/&gt; aop异常通知 以上结合起来意思就是在调用com.spring.service包或子包下的所有方法之前或之后或抛出异常时依次调用id为logIntercepter的类中的...

    dart Throwing on surfaces

    这篇论文是EGSR上面的,很经典,对于研究计算机图形学的朋友们很有用哦。我正在搞。

    Simple Throwing Ball Game using JavaScript

    JavaScript

    Fast knife throwing game-crx插件

    使用快速刀投掷游戏拆分木​​头和切苹果 快速刀-在原木上扔刀将其劈开,难度随着等级的提高而增加! 支持语言:English

    PowerShell-7.0.0-preview.4-win-x64.msi

    v7.0.0-preview.4 - 09/19/2019 Engine Updates and Fixes Add support to ActionPreference.Break to...Add throwing an error in Add-Type if a type with the same name already exists (#9609) (Thanks @iSazonov!)

    throwing-darts

    Dart 您最近刚刚受雇计算Dart盘游戏的分数 评分规格 0 分 - 半径大于 10 5 分 - 半径介于 5 和 10 之间,包括 10 分 - 半径小于 5 如果所有半径都小于 5,则奖励 100 点奖励积分! 空数组应该返回 0。...

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

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

Global site tag (gtag.js) - Google Analytics