在PHP中获取数字的可能值 [英] Get possible value of a number in PHP

查看:52
本文介绍了在PHP中获取数字的可能值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入值,例如= 3360或任何值,如4500,5000,3900,2000等。



我有3个标准值= 1120,2240 ,3360 //这些是固定的长度



我的问题是,如果用户输入值3360,那么我该如何显示可能的值,如: -



1120 2240 3360

3 0 0 //总和3360

1 1 0 //总计3360
0 0 1 //总计3360



我的尝试:



我从来没有完成这些类型的工作,请帮助。

I Have a user input value e.g = 3360 or any value like 4500, 5000, 3900, 2000 etc.

I have 3 standard value = 1120, 2240, 3360 //these are fixed length

My question here is, If user input a value 3360 then how can I show the possible value like:-

1120 2240 3360
3 0 0 // It sum 3360
1 1 0 // It sum 3360
0 0 1 // It sum 3360

What I have tried:

I am never done these type of work, Please help.

推荐答案

首先考虑一下你必须使用的数字。 />
我们称他们为 x y ,以及 z ,并调用用户输入的目标 t

因此,您正在寻找
Start by thinking about the numbers you have to work with.
Let's call them x, y, and z, and call the target the user entered t
So you are looking for a solutions where
a * x + b * y * c * z = t



这里有两种可能的结果:

1)三个数字没有组合等于t。这将是最常见的!

2)有组合。

所以从 x 中最大的一个开始, y z

t中减去它。如果结果为正,则将t设置为它,并对其进行计数。回去再做一次。

如果没有,那么就没有更大的值可以安装在那里了,所以重复下一个最大的练习,依此类推,直到你用完为止。 x y z

如果在任何时候 t = 0 ,你就完成了并找到了匹配。



要查找下一个匹配项,请从第二个匹配项开始,然后重复此过程。



在纸上试一试,你会明白我的意思。



我建议这可能非常适合递归解决方案,但代码由你决定 - 毕竟这是你的功课!


There are two possible outcomes here:
1) There is no combination of the three numbers that equals t. This will be the most common!
2) There are combinations.
So start with the largest one of x, y , and z.
Subtract it from t. If the result is positive, set t to it, and count it. Go back around and do it again.
If not, there are no more large values that can be fitted in there, so repeat the exercise with the next biggest., and so on until you run out of x, y , and z.
If at any point t = 0, you're done and found a match.

To find the next matches, start with the second largest, and repeat the process.

Try it on paper, and you'll see what I mean.

I'd suggest that this is probably well suited to a recursive solution, but the code is up to you - this is your homework, after all!


这篇关于在PHP中获取数字的可能值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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