- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要找到彩票组合的等级/索引并能够逆向过程(根据等级找到彩票组合)。
考虑一个彩票游戏,5 个球从 1 到 45,1 个强力球从 1 到 20。不允许重复,顺序无关紧要。组合数为:
(45 * 44 * 43 * 42 * 41 / 5!) * 20 = 24,435,180
第一个组合(索引 0)是:
1, 2, 3, 4, 5, 1
最后的组合(索引 24,435,179)是:
41, 42, 43, 44, 45, 20
如何在不详尽枚举所有组合的情况下将组合转换为其索引,反之亦然?
我遇到了 this MSDN 文章,其中展示了如何获取给定索引的组合。但是,我不知道如何从组合中获取索引。我试过:
Choose(c1,k) + Choose(c2,k-1) + Choose(c3,k-2) + Choose(c4,k-3) ...
其中ci是有序组合集合中位置i的数,k是集合的大小。由于组合的索引取决于元素的范围,因此这是行不通的。此外,我不确定它是否适用于集合中不同大小的元素(例如主池的范围是 1-45,强力球的范围是 1-20)
最佳答案
我想通了。我创建了一个新的组合类,基于 MSDN example ,它可以将组合转换为索引,反之亦然。为每个号码池检索单独的索引。然后组合所有索引以表示具有不同大小的元素的组合。
还创建了一个 Pool 类来表示池的设置(元素范围、大小等)。池类:
public class Pool {
public int From { get; set; }
public int To { get; set; }
public int Size { get; set; }
public int Numbers { get { return (To - From + 1); } }
public Pool(int From, int To, int Size) {
this.From = From;
this.To = To;
this.Size = Size ;
}
}
组合类:
class Combination {
public Pool[] Pools { get; set; }
public long[][] Data { get; set; } //First index represents pool index, second represents the numbers
public Combination(Pool[] Pools, long[][] Data) {
this.Pools = Pools;
this.Data = Data;
if (Data.GetLength(0) != Pools.Length) {
throw (new ArgumentException("Invalid data length"));
}
for (int i = 0; i < Data.GetLength(0); i++) {
if (Data[i].Length != Pools[i].Size) {
throw (new ArgumentException("Invalid data length"));
}
}
}
public static Combination FromIndex(long Index, Pool[] Pools) {
long[][] elements = new long[Pools.Length][];
long[] c = new long[Pools.Length - 1];
long cumulative = 1;
for (int i = 0; i < Pools.Length - 1; i++) {
c[i] = Combination.Choose(Pools[i].Numbers, Pools[i].Size);
checked {
cumulative *= c[i];
}
}
for (int i = Pools.Length - 1; i >= 1; i--) {
long ind = Index / cumulative;
Index -= ind * cumulative;
cumulative /= c[i - 1];
elements[i] = Combination.FromIndex(ind, Pools[i]);
}
elements[0] = Combination.FromIndex(Index, Pools[0]);
return (new Combination(Pools, elements));
}
public static long[] FromIndex(long Index, Pool Pool) {
long[] ans = new long[Pool.Size];
long a = (long)Pool.Numbers;
long b = (long)Pool.Size;
long x = GetDual((long)Pool.Numbers, (long)Pool.Size, Index);
for (int k = 0; k < Pool.Size; k++) {
ans[k] = LargestV(a, b, x);
x -= Choose(ans[k], b);
a = ans[k];
b--;
}
for (int k = 0; k < Pool.Size; k++) {
ans[k] = ((long)Pool.Numbers - 1) - ans[k];
}
//Transform to relative
for (int i = 0; i < ans.Length; i++) {
ans[i] += Pool.From;
}
return (ans);
}
private static long GetDual(long To, long Size, long m) {
return (Choose(To, Size) - 1) - m;
}
public static long Choose(long To, long Size) {
if (To < 0 || Size < 0)
throw new Exception("Invalid negative parameter in Choose()");
if (To < Size)
return 0; // special case
if (To == Size)
return 1;
long delta, iMax;
if (Size < To - Size) {
delta = To - Size;
iMax = Size;
} else {
delta = Size;
iMax = To - Size;
}
long ans = delta + 1;
for (long i = 2; i <= iMax; ++i) {
checked {
ans = (ans * (delta + i)) / i;
}
}
return ans;
}
private static long LargestV(long a, long b, long x) {
long v = a - 1;
while (Choose(v, b) > x)
--v;
return v;
}
public long ToIndex() {
long Index = 0;
long cumulative = 1;
for (int i = 0; i < Pools.Length; i++) {
checked {
Index += ToIndex(i) * cumulative;
cumulative *= Combination.Choose(Pools[i].Numbers, Pools[i].Size);
}
}
return (Index);
}
public long ToIndex(int PoolIndex) {
long ind = 0;
for (int i = 0; i < Pools[PoolIndex].Size; i++) {
long d = (Pools[PoolIndex].Numbers - 1) - (Data[PoolIndex][i] - Pools[PoolIndex].From);
ind += Choose(d, Pools[PoolIndex].Size - i);
}
ind = GetDual(Pools[PoolIndex].Numbers, Pools[PoolIndex].Size, ind);
return (ind);
}
public override string ToString() {
string s = "{ ";
for (int i = 0; i < Data.Length; ++i) {
for (int k = 0; k < Data[i].Length; k++) {
s += Data[i][k] + " ";
}
if (i != Data.Length - 1) {
s += "| ";
}
}
s += "}";
return s;
}
}
要查看实际效果:
//Create pools
Pool[] pools = new Pool[2];
pools[0] = new Pool(1, 45, 5);
pools[1] = new Pool(1, 20, 1);
//Create a combination
long[][] data = new long[][] { new long[] { 41, 42, 43, 44, 45 }, new long[] { 20 } };
Combination combination = new Combination(pools, data);
//Get index from combination:
long index = combination.ToIndex();
Console.WriteLine("Index: " + index);
//Get combination from index:
Combination combFromIndex = Combination.FromIndex(index, pools);
Console.WriteLine("Combination: " + combFromIndex);
输出:
Index: 24435179
Combination: { 41 42 43 44 45 | 20 }
关于combinatorics - 查找彩票组合的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219784/
我正在查看下面的示例代码, r element frequency and column name 并且想知道除了r中的排名和频率之外,是否有任何方法可以显示每列中每个元素的索引。因此,例如,所需的输
我有下表按 Id、Year DESC 排序 ID 年份有效 1 2011 1 1 2010 1 1 2009 0 1 2002 1 4 2013 1 4 2012 1 4 2011 1 等等。 我想要
鉴于此数据 Type Time Outcome Wanted Result 1 8:00 1 1 1 9:00 1 1 1 10:00 1 1 0
我正在寻找一种对两个句子进行排名/匹配的方法。 例如,取以下2个例句。 这是一个简短的句子。 这是一个包含很多单词的长句子。 我的新句子是这是一个句子。 我想将我的新句子与现有句子进行比较。我的新句子
我是 scikit 新手,我正在按照此处的示例 http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_dat
我有一张 table : r_user | r_points | -------------------- user1 | 12 | user2 | 124 | use
我需要获得顶级玩家、给定玩家的排名以及与该给定玩家排名相关的少数玩家。 为了更清楚地解释,下表是我想要的,它显示了得分最高的 3 名玩家、给定玩家的排名 (id=11) 以及得分略高于和低于该给定玩家
我正在尝试创建一个排名(排名)查询。 我使用表users、schedule 和picks来计算排名。然而,这样做的缺陷是,如果用户未提交任何选择,则该用户将不会出现在排名中。 下面的查询返回所有已提交
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: High score system from my iphone game 我的查询是: $sql = "SELEC
我有一个玩家表和MatchUps表。 MatchUps 表具有一个 winner_id 列和一个 loser_id 列。我可以根据一次查询的胜率获得排名/排名吗?如果我能为每个玩家返回这样的东西,那就
我正在尝试创建一个排名/阶梯系统,其中排名最高的氏族 (1) 应位于顶部,排名为 2 的氏族应位于顶部,依此类推。但我无法让它工作......我尝试了不同的方法: PHP: $sql = mysql_
我需要一份游戏中排名最高的玩家列表。排名是即时计算的,数据取自两个表。我设法以正确的方式对它们进行排序,但是@rank:=0 -> @rank:=@rank+1 技巧,其中一个名为 rank 的附加字
下周末我们将进行一场包含 3 项资格赛(半决赛和决赛)的比赛。只有最好的 15 名参赛者才能参加半决赛。只有最好的 6 人才能参加总决赛。 在资格考试中,每项资格考试的分数从 0 到 100 不等 我
我正在尝试找出对我的产品进行加权的最佳方式,以及它们应该以何种顺序出现在主页等地方。 我想处理四个指标并将其转化为排名: 购买产品 产品有多久了(以天为单位) 产品被保存了多少次 产品被浏览了多少次
使用 @N=@N + 1 的经典技巧来获取某些有序列上的项目排名。现在在订购之前,我需要通过将它与其他表内部连接来从基表中过滤掉一些值。所以查询看起来像这样 -: SET @N=0; SELECT
我需要一些帮助来处理在 MySQL 中排名时的关系。例如: 播放器 |积分 玛丽:90 鲍勃:90 吉姆:65 凯文:12 Bob 和 Mary 应该都排名第一。吉姆应该是#3。凯文应该是#4。 My
我正在寻找一种算法,该算法可以为我提供具有特定强度的下一个排列。长度为 n 的排列由元素 (1,2,3,...n) 定义 排列的强度是多少? 长度为 10 的排列的强度定义为 |a1-a2|+|a2-
我正在编写一个具有信誉组件的电子商务引擎。我希望用户能够对项目进行评论和评分,并能够对评论进行评分。 用于根据“最佳”评论对项目进行排序的最佳算法是什么?它必须根据给出最佳评论的人获得的质量评论数量进
我有一个按游戏结果填满游戏的数据库表,想知道我是否可以计算以下内容: GP(玩过的游戏) 获胜 失败 积分(每胜2分,每负1分) 这是我的表结构: CREATE TABLE `results` (
我有一个 users 表,其中有一列名为 money_sent。我想按 money_sent 降序排列此表,然后找出特定用户的“排名”。 例如,只有 111 人比用户 12392 花费更多的钱,因此他
我是一名优秀的程序员,十分优秀!