返回列表 發帖

TQC104 ~ TQC106 答案

TQC104% S2 x. \  t8 c0 b
public class TQC104 {& j2 S/ [* ^1 Q5 e9 `
  public static void main(String[] args) {
% n/ F; x: e( n. z   int a[]={0,1,2,3,4,5,6,7,8,9};2 v+ u+ y, n  H5 |3 Q7 k2 t
   boolean check=false;
( \. [. K. x# R1 h   int i,j,c1,c2,c3,r1,r2,r3,x1,x2;+ W" w( C. z' W: G7 P7 c
   do{
0 _; f6 |) X# w: Q; D      for( i=1 ; i<a.length ; i++){
' S. `# G) Y0 E         j=(int)(Math.random()*9)+1;
# p1 s0 L$ m% h3 b         a[0] = a[i];
$ D  Q+ ?8 A5 F3 `( ~         a[i] = a[j];
- z( [6 w* D  H: j( k5 U; Z         a[j] = a[0];: `& d1 T5 S: r; O: {) y1 v
      }4 A( L% x# L- h: C( e( a& {3 c

$ K5 |3 b7 p6 p9 E; V! M- p2 P5 V      c1=a[1]+a[2]+a[3];. G  f8 h; K) F# O3 l; t! T5 ^3 P3 Y
      c2=a[4]+a[5]+a[6];
1 ^* M  d! ?8 J2 |2 D- B      c3=a[7]+a[8]+a[9];
; g* c. d4 w' O! y$ p& P) X      r1=a[1]+a[4]+a[7];1 [) h* k) r7 N( l; A9 F
      r2=a[2]+a[5]+a[8];
, q3 f. _" U6 \1 {/ G, |, m6 y. |      r3=a[3]+a[6]+a[9];! i+ O$ H' G/ f' X  I
      x1=a[1]+a[5]+a[9];
, i% x. y/ s  ]- R4 O' U" y. E# G      x2=a[3]+a[5]+a[7];
' ~9 ^5 H. f% k      if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15
0 l# O! h8 Y! O! h+ q! [         && x1==15 && x2==15){
0 c2 U2 ^; f! \# J; z/ _! Z* P6 M         check=true;
0 o+ \" D% C2 x( y5 Q      }
, ^6 F6 Q0 ^! W/ Q   }while(check==false);' v1 Z5 A. p. C$ G. t# C( G7 `4 M
   System.out.println("答案為:");
: o% y7 \) x' T3 b$ d
0 q: C- s" |& V1 v      System.out.println(a[1] + " " + a[2] + " " + a[3] );
, p$ |% p4 {8 x8 e" G3 l0 K  B5 b      System.out.println(a[4] + " " + a[5] + " " + a[6] );# J# I# b: y( _/ ~8 b  P8 L1 h# F
      System.out.println(a[7] + " " + a[8] + " " + a[9] );, d! _9 w$ w5 x" q2 V' o% [% P
# X+ b' f8 q0 r- L  h# N! U& I5 N
   System.out.println("不論橫向縱向及對角線加起來的和都是:15");' J: A& p. X+ `9 h3 y+ J5 E
  }2 k$ W( z! h* O' |6 a
}
& y) C% g" y3 q8 _+ A) A+ u9 c; `5 s4 Z3 P  I0 b. v# P4 u
4 H) S  @( ]8 L* P/ P7 a

" r8 Z1 u  ^  T8 n+ OTQC105
3 m* }. |& L& F. R7 himport java.io.*;  K: d) Z# Z. _
public class TQC105 {
! c) s) V! Z  l3 o, f6 G& [2 Z/ H- r9 ^
  public static void main(String[] args){
* ]! o1 Q2 i' d0 n: h   try{
4 P4 c/ t- m8 _  _* u. B) y7 X      int row,col;4 h1 ~, A5 y5 [
      int i,j;
& y( s' S# F5 s* L5 w/ o* x      String tmp;/ s3 W1 D; d: B. q4 `
      int aryA[][];
8 G. z% i1 S2 j  T      int aryB[][];
7 l+ R6 n9 A- [$ [      BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
1 x# R  d1 [+ m+ V  
9 o( F4 m; Q' D1 c' U7 Q      System.out.println("請輸入陣列列數:");& X+ b; R  e2 j6 m
      row = Integer.parseInt(bin.readLine());
% t  D! w$ I0 f2 F  E      System.out.println("每一列陣列裡要幾個數字:");7 w- @+ u4 w" a: [" }
      col = Integer.parseInt(bin.readLine());
7 v3 A6 J; U) }, Z# R$ i; X  9 A% T+ \% }+ p
      aryA = new int[row][col];
. _5 b9 ]: y+ g# R      aryB = new int[col][row];
; B$ V# V9 u2 o+ D/ L  
8 v& P' Y1 N* y5 H      for( i=0 ; i<row ; i++){
$ G3 N1 P+ H8 y9 p. }* b+ m         System.out.println("請輸入第 "+ i +"列");6 }/ H( }2 e7 L% E
         tmp = bin.readLine();
