返回列表 發帖

TQC104 ~ TQC106 答案

TQC104" X% O2 K& B( q, D( Y' ?  ^' J
public class TQC104 {
& Y! b* p3 P2 ^: u8 u) m  public static void main(String[] args) {' z7 e' F9 b6 Q1 n4 h; [
   int a[]={0,1,2,3,4,5,6,7,8,9};
1 q; V- C. z9 H- y7 `   boolean check=false;
/ P  \3 i9 z1 g4 L# P) J4 X   int i,j,c1,c2,c3,r1,r2,r3,x1,x2;9 q8 ?! `7 q2 [: n- R" s
   do{8 r& C' ~7 a# K% C
      for( i=1 ; i<a.length ; i++){
$ B$ @+ f0 ^1 H& i% f6 O4 N         j=(int)(Math.random()*9)+1;/ i3 |' Q) m2 p% c1 |8 N
         a[0] = a[i];
- Q" N5 V$ L9 a: A  B         a[i] = a[j];
' w" i# @2 l' q9 d- T         a[j] = a[0];1 ~- i+ R4 Y+ n9 c3 V: y
      }! V4 I3 e1 {! V# J
7 W9 D* M4 z) M% G
      c1=a[1]+a[2]+a[3];8 R8 f7 O& K+ ]" v8 y9 `% T6 n6 J9 @
      c2=a[4]+a[5]+a[6];( X1 I* P) ~5 j' l. w
      c3=a[7]+a[8]+a[9];
1 z. \  s& D$ P# O: V" l      r1=a[1]+a[4]+a[7];1 l8 y- o5 t3 V! Z
      r2=a[2]+a[5]+a[8];
2 z" k, Z+ V8 x( i      r3=a[3]+a[6]+a[9];% q* k  t+ F0 d
      x1=a[1]+a[5]+a[9];: R: _+ q+ }2 V& N. O8 l- P
      x2=a[3]+a[5]+a[7];
0 u5 }# c/ y- X4 G' i+ ?3 b: i* w      if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15, [( g( q( [, u  [% U2 b
         && x1==15 && x2==15){# j' Y9 J+ N2 r4 j" O/ E
         check=true;/ H4 [  z! ^, B" Y) m5 a
      }
: w% F6 m( Q( }   }while(check==false);
2 [4 `% `2 s5 Q9 N   System.out.println("答案為:");( Z! V9 m1 P! w

' E5 M7 K1 `7 p3 C1 A7 G8 V) |+ K9 j      System.out.println(a[1] + " " + a[2] + " " + a[3] );
1 m* R  v5 b6 a4 b/ Z      System.out.println(a[4] + " " + a[5] + " " + a[6] );
7 m- E% r: m( |2 ~; Z$ j3 P      System.out.println(a[7] + " " + a[8] + " " + a[9] );* p" ?! E8 o: Q6 n* K$ j7 n
2 {8 Y+ E% n9 ?; f* g) Y
   System.out.println("不論橫向縱向及對角線加起來的和都是:15");1 s2 E/ G: t' ^8 [( f  W2 h) S
  }5 Y: V$ f/ z7 P$ G5 ~; @
}
1 ^. s% e% B' J
; m" p. a9 ?+ K( T; J# k
9 T4 Y  F& h& q3 P" |. W( E, g& r
TQC105( p. U( R& }# X& H, F1 T& l
import java.io.*;1 c5 |2 b2 w5 O; P0 d+ _
public class TQC105 {
' S( \) j1 k/ G4 j
( R: f2 Y) V7 K# O9 O  public static void main(String[] args){$ j: D' N& i& v' z7 M9 m/ L* [) Q! {! a
   try{
1 q5 u* W" j$ _3 }8 f      int row,col;+ Q  v7 |8 s2 x0 m  v
      int i,j;
" ^5 p6 U! B: z# M. d, Z3 `) P      String tmp;! Q$ r9 K1 ]' r, e# L! S5 n
      int aryA[][];
- Y0 N# P: I! |+ T      int aryB[][];3 i* Q- {5 Q6 Q# O* Y
      BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));# {' B4 I7 d* B$ U! R/ Q. w1 Z1 ?
  # J% Z" S& `6 M# Q! k5 Z' y
      System.out.println("請輸入陣列列數:");
2 }' v5 }# e- y" D  B2 K      row = Integer.parseInt(bin.readLine());
! {3 q+ I* N) I" O9 N      System.out.println("每一列陣列裡要幾個數字:");
5 Q1 ~  M: h, o$ l, L$ n8 ]" p      col = Integer.parseInt(bin.readLine());) e8 a% U4 Y; p. X7 |
  9 {. Y$ p4 X+ y, g2 [% e* n
      aryA = new int[row][col];- M3 @5 I! ?4 r& W7 Z
      aryB = new int[col][row];
! B3 Y2 J+ X; U  o, C* Y7 O  7 @, H$ |( }1 y3 g! Y; h0 J* Z5 R# Y
      for( i=0 ; i<row ; i++){4 [: S0 f; W) D( o0 q$ p. H5 X4 T
         System.out.println("請輸入第 "+ i +"列");
$ m/ m) S% j. L* ~4 a# q         tmp = bin.readLine();* {5 Y( x% ]1 P9 T
         String[] strA = tmp.split(" ");! c6 j; p0 E8 b  L5 u$ g
         for( j=0 ; j<col ; j++ ){7 X; \% d' l& B; y# @1 t! n2 `/ U
            aryA[i][j]=Integer.parseInt(strA[j]);
- o1 e4 l0 S& y0 d& o         }+ g9 _/ f1 {  L6 \1 {4 g
      }
' O7 C9 [$ a/ U, j0 C1 a      for( i=0 ; i<row ; i++){  c3 l# L, |5 g, S
         for( j=0 ; j<col ; j++){
3 F; N* c: }) O- r5 _            aryB[j][i]=aryA[i][j];; u# y$ I, U' Z$ s# K
         }
5 j* a1 F" b" y# N  w; y+ b% P      }5 Q7 z+ A& R, Y3 Y& z2 ~
  
  M* f" V: d% U* X+ ~6 W* E      System.out.println("陣列經行列轉換結果");! v/ T2 u/ Q3 T, O1 \; d% V
      for( i=0 ; i<col ; i++){+ j' b" i+ E$ c& n; ~& Q, z/ O
         for( j=0 ; j<row ; j++){
7 X" r7 r$ |% L+ f! Z  z! Z, Y4 W$ m            System.out.print(aryA[j][i] + " ");  Z& K8 J! C6 p  [
         }
$ V9 V& Z2 \- d' \+ C/ f% A         System.out.println();5 _( y$ ~+ ~" i# g' k2 a
      }5 E. `+ E: w0 ^5 @9 T+ i. R
   }catch(Exception e){; I5 u' d; k, f! `) b
      System.out.println("請輸入數字");1 b6 T" V# @3 P) l, n
      e.printStackTrace();
8 G" J' x# y" f! l& [4 Y   }
9 L; i1 a  p9 g% L) H/ u' |: m# n7 ~- n
  }, x1 e) |: i; w$ m
}/ l1 f4 n0 B6 A6 o

