返回列表 發帖

201 解聯立方程式

本帖最後由 b790113g 於 2012-2-18 11:57 編輯 : D2 G5 J5 P: q5 r" j+ U
7 e# F( U  I+ {; G9 y3 K; i6 S
使用者輸入 x 與 z% v) d6 i0 Y$ }' J) H
* n: V' l0 @' [/ L, E  }' a
計算出 3x2+2x+1
% s7 a; Z) ~' o" h. Z5 _1 p! ^& l
8 w3 ~5 ?2 H$ H  t如果y<z 最佳解
; R" k6 b( K) U如果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 編輯
% D. x% \- C0 i& q9 c  _! B1 z3 R: J7 \. ^4 ?# ^* q
import java.util.*;$ S& J' [# f( b* ^) Q, h+ [
public class j201+ c) i% e, z+ `; [1 r
{" ^' w' R' s/ ?/ p8 m& V8 j. _
    public static void main(String arg[])
* ^; s; U0 }; W/ ?3 w( F' ?    {( P& D+ m8 p; L, h8 {+ ^' w. Y
                Scanner s=new Scanner(System.in);9 Y, W3 w8 ?7 g, h0 d9 c
                System.out.print("請輸入Z可能的最大值:");
8 \2 s0 c0 ?( I; v4 m- d                int z=Integer.parseInt(s.next());7 v5 T, ^6 h7 X% d8 ^  F
                int temp;
7 {( c& z0 P9 g% s) o. Q; K' u6 ]                for(int x=1;x<=10;x++)( T. y1 |$ ^9 m: V: ~
                {% y, i; u% d' `* Z
                        int y=x*x*3+x*2+1;& `: a) y  b: h) A' ^
                        if(y<z)4 {1 Q8 T9 g, _' j
                        {
0 u, I+ L% U' F0 C8 ?7 F1 @                                System.out.printf("x=%d,Y=%d,Z=%d,符合Y<Z的條件");* Q0 ]2 _# K5 p% ^/ _& s4 [" h
                                break;
) B: e2 D* p) d) J7 W/ b                        }. c/ g! Y. K7 [) g# b+ H( t
                        temp=y;9 \& }5 z. K0 ]
                }5 S8 h' p* S* h9 E4 R& V( O
    }
8 D- f) ?& }3 U( N; M) m}
小雲雀

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

返回列表