返回列表 發帖

TQC104 ~ TQC106 答案

TQC104" w% N) t+ m' d$ U* K" B: w5 ]; m
public class TQC104 {) q! D% ~$ e; k3 i2 o
  public static void main(String[] args) {2 a' l  @' U$ H' R( r
   int a[]={0,1,2,3,4,5,6,7,8,9};
! g% x3 s# d0 R% R+ l   boolean check=false;
3 `5 z  b* T/ l5 f- r   int i,j,c1,c2,c3,r1,r2,r3,x1,x2;& B; D  Q9 z/ K; r7 Y
   do{" \, Y: t/ g) m7 I+ \9 m
      for( i=1 ; i<a.length ; i++){
0 z6 A% L) p) [2 h3 V9 s) ]         j=(int)(Math.random()*9)+1;
7 ]9 G6 U  f& F" s. Q         a[0] = a[i];
" ]5 a& A0 ~0 V         a[i] = a[j];5 u/ b4 J8 Y1 K4 y5 F3 h+ U+ b% `
         a[j] = a[0];! o) K" n# R" r
      }
& h, R6 q( D  ]# i( g% H/ q5 h4 }' ]6 ]& i$ U% P; `
      c1=a[1]+a[2]+a[3];- V* @9 I( y( Z4 |5 P' G# V
      c2=a[4]+a[5]+a[6];* J5 ~: c1 a; d  b1 R
      c3=a[7]+a[8]+a[9];
/ j1 b* Q# B/ }+ o3 l3 V      r1=a[1]+a[4]+a[7];7 y3 @# T* S6 _  D
      r2=a[2]+a[5]+a[8];
7 g' d4 \, k, |$ V( v1 M9 K      r3=a[3]+a[6]+a[9];
6 ]0 r7 Q3 P7 O- p2 T8 b+ P* @' C      x1=a[1]+a[5]+a[9];4 W3 F' `: I6 n. K5 b* l
      x2=a[3]+a[5]+a[7];
6 m0 H) ]9 _* L" z8 G8 n7 Y      if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15% \+ c& L: E/ Q' t
         && x1==15 && x2==15){# n9 o" i/ B( ]  M# X- A
         check=true;
2 T6 m' n, T4 F* H* e$ \      }# @6 e4 q7 s0 \' w
   }while(check==false);
' M2 D1 U+ Z; x1 l' `% K" g$ l- L  @   System.out.println("答案為:");- D' W2 r4 l1 H( q1 P' a

" C- |" D: j( _6 N; E      System.out.println(a[1] + " " + a[2] + " " + a[3] );
9 ?) n0 z4 g% G      System.out.println(a[4] + " " + a[5] + " " + a[6] );( z6 e, m. F. S' X/ j, \" w
      System.out.println(a[7] + " " + a[8] + " " + a[9] );' _1 t1 ^5 }+ Z7 y* f
+ G; {" x1 v: m: J
   System.out.println("不論橫向縱向及對角線加起來的和都是:15");
; D" K0 i& q* ~: R- Z% p  }
/ F5 G+ _! J6 B* ~: g4 M+ C4 a/ p}
8 y' a. t3 \$ F! h, B1 S4 e( O' V% M2 R, |2 h0 `* J4 i

" G0 T: N+ p& M, w: A. n/ t# Z, [7 f- t; `1 f' J
TQC105
$ B# c" E& f* t. Y) V3 aimport java.io.*;
- E: v2 {- ]. D3 W& g) ]" O4 Tpublic class TQC105 {
: D7 ~5 q' G: K) v' `1 M; K, N
( u* X' z8 ?  O  n+ B( b  public static void main(String[] args){/ W; [. E8 N; S8 F4 {$ ]
   try{, g3 s3 z! ^0 f: \. a* i% k% U
      int row,col;
  _8 }. a  q  `" F, v      int i,j;
! J7 f( j! F5 z3 a6 D6 `3 B# @      String tmp;
" z. ?; _: X8 q' g7 Y. d. \      int aryA[][];6 v- u% Z- x2 e, m
      int aryB[][];
4 `6 j/ D5 u8 E; V" C- c5 P      BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));' \* k/ ^3 S5 |: h
  $ g  N7 ], i$ ?: o
      System.out.println("請輸入陣列列數:");+ }$ _- p4 G/ F" O
      row = Integer.parseInt(bin.readLine());% x! L  N6 V) A) F% ]- }
      System.out.println("每一列陣列裡要幾個數字:");
