任何人都可以使用c#.net尝试以下逻辑吗? [英] Can anybody try the below logic using c#.net?

查看:59
本文介绍了任何人都可以使用c#.net尝试以下逻辑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下面额2,3,7。如果我们将一个输入传递给函数,那么函数应该返回组合的总数。



例如我将输入传递为9.然后函数应该返回3因为当我们添加面额时

2 + 2 + 2 + 3 = 9 - 组合1.

3 + 3 + 3 = 9 - 组合2.

2 + 7 = 9 - 组合3.

I have the following denominations 2,3,7. If we pass an input to the function then the function should return the total number of combinations.

For Example I passed the input as 9. Then the function should return 3. Because when we add the denominations
2+2+2+3=9 - Combination 1.
3+3+3=9 - Combination 2.
2+7=9 - Combination 3.

推荐答案

简单的蛮力方法是:



一般组合可表示为
The simple brute force approach would be this:

The general combination can be represented as
2A + 3B + 7C

其中A,B和C小于或等于0且不超过N / 2,N / 3或N / 7,其中N是所需的总和。然后,您可以列出三重嵌套循环中的所有组合,并检查它们是否适合。这个过程很容易优化,因为一旦你发现了一些组合或超过总和,增加任何因素是没有任何意义的。另外,考虑一下这三个循环的正确排序。



这是我想到的第一件事。



-SA

where A, B and C are less or equal to 0 and no more then N/2, N/3 or N/7, where N is the required sum. Then you can list all the combinations in a triple nested loop and check up if each of them fit or not. This process is easy to optimize a bit, as once you found some combination or exceeded the sum, it would not make any sense to increment any of the factors. Also, think about proper ordering of those three loops.

This is the first thing which comes to mind.

—SA


这篇关于任何人都可以使用c#.net尝试以下逻辑吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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