返回列表 發帖

TQC104 ~ TQC106 答案

TQC104! d/ @1 g5 j2 F( s
public class TQC104 {
) |& ?0 k: t. q  public static void main(String[] args) {" k4 h! \% W/ l
   int a[]={0,1,2,3,4,5,6,7,8,9};
+ G. [; h/ [- p; K   boolean check=false;
, X" \7 B/ J8 p   int i,j,c1,c2,c3,r1,r2,r3,x1,x2;7 v) y1 W1 u  s, u- j
   do{. u9 f: N7 X' m/ _6 t# h
      for( i=1 ; i<a.length ; i++){
" ]  |$ I) n) C- ?8 R* U9 o         j=(int)(Math.random()*9)+1;
. ?5 g# S" w8 `2 K; C9 m         a[0] = a[i];& Q1 {* W0 ~( t
         a[i] = a[j];- d9 z  G& g  X# [* O+ F- r) f$ k
         a[j] = a[0];
4 b. ?; v& e6 |8 X# r% p) H2 L5 B      }
& \0 o4 X& d; u2 [1 j  v
  }; ^6 a- M$ I* p      c1=a[1]+a[2]+a[3];9 O% T; `1 J4 j* f, K4 R. r8 ?
      c2=a[4]+a[5]+a[6];8 B' J! }2 q5 [5 X% t, Q
      c3=a[7]+a[8]+a[9];
- m; `. p! P. C6 _$ ?7 W      r1=a[1]+a[4]+a[7];7 }7 `! g# \& u6 G5 T) J$ K5 E
      r2=a[2]+a[5]+a[8];
" o% J" C8 _+ o5 x3 d8 y, |: @; C. a      r3=a[3]+a[6]+a[9];
7 ~, W$ i" t/ l! @6 F: k      x1=a[1]+a[5]+a[9];
  N! w. g! w) K* h      x2=a[3]+a[5]+a[7];: e2 z$ n) H) w; ^) R$ x/ e' \
      if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==154 J5 c, g( _! w) y/ h0 S  T
         && x1==15 && x2==15){
. g' p" B8 T7 [% y  f& S: S4 x         check=true;, \; Y. g/ C8 ^
      }
& A% y; J' p" E; C   }while(check==false);+ z) I+ @' e0 o4 q! [
   System.out.println("答案為:");
8 V' l6 P  b" o( a; y+ m5 `% Z& k# ^' j: ~& z
      System.out.println(a[1] + " " + a[2] + " " + a[3] );; ~1 r3 L" Z2 j: c8 z, _; r. x
      System.out.println(a[4] + " " + a[5] + " " + a[6] );
; S8 G. b* N! g# {6 u6 i4 E      System.out.println(a[7] + " " + a[8] + " " + a[9] );/ e3 N8 G; z0 l  a; X4 ]; z
. J3 S. f) {! L: Q1 C( B
   System.out.println("不論橫向縱向及對角線加起來的和都是:15");( R  K. f& u& `+ }# O! J
  }
( i2 K  B7 @& N2 e}2 u8 [5 c1 f* V# N. ?: }1 Z: H
! u3 ^( ?% @( Z. K/ n1 o( @: ^
3 I4 R. F; w: a& q8 G

4 J1 b7 P5 W4 uTQC105
4 w# i3 ~$ }1 E4 ^! B& e! {/ Eimport java.io.*;) {* F8 ?/ q- s8 K' G: k
public class TQC105 {7 d6 \1 s( E6 R+ i% {

) F  z9 B8 Q+ o6 }/ D' D  public static void main(String[] args){
! z2 P( Z) j/ U. G/ u9 a   try{% m% f6 D; {+ I5 \: q9 q  F
      int row,col;; F% @3 a2 L- c9 |. \' |. w
      int i,j;6 Y4 F& ~# ^* G
      String tmp;& Y0 x& ]% e/ `1 P
      int aryA[][];* g4 l( B# n! X) J
      int aryB[][];0 z0 Y' }6 N2 @( \+ P, a+ p5 C
      BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
3 J9 v9 ]' r" i, a9 p; m9 {$ D  , Y7 b! v9 R& G% \3 y  L
      System.out.println("請輸入陣列列數:");* |4 T2 y1 I8 y6 L2 j" R9 r" r
      row = Integer.parseInt(bin.readLine());
5 C; Z# V. u- E) N6 V7 y* f/ v      System.out.println("每一列陣列裡要幾個數字:");  i7 R2 v! E8 h3 u
      col = Integer.parseInt(bin.readLine());+ h$ c1 f3 E9 i$ `) W7 `- ~
  - v% s4 _: t# r- ^! y+ F* b+ ?
      aryA = new int[row][col];
" {9 b2 }- {+ @5 h; a9 d0 @3 |      aryB = new int[col][row];
/ X$ x8 }. o5 V  M  9 C) E  N! ?  L5 s: e- x( k
      for( i=0 ; i<row ; i++){/ ?* W% }/ x. s( H; x) m4 R5 X
         System.out.println("請輸入第 "+ i +"列");1 E: K  E' g  q7 z- p+ L$ o7 K/ r
         tmp = bin.readLine();
: B: N3 \9 h3 p; {2 j         String[] strA = tmp.split(" ");
1 s6 T& }9 @0 U! a$ j/ G7 i         for( j=0 ; j<col ; j++ ){
7 b) ^, }! X/ k            aryA[i][j]=Integer.parseInt(strA[j]);, [& g# K& E2 }8 z
         }
9 U4 [% K6 f" G( [% |# B      }
- t# a4 t8 A" q4 a      for( i=0 ; i<row ; i++){
* k2 d+ p2 Z) w4 N2 E$ T0 G         for( j=0 ; j<col ; j++){& Y* Y8 ]" g3 g, h, N* o
            aryB[j][i]=aryA[i][j];3 x+ i, L8 _$ f. o, v% Y+ T
         }
# y$ y& c" I' ^0 n6 c9 W9 M! H% z      }
. ~! K9 c2 t* Z# |7 S& Y% V  # a" [) O: B% D
      System.out.println("陣列經行列轉換結果");! Y* q, m% s$ _2 k" C6 G3 j
      for( i=0 ; i<col ; i++){
; x& a0 M' E4 B; X8 T         for( j=0 ; j<row ; j++){: c! z6 h+ ], i0 K; j
            System.out.print(aryA[j][i] + " ");- l; j* p. G: y8 a9 A+ W2 y
         }& k3 I8 \7 h6 X) j8 P
         System.out.println();/ f$ n5 z& I) c( W3 o5 {! K
      }% _( R! M+ i2 M; N2 X
   }catch(Exception e){& V# @; p: V2 M5 V
      System.out.println("請輸入數字");
. V& d8 {4 O0 T, u- S, a$ x  O      e.printStackTrace();
/ y0 j. i& g5 L5 e  ^6 y: H# E   }
4 y! l3 s& W2 C4 |7 v7 q/ {- @! a' I* g9 {& M, D3 l4 H* U0 h
  }
( L' @. W' ^/ `0 c' ?, `  |}' @/ ?4 p8 F) X9 B2 N4 L9 a
1 k3 ?" S5 q- r
4 I/ T5 O# D' K, Z4 f0 w
TQC106
% @9 F0 ]7 g# Jpublic class TQC106' C+ }; H8 @! u! u2 D  t! \
{
/ F9 o( Q, e( t* g$ [   public static void main(String args[]). O$ {9 O' f4 v9 ~. f1 |& Q' t! \
   {) `" y. s8 }( C# f. s
      int sum = 0;
; O0 I5 j& @# q3 j& V) B      int j =  0; . B* ~  C; ^, r( \  r+ I
      int k =  0;$ i5 [+ [* R. @. r/ p6 v
      for(int i=0 ; i<args.length ; i++){
# L  v2 ]5 O% u" ~9 Z         try{
# D& L% l+ L4 U9 u            sum += Integer.parseInt(args[i]);6 j1 A/ W+ Z  J! o5 G. k8 C
            j++;4 Z  d  ?* D  Y5 R5 _9 @: m
         }
* \  f; n/ N: _4 k# ^9 w! D         catch(NumberFormatException e){
( S' C- t) E5 Z            k++;
2 k  ]2 v3 F/ a0 v( C: H* j' U         }
) [8 M% k) F; L      }0 U' P; f0 A5 O1 J* C( A
      System.out.println("數值之總合為:" + sum );
3 J4 K8 g1 ]) @0 ]. }0 V9 X      System.out.println("非數值個數為:" + k );
0 b. P7 S  A" ^/ @      System.out.println("純數值個數為:" + j );
/ v+ u- X* m/ Y/ ?- [' [0 r, }   }$ c- q4 z) z( }7 V9 j
}

返回列表