生成一组所有可能性 [英] Generating a set of all possibilties

查看:111
本文介绍了生成一组所有可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C中制作一个游戏,我需要为序列的给定代码长度生成一组所有可能的数字序列,并给出数字序列中的数字范围。



例如:如果代码长度为4且范围为0到8



则总数为。可能性是9 ^ 4



我想创建一个包含所有这些可能性的集合。关于我应该怎么做的任何建议?



请记住我是一个初学者,并在几周后开始学习C.



谢谢! :)

I am trying to make a game in C for which I need to generate a set of all possible "sequence of numbers" for a given code length of the sequence and given range of digits in the sequence of numbers.

for eg : if the code length is 4 and the range is from 0 to 8

then total no. of possiblities are 9^4

I want to create a set containing all these possiblities. Any suggestions on how I should go about it?

Please bear in mind I am a beginer and started learning C just a few weeks back.

Thank You! :)

推荐答案

您的问题可以简化为数字系统的主题。在您的示例中,您将讨论9号系统中的4位数字。为什么?想想这个代码长度为4,范围从0到9 - 你有从0000到9999的所有10000个数字。你知道了吗?

所以你只需要从在您的情况下(或任何其他值)0000至8888。

算法很简单:

Your problem can be simplified to the topic of number systems. In your sample you talk about a 4 digit number in the number 9 system. Why? Think of this one "the code length is 4 and the range is from 0 to 9" - and you have all 10000 numbers from 0000 to 9999. You got it?
So you simply have to count from 0000 to 8888 in your case (or to any other value).
The algorithm is simple:
 0) let N be the code length, and D the highest value.
 1) declare an array of int with N elements
 2) set all values to 0
 3) add 1 to the last element
 4) loop from the last to the first element:
 5)   if the current element is higher than D, reset to 0, 
 6)      if there is a previous element, increment the previous element
 7)         else you passed the last element, finished
 8)      else exit loop
 9) PROCESS array
10) go to step 3



你可以添加额外的条件只有在你需要的时候停在最后一个元素而不是传递它。在步骤9中,数组将包含您需要的数字。

如果使用数组值作为索引,则可以将其扩展为任何数字集。


You can add an extra condition to stop at the last element and not to pass it, only if you need it. In step 9 the array will contain the "number" you need.
You can extend this to any set of "digits", if you use the array value as index.


如果我理解正确,你想要做的是:



给定A,B,C,生成所有可能的长度为2 =>的字符串; AA,AB,AC,BA,BB,BC,CA,CB,CC



在这种情况下,你会想找到生成的代码C中的排列谷歌应该在那里提供很大的帮助,我自己也不知道任何好的C例子,我只做过涉及Python的排列的东西,它在标准库中支持它们。
If I understand correctly, what you want to do is something like:

Given A,B,C, generate all possible strings of length 2 => AA, AB, AC, BA, BB, BC, CA, CB, CC

In that case, you''ll want to find code for generating permutations in C. Google should be of great help there, I don''t know of any good C examples myself, I''ve only done stuff involving permutations in Python which has support for them in its standard library.


我想建立Zoltan解决方案。因此,您了解您想要在基数为9的数字系统中枚举从0000到8888的所有数字。



有一种非常简单的方法可以做到这一点。只需使用一个int变量,逐步增加它。 int中的值存储在base 2数字系统中,对于我们人类,每当我们显示或打印其值时,我们倾向于将其转换为十进制(基数为10)。但我们同样可以在任何其他基础上输出它,例如通过

I would like to build upon Zoltan solution. So you understand that you want to enumerate all numbers from 0000 to 8888 in a number system with base 9.

There is a pretty easy way to do that. Just use an int variable an increase it step wise. The value in an int is stored in the base 2 number system and for us humans we tend to convert it to decimal (base 10) whenever we display or print its value. But we can equally well output it in any other base, for example by
int i;
char buffer [20];
itoa (i, buffer, 9); // convert i into a string in buffer with base 9



所以你所要做的就是从0到某个值计数,并将上面显示的值转换为基本9系统。现在,您需要计算的价值是多少?那么十进制10000(base9)的对应关系是什么?这是我们不得不停止计算的价值。它是


So all you have to do is count from 0 to some value and convert that value in way shown above to the base 9 system. Now, what is the value you need to count to? So what is the correspondence of 10000(base9) in decimal? That is the value at which we have to stop counting. It is

limit = 8 * 8 * 8 * 8;



所以这是简单的版本:


So here is the simple version:

int i;
int limit = 8 * 8 * 8 * 8;
char buffer[20];

for (i = 0; i < limit; ++)
{
    itoa (i, buffer, 9);
    /* do something with buffer, put it in array for example */
}





唯一的缺点是itoa不会产生前导零。因此,对于第一个值,您将获得0而不是0000。如果这困扰你,你可能会考虑自己转换到base 9,例如:



The only downside is that itoa does not produce leading zeros. So you will get "0" instead of "0000" for the first value. If that bothers you, you might consider to do the conversion into base 9 yourself, for example:

char d;

for (j = 0; j < 4; ++j)
{
    d = (i % 9) + '0';
    buffer [3 - j] = d;
    i /= 8;
}
buffer[4] = '\0';





我希望能给你一些想法。



I hope that gave you some ideas.


这篇关于生成一组所有可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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