返回列表 發帖

201 解聯立方程式

本帖最後由 b790113g 於 2012-2-18 11:57 編輯
3 s) ~* |! n- T& J- q( ~3 X/ d, g1 V  E
使用者輸入 x 與 z2 ]3 c  b' c' O0 j

" Z, m; M8 r$ e% N計算出 3x2+2x+1- d) t& b' f) @! }2 B6 D

# N- J8 P, ~0 v$ b, V- C3 P如果y<z 最佳解
  q( e* \: i' M4 M% j如果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 編輯
7 z: o& s6 G7 Q
5 \9 i7 M0 G- F9 ?+ z/ p" r: a* Iimport java.util.*;+ o0 Y  c8 t% q( ]& A7 E
public class j2012 v1 E8 @+ L) B" W, Y. h6 e
{
; R, L- O; o2 Q0 C0 P% y8 W    public static void main(String arg[])
9 A; `( H, C( a% W/ C& U    {
+ o( W3 ~2 W- M- ^# z( h& P                Scanner s=new Scanner(System.in);7 p  g6 _9 L0 c5 T* l) ?; l  ^
                System.out.print("請輸入Z可能的最大值:");
% E6 }" p  R% J                int z=Integer.parseInt(s.next());! v- E- ~: [* h/ |
                int temp;0 Z+ T* _+ s& |  E: k
                for(int x=1;x<=10;x++)5 R, d; T$ E$ \
                {  q; A# i, N: F. x& W7 h( `
                        int y=x*x*3+x*2+1;6 ]5 M# C! b2 u1 ?' ~
                        if(y<z)
" E! O& U9 F& A8 [' b! p( _                        {4 @6 n' M, N8 |
                                System.out.printf("x=%d,Y=%d,Z=%d,符合Y<Z的條件");* Z, g" A# |/ Z4 k6 W( s5 h
                                break;8 Q' \, w* _7 v  B5 u& `8 S
                        }; \* Q( M. v( L2 ?" d9 Q5 Z% ?
                        temp=y;
/ }' s; E' c6 o. M) M! m2 y                }. a# d& ~* E; o4 F& O2 s0 B' x% J
    }) r5 j7 Y5 s8 s2 d/ @
}
小雲雀

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

返回列表