将组合存储为唯一总和的简单数字模式
这是一个数学问题,但我确信这一定会出现在某些编程场景中,至少我希望如此,而且我想知道是否有针对这种情况的名称:
假设我有一个系列中的 7 个项目。为了这个例子,我们使用一周中的几天。我希望用户提交他们计划在下周来到一周中的哪几天。他们会看到一系列标准的复选框,一个代表一周中的每一天。
我想将他们在一个数据库字段中选择的日期存储为单个整数。
显然,我可以为每一天指定一个数字,1 - 7(如果用户未选中所有选项,则保留 0)。但是,如果一个用户选择星期一和星期二 (1 + 2),而另一个用户选择星期三 (3),我就会遇到问题。
我还可以给一周中的每一天一些奇怪的独特之处,这样任何数字组合都不可能与任何其他组合相同。
我的希望是,不是为第二种情况编造这样一个级数,而是已经存在一些已经被广泛使用和尊重的数值属性(可能是级数中每个数字的平方等)。理想情况下,这对于编程来说是非常熟悉的,以至于派生单个数字只需要很少的通用编程语言(在我的例子中是 PHP)的开销。
这只是我的梦想吗,还是确实存在这样的事情?
This is a math problem, but I'm sure this must come up in some programming scenarios, at least I hope so, and I was wondering if there was a name for this type of situation:
Suppose I have 7 items in a series. For the sake of this example, let's use days of the week. I would like a user to submit which days of the week they plan to come in the following week. They are presented with a standard series of checkboxes, one for each day of the week.
I would like to store which days they choose in one database field as a single integer.
Obviously, I could assign each day a number, 1 - 7 (leaving 0 out in case the user leaves all choices unchecked). But then I run into problems if one user chooses Monday and Tuesday ( 1 + 2) and another chooses Wednesday (3).
I could also give each day of the week some bizarre unique such that it was impossible for any combination of digits to be identical to any other combination.
My hope is that rather than make up such a series for the second scenario, some numerical property already exists (perhaps the square of each number in the series, etc) that is already well-used and respected. Ideally, this would be so familiar to programming, that deriving the individual digits would take very little overhead of a common programming language (in my case PHP).
Did I just dream this up, or does something like this exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用位掩码 - 2 的幂,
依此类推。那么周一和周二就变成了:
Use a bitmask - powers of 2.
and so on. Then Monday and Tuesday becomes:
您可以使用
这些组合将是独一无二的。
然后星期一和星期二 = 3,星期三 = 4。
这很好地解释了 C# 中的想法
Enum Flags Attribute< /a>
You could use
The combinations will be unique.
Then Monday and Tuesday = 3, and Wednesday = 4.
This gives a good explanation of the idee in C#
Enum Flags Attribute
在内存宝贵的时代,程序员经常费尽心思将设置、参数、游戏布局等编码为位集。有些语言甚至内置了位集。
我记得有一次读过关于位集的教程,该教程使用它们来存储解决八皇后问题的棋盘布局。
大多数 C 程序员都有自己的小型位操作宏库来解决此类问题。例如,请参阅位集。它们仍然在直接硬件操作的代码中大量使用——例如在数字 I/O 中打开和关闭位。
Back in the days when memory was precious, programmers often went to a lot of trouble to encode settings, parameters, game layouts, and so on as bit sets. Some languages even had bit sets built into them.
I remember once reading a tutorial on bit sets that used them to store board layouts for solving the Eight Queens problem.
Most C programmers had their own little library of bit manipulation macros for just these kinds of problems. See Bit Sets for example. They are still used a lot in code that does direct hardware manipulation -- turning bits on and off in digital I/O for example.