返回列表 發帖

201 解聯立方程式

本帖最後由 b790113g 於 2012-2-18 11:57 編輯 6 K6 X& ?% k4 v, I9 k! }( _0 i) Y
3 l" _5 m2 }, x8 p; B
使用者輸入 x 與 z, T9 e* v- [% p3 D, D/ `$ b

+ M* G- G9 y; D1 C& C  ^( F" ~: Y. Q+ z計算出 3x2+2x+1* A: Q2 j1 d' A; m  n+ ]0 F9 R; E

& w/ w3 W/ g5 F如果y<z 最佳解
( r) K: j( F+ S, F如果Y>Z非最佳解
  1. import java.util.*;
  2. public class jva201{
  3.         public static void main(String arg[]){
  4.        
  5.                 Scanner s = new Scanner(System.in); //需引入 java.util.*
  6.                 System.out.print("請輸入z可能的最大值:");
  7.                 int z = Integer.parseInt(s.next()); //s.next() 字串->數字
  8.                
  9.                 int temp = 0;
  10.                
  11.                 for(int x=1;x<100000;x++){
  12.                         int y = 3 * (x*x) + 2*x +1 ;
  13.                         //System.out.println(y);
  14.                         if(y>z){
  15.                                 System.out.printf("當 x =%d 時, Y=%d , Z=%d,符合 Y < Z 的條件",x-1,temp,z);
  16.                                 break;
  17.                         }
  18.                         //----------------------------
  19.                         temp = y ;
  20.                 }
  21.                 /*
  22.                         x        y        temp
  23.                         1        6        0
  24.                         2        17        6
  25.                         3        34        17
  26.                 */
  27.         }
  28. }
複製代碼

本帖最後由 johnson 於 2012-2-18 12:00 編輯
( q8 G! @6 d% ?& H. S, _* n5 x
2 F' L3 q0 T6 M6 s( i/ A6 oimport java.util.*;
# _3 S# h2 K6 J0 Rpublic class j2011 D7 r$ U) l/ k
{) O" P8 @* S8 i5 j/ G; q7 g6 L( r
    public static void main(String arg[])
. |' M, x1 G( Z9 G    {; w, n0 D0 i3 r6 ^( G6 y9 f
                Scanner s=new Scanner(System.in);0 k- K9 {, S! O
                System.out.print("請輸入Z可能的最大值:");) i# F" g( K2 G" N% E- K
                int z=Integer.parseInt(s.next());' o  F1 i  L/ [6 w0 M
                int temp;4 n( _9 |9 l) W) H5 [
                for(int x=1;x<=10;x++)
! D& t+ T' e, ]5 U7 _; n0 K                {0 d% K7 h; R; J$ K# D
                        int y=x*x*3+x*2+1;
) T. W! n" a" W% W) S# {5 A9 t                        if(y<z)5 F; i( }+ Y( o% p% ~& y1 ^
                        {
0 F& U" n; p' l) }) r, A                                System.out.printf("x=%d,Y=%d,Z=%d,符合Y<Z的條件");  p+ G& a5 t7 i9 [5 U4 x/ X
                                break;/ h# g) U5 [' q
                        }; |+ ~2 f0 W( q/ \9 ~+ l( [7 Z! `
                        temp=y;# l0 \2 ?) p0 ~* r0 r! T& r0 D
                }
( f* b0 p( k  g/ N6 q    }- ^1 t: {$ _6 K- N1 g
}
小雲雀

TOP

  1. import java.util.*;
  2. public class jva201
  3. {
  4.     public static void main(String arg[])
  5.     {
  6.         Scanner s = new Scanner(System.in);
  7.         System.out.print("請輸入 Z 可能的最大值:");
  8.         long  z = Integer.parseInt(s.next());
  9.         long tot=0;
  10.                 for(long  x=1;x<=100000;x++)
  11.                 {
  12.                 long  y = 3 * (x*x) + x*2 +1 ;
  13.                         if(y>z)
  14.                         {
  15.                         System.out.printf("當 x =%d 時, Y =%d , Z =%d,符合 Y < Z 的條件",x-1,tot,z);
  16.                         break;
  17.                         }
  18.                 tot=y ;
  19.                 }
  20.         }       
  21. }
複製代碼
水桶小鄭,鯰魚

TOP

  1. import java.util.*;
  2. public class j201
  3. {
  4.     public static void main(String arg[])
  5.     {
  6.                 Scanner s=new Scanner(System.in);
  7.                 System.out.print("請輸入Z可能的最大值:");
  8.                 int z=Integer.parseInt(s.next());
  9.                 int temp;
  10.                 for(int x=1;x<=10;x++)
  11.                 {
  12.                         int y=x*x*3+x*2+1;
  13.                         if(y<z)
  14.                         {
  15.                                 System.out.printf("x=%d,Y=%d,Z=%d,符合Y<Z的條件");
  16.                                 break;
  17.                         }
  18.                         temp=y;
  19.                 }
  20.     }
  21. }
複製代碼
小雲雀

TOP

返回列表