Board logo

標題: 201 解聯立方程式 [打印本頁]

作者: b790113g    時間: 2012-2-18 10:40     標題: 201 解聯立方程式

本帖最後由 b790113g 於 2012-2-18 11:57 編輯
3 b2 ~0 x% Q9 G! E3 |1 W2 i3 A7 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非最佳解
  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 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 j2013 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

  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. }
複製代碼

作者: johnson    時間: 2012-7-10 20:36

  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. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2