( e( h6 k) u& S8 \3 p      col = Integer.parseInt(bin.readLine());- l5 T% y! m) l" a/ c3 c
  / v) t- X) H! I. x! A! o% e
      aryA = new int[row][col];
* l$ H  k: P! P& u      aryB = new int[col][row];) g$ j" T1 k4 O
  
( F( Z; @) h: u* Y      for( i=0 ; i<row ; i++){$ }8 c* o0 t9 B& N+ O) L
         System.out.println("請輸入第 "+ i +"列");
* I! {+ e# K' a4 I8 ~( m2 |         tmp = bin.readLine();
& W# G$ w0 ]0 A) @         String[] strA = tmp.split(" ");
8 [2 `: \# S8 P& j         for( j=0 ; j<col ; j++ ){, Z, Z9 \# a$ t! B
            aryA[i][j]=Integer.parseInt(strA[j]);
" k  g8 t5 d5 U; W# x         }
7 p+ m# r2 ?/ M# z3 b      }9 R, W* Z$ X% N) S8 y
      for( i=0 ; i<row ; i++){- g) t2 m3 b% j( _- P! P
         for( j=0 ; j<col ; j++){5 N& V# M* r" ^/ d
            aryB[j][i]=aryA[i][j];
8 J* d7 H, y4 Y- B2 \         }8 Q9 }0 o: C. c# h" j. }
      }) I3 e. F* ?: t7 m3 e
  . n- E( o+ L' ^/ M
      System.out.println("陣列經行列轉換結果");
  K/ o( @7 h+ K, W      for( i=0 ; i<col ; i++){/ j5 w) o; C' T. A
         for( j=0 ; j<row ; j++){
2 ^5 Q4 v. r2 @' j            System.out.print(aryA[j][i] + " ");8 M6 [$ M* L' f9 o/ `* n" y
         }9 U4 {; ]! G) d$ \
         System.out.println();
1 D% e/ G, O* m$ z: Z      }5 a2 K7 B) ?$ [; q" q
   }catch(Exception e){# m* M5 U. v8 K5 V( r6 }  |
      System.out.println("請輸入數字");
- C% x1 ~, |) _- Y      e.printStackTrace();
: w* z) }! `3 ?" o. X' N. ~) V   }
3 y* i8 {, h' x, R6 j- I1 g$ o/ a" K
  }6 y$ b% W3 V; W+ ^
}" e3 d' G/ ^1 B$ n# h  V; Z
! j2 X6 y1 ^' W& A) \. D
1 K6 }  w+ Y3 e& l' }7 l8 g0 _, e" w
TQC106; }8 v9 X: ]8 i' P
public class TQC106" n* u; K1 c7 C0 l5 J
{4 f  v! ?% G" D" I& v
   public static void main(String args[])4 y, g4 d; b( h' _, Y  M# x
   {
& N0 Z- \$ `7 k4 u$ l! \' t- c% s7 ~* M      int sum = 0; ) I: }* R' R: W6 ]3 Z% z) N
      int j =  0;   q0 I: `; x  _: u
      int k =  0;
' ?, X% x3 `4 i# `1 r0 V      for(int i=0 ; i<args.length ; i++){
+ ]; Z! l+ `+ r: _! Y         try{
& m. |& w3 B7 o5 E+ ]            sum += Integer.parseInt(args[i]);
% r, @/ i( ~( C- Q! _            j++;& W* J2 ^" q. j. E! I
         }; h8 y$ N0 @* y9 [1 u9 T
         catch(NumberFormatException e){
4 @; d$ \: C8 f) x! i0 ~. F$ p3 ?' L            k++;  @: x3 x6 e6 V; ^
         }5 S0 O1 b6 k" B7 L5 J8 [9 a
      }  K6 R# V! e2 ]/ q: I, s6 V
      System.out.println("數值之總合為:" + sum );
3 C# A  u6 ^; T8 u      System.out.println("非數值個數為:" + k );
1 Q9 R. M* a7 m, z' S, ]. {( U      System.out.println("純數值個數為:" + j );5 S2 ]! l5 j5 x6 r
   }
& d: S  r( f. x8 n( D& c" N0 e/ H}

返回列表