重新排列数组是最佳的相比,另一个数组 [英] Rearrange an array to be optimal in comparison to another array

查看:131
本文介绍了重新排列数组是最佳的相比,另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经达到什么是我的程序中最简单的一部分,但我有点失落,我如何能做到这一点的一部分,并保持高效的同时。

I've reached what is to be the most simple part of my program but I am a bit lost, on how I can do this part and keep it efficient at the same time.

attacker= {3,10,14,15,17,18};
defender = {1,5,7,9,12,18};

所以两者都是相同长度的两个阵列,也进行排序。
为了把这一背景下在两个数组的数字再由用户输入的present卷。

So both of these are two arrays of same length, and also sorted. To put this in context the numbers in both arrays represent rolls entered in by the user.

除这种风险的后卫被允许重新安排自己的阵列,这样他就可以赢得更多的战斗。这样他就可以对5 3,如果他想。

Except in this risk the defender is allowed to rearrange his array so that he can win more battles. So he can pair 5 with 3, if he wanted.

我失去了对如何做到这一点而不令人难以置信的低效率或容易很多错误。

I'm lost on how to do this without being incredibly inefficient or prone to much error.

推荐答案

我建议这个算法:

合并两个数组并标记这是后卫(d)或攻击(一)内容:

Merge the two arrays and mark the elements which are defender (d) or attacker (a):

d a d d d  a  d  a  a  a  d  a
1 3 5 7 9 10 12 14 15 17 18 18

(对于两个相等的元素,第一名后卫)。

(For two equal elements, place the defender first).

再取对,其中 A D 模式;这些将是战斗后卫胜:

Then take the pairs where a d pattern is; these would be the battles defender wins:

d a d d d  a  d  a  a  a  d  a
1 3 5 7 9 10 12 14 15 17 18 18
  ^ ^      ^  ^        ^  ^

和刚安排中奖号码第一,而亏钱的最后:

And just arrange the winning numbers first, and the losing ones last:

defender = {5, 12, 18, 1, 7, 9}

这篇关于重新排列数组是最佳的相比,另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