, i8 p  y+ w# E. B7 }" r# u         String[] strA = tmp.split(" ");8 g' p8 L  x$ Y3 o7 t
         for( j=0 ; j<col ; j++ ){0 C6 b# F, ?  j, B6 E+ r. S
            aryA[i][j]=Integer.parseInt(strA[j]);0 X* ]3 V& i3 m# Z+ }7 l
         }
) P2 O8 q) I% \3 X0 k5 v4 {, X      }
3 R4 n$ l9 D; M$ a6 ~/ g      for( i=0 ; i<row ; i++){
" |. U  k9 Q* P. u0 F- w  ?         for( j=0 ; j<col ; j++){- n* L3 |" y7 c" M7 j# {
            aryB[j][i]=aryA[i][j];
/ Q7 k1 H/ R; Z8 {8 E/ \, }4 I         }
" w4 U, x; K  }# y1 z% Q# h      }( W7 I: B$ r" }. S/ Y
  
2 _  E% d: Q, w+ f! L      System.out.println("陣列經行列轉換結果");% W/ E" M6 X- n8 P  S* X4 g! ], F5 \
      for( i=0 ; i<col ; i++){
. U( v8 ^8 U: p0 p) Z% r         for( j=0 ; j<row ; j++){2 D( \3 j. d" B1 E7 }2 u5 S
            System.out.print(aryA[j][i] + " ");" g+ _$ T1 r# u5 [  F* A5 ?
         }
# f+ y0 L; p( x; y$ c* M& ^         System.out.println();8 U* Z; Z4 P8 F" w% p8 P
      }$ }, M5 g1 T5 ~9 ~+ m
   }catch(Exception e){8 J6 I$ _" R% X( ~) t
      System.out.println("請輸入數字");
. z$ ^) l. Y3 ~* A; f: @. c6 x      e.printStackTrace();
: V; f$ W% ^0 d- c5 w; M& \   }, T' X: S) r* n5 s  z

8 _$ p0 e% P& S, L% l% P! Z4 P" E  }6 m: Y4 R% e4 r# C) G& ?% \
}$ A' o0 |% r3 D9 p

. k) ^) j/ D, f4 z* y5 R3 f7 D; q
, S  T+ Y1 @0 q' P+ dTQC106' ]- P: D5 N6 o
public class TQC106* {$ c0 ~. s, G+ e; a! q
{# n  b/ O' u( e& N) [$ G9 _
   public static void main(String args[])5 r7 {# O# v" o3 n9 [: |* G
   {, |* M& [  |; j; \7 z
      int sum = 0;
7 M: S- x  b) s) D) E      int j =  0;
- X$ f( a4 H- N$ b8 l      int k =  0;
. u  `" z/ {) _, B2 C- L. d3 K" j      for(int i=0 ; i<args.length ; i++){9 B* f% z0 D0 t. a
         try{
3 K& P% P+ G4 Z) x! g& r            sum += Integer.parseInt(args[i]);1 x$ `: p4 e7 A& U& f, X
            j++;( y; v/ g/ h( G3 L) r: C$ D1 O) W
         }6 _( x5 s* j' t$ g6 n) y
         catch(NumberFormatException e){
5 [/ U. d& g, c4 B( D4 A  V            k++;
! ]3 L4 ]1 r" C. j) M4 N         }; P( K+ c. O# }2 r" W
      }
7 v' {# X4 j, A( J) x$ q' e3 h0 _      System.out.println("數值之總合為:" + sum );; E( g' w4 @1 }
      System.out.println("非數值個數為:" + k );# b% D+ a$ ^0 x6 ]: D9 A
      System.out.println("純數值個數為:" + j );
- K/ K& \# u* d$ |5 u   }$ `+ d7 [4 P5 z0 A2 g: L5 O* j2 h
}

返回列表