8 G7 N' [. @4 l( K7 M5 B# V1 }# }
; r+ V6 s9 J0 K" }* j9 ?/ `# gTQC106) W# u: q# d/ Z# P( p
public class TQC106
& V' R4 N, g. n6 y) O{3 [5 P: Y* W6 X8 _2 H
   public static void main(String args[])
* F  @8 ]) u0 }1 d- Q   {9 O( h2 l1 O" U
      int sum = 0; 9 m+ H( c2 t. i1 W2 A: i
      int j =  0; 4 V, J1 Z1 L9 ~( S
      int k =  0;9 q) Y2 P* z6 P8 r/ u: j; f9 P- q
      for(int i=0 ; i<args.length ; i++){
7 E! H& @7 ^, s- l  }- w  y         try{3 L' o  U) x" l& Z
            sum += Integer.parseInt(args[i]);
: k, K' P" g5 {9 G            j++;, L& I$ O' A- R1 \
         }" @% \( R, ]2 T& u! P$ e
         catch(NumberFormatException e){# T* h) W0 O8 k
            k++;
# ]" x( E% e. n6 t$ r. p, K         }( I3 E* |+ f$ R6 w& Z
      }
* c9 Y1 u5 {/ B! ?      System.out.println("數值之總合為:" + sum );$ c. Z1 O" b: R; D+ H3 ^
      System.out.println("非數值個數為:" + k );
0 Z0 d* s5 x/ N! l( {& u      System.out.println("純數值個數為:" + j );
7 f3 L: @2 d3 x& Q" j2 o   }7 |$ [) @" c: H1 ~4 X/ n5 c1 Q
}

返回列表