返回列表 發帖

201 解聯立方程式

本帖最後由 b790113g 於 2012-2-18 11:57 編輯 ; \3 r1 o7 a3 Y4 a# |; L
. z7 g( n" ]8 q$ d7 t. `$ }
使用者輸入 x 與 z/ ~/ y4 Z2 w( n8 i

0 l" l6 O# m  q3 s  Z6 O計算出 3x2+2x+19 `; l5 ~4 y: U( V- ]
" M8 F1 k+ n6 o( X/ Q- l' d" `
如果y<z 最佳解
  `. |2 m8 i: h# x$ n0 z3 ^如果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 編輯
# T$ {! x7 S6 C. s/ p( K/ I2 V9 \8 |9 `" A7 \
import java.util.*;
* ?! X& c/ r/ @: G. xpublic class j201- ~  \9 Z) P+ O6 [
{
% I, F) U; W3 D) H6 }* y    public static void main(String arg[])
+ W+ F  h: b& A* G2 h; G1 z    {+ E( \# F/ X% a& D: h
                Scanner s=new Scanner(System.in);
1 h  q; s7 n. _0 A* T5 Z8 b; U# g                System.out.print("請輸入Z可能的最大值:");+ a6 J2 L6 a% P" L4 O" }
                int z=Integer.parseInt(s.next());
3 a, ], @: z6 T                int temp;; V, d" x8 u3 Q: @+ h; T
                for(int x=1;x<=10;x++)* u) S; X" r, c) h" k) Z+ W
                {
; L1 o7 v; q+ c" Y; {( e                        int y=x*x*3+x*2+1;
) t  _2 ^  Z8 N  O% ^3 r; n                        if(y<z)
  H2 q1 F  x$ t                        {9 i  P8 I7 n+ p2 D# W1 @. k' F/ e
                                System.out.printf("x=%d,Y=%d,Z=%d,符合Y<Z的條件");5 m, k: k! z; i% q9 V8 _! ^2 @! r
                                break;
/ q, d9 w& C: r0 |+ _+ |                        }; t- n7 p! {. S' i7 P, m  k$ ~/ X
                        temp=y;3 o. n* J# H0 F
                }1 h/ Q& e6 Q# |
    }
5 @, C# H( C3 ^}
小雲雀

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

返回列表