標題:
201 解聯立方程式
[打印本頁]
作者:
b790113g
時間:
2012-2-18 10:40
標題:
201 解聯立方程式
本帖最後由 b790113g 於 2012-2-18 11:57 編輯
3 b2 ~0 x% Q9 G! E3 |1 W2 i3 A
7 s D8 q. \! B. g
使用者輸入 x 與 z
( ?0 |8 ]/ D" [
1 j) c9 ~2 B/ V4 {! W, B* g
計算出 3x2+2x+1
4 @1 [1 {0 |% {# j7 l% ?
' u' G8 u) D# Z5 n0 J- q
如果y<z 最佳解
% F7 P6 Y' a5 R6 t" z
如果Y>Z非最佳解
import java.util.*;
public class jva201{
public static void main(String arg[]){
Scanner s = new Scanner(System.in); //需引入 java.util.*
System.out.print("請輸入z可能的最大值:");
int z = Integer.parseInt(s.next()); //s.next() 字串->數字
int temp = 0;
for(int x=1;x<100000;x++){
int y = 3 * (x*x) + 2*x +1 ;
//System.out.println(y);
if(y>z){
System.out.printf("當 x =%d 時, Y=%d , Z=%d,符合 Y < Z 的條件",x-1,temp,z);
break;
}
//----------------------------
temp = y ;
}
/*
x y temp
1 6 0
2 17 6
3 34 17
*/
}
}
複製代碼
作者:
johnson
時間:
2012-2-18 11:57
本帖最後由 johnson 於 2012-2-18 12:00 編輯
6 o9 O s) p* |- ]& S8 P6 N" S
( x. @) L+ P& K; H
import java.util.*;
9 `% V& m$ k( l" `
public class j201
3 H1 i' A8 X2 G: {4 s
{
/ Y9 v/ G& y+ X, f* W
public static void main(String arg[])
7 _7 i4 p. l1 ~- f' i7 K9 I
{
/ K% d" V4 ?' m. N# u; K
Scanner s=new Scanner(System.in);
, ^2 m9 g& i1 c5 V& I/ A. D
System.out.print("請輸入Z可能的最大值:");
% }' |& Q B0 a
int z=Integer.parseInt(s.next());
! P2 B% Z/ _+ \4 g. H$ _+ c
int temp;
1 t7 f! C2 i" C$ N
for(int x=1;x<=10;x++)
# k3 s" Z& n2 v( C4 P- X& m
{
U: F3 l% V3 e1 ?0 j/ p+ |
int y=x*x*3+x*2+1;
& O/ `9 o G2 j, U
if(y<z)
$ H- k/ |6 r9 L% W) u0 m2 o9 ~: g
{
1 p e4 C, O. ^2 A/ H" }
System.out.printf("x=%d,Y=%d,Z=%d,符合Y<Z的條件");
: o9 T8 m9 \& I( Z
break;
* [6 G$ i7 N( w: C& h! U C
}
5 ], x8 C0 ]& a6 b5 }% V
temp=y;
: \& M$ |& d+ K! H
}
* t7 W8 g: r0 }6 f
}
' Y( s. J- y+ c N/ B) H
}
作者:
TOM
時間:
2012-2-18 12:04
import java.util.*;
public class jva201
{
public static void main(String arg[])
{
Scanner s = new Scanner(System.in);
System.out.print("請輸入 Z 可能的最大值:");
long z = Integer.parseInt(s.next());
long tot=0;
for(long x=1;x<=100000;x++)
{
long y = 3 * (x*x) + x*2 +1 ;
if(y>z)
{
System.out.printf("當 x =%d 時, Y =%d , Z =%d,符合 Y < Z 的條件",x-1,tot,z);
break;
}
tot=y ;
}
}
}
複製代碼
作者:
johnson
時間:
2012-7-10 20:36
import java.util.*;
public class j201
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
System.out.print("請輸入Z可能的最大值:");
int z=Integer.parseInt(s.next());
int temp;
for(int x=1;x<=10;x++)
{
int y=x*x*3+x*2+1;
if(y<z)
{
System.out.printf("x=%d,Y=%d,Z=%d,符合Y<Z的條件");
break;
}
temp=y;
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2