Topic: 大家能用编这个小程序吗?

  Print this page

1.大家能用编这个小程序吗? Copy to clipboard
Posted by: smwyzy
Posted on: 2008-10-19 21:13

x+y+z=100
3x+5y+0.5z=100
能编个能解这方程组的程序吗?让我学习学习;

2.Re:大家能用编这个小程序吗? [Re: smwyzy] Copy to clipboard
Posted by: micsolaris
Posted on: 2008-10-20 02:20

你给的这两个式子要求的解应该是不存在的吧?三元函数好像代表的是一个三维图中的面吧,两个面相交的话是一条线,所以解应该有无数个。除非你要的是如下的做法。(取一段范围的数 x (1-100) y (1-100))

package com.ricky.www;
public class Test{
  public static void main(String args[]){
    int z = 0;
    for(int x = 0 ; x < 100 ; x ++){
      for(int y = 0 ; y < 100 ; y ++){
        z = 100 - x - y;
        if((3 * x + 5 * y + 0.5 * z) == 100){
          System.out.println("当x = " + x + " y = " + y + " z = " + z + "时候等式成立.");
        }
      }
    }
  }
}

3.Re:大家能用编这个小程序吗? [Re: smwyzy] Copy to clipboard
Posted by: smwyzy
Posted on: 2008-10-21 21:37

多谢,漏了个条件:x,y,z必须都是整数。

4.Re:大家能用编这个小程序吗? [Re: smwyzy] Copy to clipboard
Posted by: gbluo
Posted on: 2008-10-29 11:41

我在想x,y和z能不能取负值.......
要是能取负值的话,一楼的程序应该有问题,要是不能的话,只要:
package com.ricky.www;
public class Test
{ public static void main(String args[])
{ int z = 0;
for(int x = 0 ; x < 34 ; x ++)
{ for(int y = 0 ; y < 21 ; y ++)
{ z = 100 - x - y;
if((3 * x + 5 * y + 0.5 * z) == 100)
{ System.out.println("当x = " + x + " y = " + y + " z = " + z + "时候等式成立.");
}
}
}
}
}
就可以,不过可惜,这个程序我也没有运行出结果来,大家帮忙看一下.

5.Re:大家能用编这个小程序吗? [Re: smwyzy] Copy to clipboard
Posted by: floater
Posted on: 2008-10-30 02:16

no solution since you have that 0.5 coefficient there.

6.Re:大家能用编这个小程序吗? [Re: smwyzy] Copy to clipboard
Posted by: zyj0021
Posted on: 2008-10-30 23:44

得,回去看下线性代数!!
数学的东东忘的差不多了!!!!!

7.Re:大家能用编这个小程序吗? [Re: smwyzy] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-11-10 20:31

IMHO, I don't think we can work out this simultaneous equation by only two equations. As far as I can remember, my maths teacher in the senior high school mentioned we need at least three non-redundant equations, in order to work out x, y, z.

In addition, as *micsolaris* pointed out, there are three unknown variables, x, y, z. Thus it is not a linear line but a surface. Hence, these two equations represents two separate surfaces in a 3-D space. Then in this case, there are two options:
1) If they intersect, then answers: x, y , z are unlimited.
2) If they don't intersect, then answers are none.

Fortunately, I think our computer can help us to solve this simultaneous equation by running the following code: (which accounts for negative numbers.)

package com.algorithm;

public class FormulaXyz
{
public static void main(String[] args)
{
Integer x = Integer.MIN_VALUE;
Integer y = Integer.MIN_VALUE;
Integer z = Integer.MIN_VALUE;

// I want to calculate the time to execute this algorithm
long startTime = System.currentTimeMillis();

for (x = Integer.MIN_VALUE; x < Integer.MAX_VALUE; x++)
{
for (y = Integer.MIN_VALUE; x < Integer.MAX_VALUE; x++)
{
for (z = Integer.MIN_VALUE; x < Integer.MAX_VALUE; x++)
{
if (x + y + z == 100 && 3*x + 5*y + 0.5*z == 100)
{
System.out.println("Bingo, formula solved, we have found x,y,z:");
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
}
}
}
}

long stopTime = System.currentTimeMillis();

System.out.println("Time to execute this program takes: "
+ (stopTime - startTime) + "milliseconds");
}
}


It is important to note that this program only takes account of the Integer range which inside -2 to the power of 31 to +2 to the power of 31. Of course all computers have limited CPU, memory, resources etc... We can not run unlimited range of numeric values.

Unfortunately, I have naively think about successfully running this program on my computer. The program has been running on my machine for about 3 hours and it took all my CPU utilisation. Perhaps you can use a super-computer to run this program and find out the answer. (maybe DeepBlue can help)

Finally, I do believe that there are answers for this simultaneous equation. Smile

Regards,
Jiafan


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923