解决“非常长的循环!” [英] Solve for "very long loops!"

查看:79
本文介绍了解决“非常长的循环!”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以帮我怎么做这个操作。

Can u help me how to do this operation.

Private Sub Max2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Max2ToolStripMenuItem.Click
Dim count As long=0
           For i1 As Integer = 0 To 90
             For i2 As Integer = i1 + 1 To 91
                For i3 As Integer = i2 + 1 To 92
                   For i4 As Integer = i3 + 1 To 93
                        For i5 As Integer = i4 + 1 To 94
                            For i6 As Integer = i5 + 1 To 95
                                For i7 As Integer = i6 + 1 To 96
                                    For i8 As Integer = i7 + 1 To 97
                                        For i9 As Integer = i8 + 1 To 98
                                            For i10 As Integer = i9 + 1 To 99
                                                'input n.
If (i1+i2+i3+i4+i5+i6+i7+i8+i9+i10<n) Then
count+=1
End if
                                            Next i10
                                        Next i9
                                    Next i8
                                Next i7
                            Next i6
                        Next i5
                    Next i4
                Next i3
            Next i2
        Next i1
        MsgBox("completed" &count)
End Sub





全部谢谢!

另一种方法可以做这项工作吗?



Thanks all!
another way to do this work?

推荐答案

用消息框替换整个代码。 请放心,这需要几千年,具体取决于你的硬件。



我很认真:无论在这里进行什么操作,都会重复大约10 ^ 20次。如果您的硬件每秒可以处理大约30亿次操作,那么完成它仍需要一千年的时间! (每年处理3亿次操作的每年3170万秒相当于每年95 * 10 ^ 15或~10 ^ 17次操作 - 所以千年应该足以处理这些循环)



解决这个问题的唯一方法是在具有几千个核心的强大GPU上进行编程,或者使用一组4-8 GPU,比如比特币农民使用的钻井平台。



如果有的话,这是硬件问题而不是软件问题 - 除非这个问题的目的是让你意识到这样做是个好主意。在开始编程之前首先检查一下...
Replace the entire code with a message box. "Please get comfortable, this will take a few thousand years, depending on your hardware."

I'm serious: whatever operation is performed here, it will be repeated on the order of 10^20 times. If your hardware can process about 3*billion operations per second, it will still take a thousand years to complete! (31.7 million seconds per year at 3*billion operations processed per second equates to 95*10^15 or ~10^17 ops per year - so thousand years should be enough to process these loops)

The only way to solve this is programming it on a powerful GPU with a few thousand cores, or maybe a set of 4-8 GPUs such as the rigs used by bitcoin farmers.

If anything, this is hardware question more than a software question - unless the purpose of this question was for you to realize that it may be a good idea to do a sanity check first before starting to program something...


首先,请阅读所有评论!



我是不确定我理解你,但你 - 可能 - 正在寻找一种方法来减少循环的数。



你知道什么是递归 [ ^ ]?在你的情况下,我建议写递归函数 [ ^ ]类似于:

First of all, please read all comments!

I'm not sure i understand you well, but you - probably - are looking for a way to reduce the count of for loops.

Do you know what is recursion[^]? In your case, i'd suggest to write recursive function[^] similar to:
void RecursionFunction(int currValue, int endValue)
{
  cout << "The number is: " << currValue << " when limit is: " << endValue << endl;
  if(currValue<endvalue)>
  {
    //do what you want to do
    RecursionFunction(currValue+1, endValue);
  }
}
//call
int j = 0;
int k = 90 
for (int l=0; l<=9; l++)
{
    RecursionFunction(j + l, k +l);
}



注意:上面的代码是用C ++编写的,因为使用了标记。


Note: above code is in C++, because of used tag.


我假设 n 不是在内循环中输入,而是作为参数给出(否则用户必须在接下来的1000年内回答许多提示)。



在这种情况下,您的代码会回答以下问题:i1到i10的数字组合存在多少,使得它们的总和小于 n ,其中i1至i10在0 ... 99的范围内并且相互不同并且以递增的顺序。现在,这个问题可以通过分析回答,而无需枚举i1到i10的所有可能组合。这是一个如何完成的暗示:



考虑只有两个变量i1和i2的简单情况,每个变量在1 ... 5和n = 8的范围内现在您可以在下图中可视化i1和i2的组合:

I assume that n is not to be input in the inner loop, but to be given as a parameter (otherwise the user had to answer many prompts over the next 1000 years).

In that case your code answers the questions: How many combinations of numbers i1 to i10 exists such that their sum is smaller than n, where i1 to i10 are in the range of 0 ... 99 and mutually distinct and in increasing order. Now, that question can be answered analytically, without enumerating all possible combinations of i1 to i10. Here is a hint how it's done:

Consider the simple case of only two variables i1 and i2, each in the range of 1 ... 5 and n = 8. Now you can visualize the combinations of i1 and i2 in the following diagram:
    i2  1  2  3  4  5
i1
1       *  *  *  *  *
2       *  *  *  *  *
3       *  *  *  *
4       *  *  *  
5       *  * 

The * means that the sum of i1 and i2 is smaller than 7. 



你能看到有多少种组合? (5 * 5) - 1 - 2 - 3 = 19.实际上,您的代码只计算in是相互不同和有序的组合。但你可以解释这一点。现在,您唯一需要做的就是为您的案例构建通用公式,并将问题从2维扩展到10维,这应该不是那么难。生成的代码将运行得更快,可能是微秒而不是一千年。我想有些改进。


Can you see how many combinations there exist? (5 * 5) - 1 - 2 - 3 = 19. Actually, your code does only count the combinations for which the in are mutually distinct and ordered. But you can account for that. Now, the only thing you have to do is construct the general formula for your case and to generalize the problem from 2 to 10 dimensions, which should not be all that hard to do. The resulting code will run a lot faster, probably some micro seconds instead of a thousand years. Some improvement, I guess.


这篇关于解决“非常长的循环!”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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