返回列表 發帖

TQC104 ~ TQC106 答案

TQC104
# [+ v1 T9 [: `* |$ j, j: ~public class TQC104 {
; W! `$ x0 x8 P( G9 z" i: ?: a- B  public static void main(String[] args) {: l- m2 b1 Z  T5 O
   int a[]={0,1,2,3,4,5,6,7,8,9};: u2 e5 {; f3 `' A
   boolean check=false;" r, C0 {6 K/ e- W( n2 w
   int i,j,c1,c2,c3,r1,r2,r3,x1,x2;( t& P4 o" C& ]' h
   do{
& e7 K7 `3 y4 T; e      for( i=1 ; i<a.length ; i++){4 T6 D1 Z# q5 T4 }0 D4 i
         j=(int)(Math.random()*9)+1;' m0 E# Z4 M. M; m$ A" Y
         a[0] = a[i];" O: g% v2 H7 H% p  a
         a[i] = a[j];. [2 v/ j3 T* u8 ~4 L5 j
         a[j] = a[0];
" Q5 B7 j* T: E" |5 r3 e8 F) E      }6 o4 P$ ?" A  t# I1 R3 {
8 V- Y: J8 P8 u& o7 K0 ^' i" J4 R
      c1=a[1]+a[2]+a[3];
: N0 Y& B! m& T( ]      c2=a[4]+a[5]+a[6];
% Y3 q+ {* }! H9 s' C$ }      c3=a[7]+a[8]+a[9];
$ d% h) Z& O% @2 h      r1=a[1]+a[4]+a[7];
7 I+ o5 [7 w2 G# j) f" l2 a      r2=a[2]+a[5]+a[8];
7 l) F0 @( j3 @- S6 o3 g! \      r3=a[3]+a[6]+a[9];
3 I( `7 l) T1 C  d3 \6 Q( h# w      x1=a[1]+a[5]+a[9];3 e0 Y" M- L; D2 T" }2 Z
      x2=a[3]+a[5]+a[7];
2 ?) z$ @9 t5 z) `/ A) J9 ~; J      if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15$ R! T( m6 t+ @; t
         && x1==15 && x2==15){: I: \$ p  H- F1 \0 v% U* O
         check=true;
8 r! R/ e+ _7 ?' V/ A# ~2 {      }: H. l2 o* i/ q- J
   }while(check==false);6 k. ~# u5 x( J
   System.out.println("答案為:");* @2 n. i, e! Q$ q
% h. B/ ]" B6 Z5 t; I1 E
      System.out.println(a[1] + " " + a[2] + " " + a[3] );" ^6 D( I# s  ^  C. W
      System.out.println(a[4] + " " + a[5] + " " + a[6] );
- L& d# g5 t' ^& s1 s: S  P% u      System.out.println(a[7] + " " + a[8] + " " + a[9] );
* s6 o3 b$ `2 v, U1 [
* P3 J! J% i7 n   System.out.println("不論橫向縱向及對角線加起來的和都是:15");! _) @$ C+ y/ j5 Y0 I
  }9 B2 O4 j" \) Q7 Y0 }+ f1 a3 I
}
0 _1 l6 n9 j. f; p- U( g6 t" U! K# K% b1 ]% n) _4 N( ], B

. b" V) t" ]6 c1 Y% p0 U0 o- k/ }/ A- B/ e& h# v) [
TQC105
8 Q1 W/ {8 [- c, c; o2 j1 nimport java.io.*;
, h7 r* z2 H: Mpublic class TQC105 {
5 l/ f$ u8 T' q8 U0 J+ J$ v* @8 v
  public static void main(String[] args){9 u4 ~+ m9 ~: C$ J9 n
   try{' C  s3 }( d" t
      int row,col;
% {8 `8 {1 S) I- d4 D) A      int i,j;
. Y7 {" C3 q5 `1 Q( i      String tmp;
4 B+ c6 s4 O+ w" B0 `      int aryA[][];
1 x/ c1 L  t  y: I! ^0 L: ?" o      int aryB[][];* t1 k  g$ y& [' l* ^& }0 {" Z
      BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));, l4 Q! Z; r7 \- M
  ' K7 \# w9 x# ~
      System.out.println("請輸入陣列列數:");2 _( Q8 f, ]5 [: \; Q
      row = Integer.parseInt(bin.readLine());  V) o5 ]. l7 f9 ^3 K
      System.out.println("每一列陣列裡要幾個數字:");
) w$ N) z; L, ?9 k2 A4 U! o      col = Integer.parseInt(bin.readLine());' @2 \  l  x7 X8 P" e3 p
  
; ?; T9 D3 }* F9 y3 q      aryA = new int[row][col];
4 r" O- N& _8 p- I: x7 C$ Z  T      aryB = new int[col][row];
4 t1 d  O7 c6 E* T( r  + s" [3 e/ D+ |9 Y* S2 ~, O& ]7 L
      for( i=0 ; i<row ; i++){8 N. r/ S% q( w
         System.out.println("請輸入第 "+ i +"列");# U/ \, F7 g% [1 s& Y3 p
         tmp = bin.readLine();) Q1 O9 L; g9 L+ V, Z
         String[] strA = tmp.split(" ");2 e* |# h" s: r
         for( j=0 ; j<col ; j++ ){% n+ }1 d; b+ O$ [4 O- A
            aryA[i][j]=Integer.parseInt(strA[j]);6 {( o3 J2 y3 L! p6 w( Z
         }; `4 o7 B+ ^, L( Z% @# [5 J
      }- o/ ~4 U' S; c. X" H
      for( i=0 ; i<row ; i++){3 s& W' p4 s) d4 j( b
         for( j=0 ; j<col ; j++){
5 J. ]8 ]2 U2 Z) F  X, x+ i" a            aryB[j][i]=aryA[i][j];- b: V/ T" |' {) r9 M4 Z2 z
         }
* M3 \7 N# Z8 \0 \6 _. W6 M4 [5 O      }& I3 G3 J2 U/ K* L, ?/ L
  / ]9 j5 ?: F' G8 V3 a8 W
      System.out.println("陣列經行列轉換結果");
5 B' H2 j0 z: r: u: b1 u      for( i=0 ; i<col ; i++){, x4 t. e$ K: k0 o& _
         for( j=0 ; j<row ; j++){
+ g% o& Z% c5 \9 n% h9 \1 E9 J            System.out.print(aryA[j][i] + " ");
3 k& x$ U. S4 c, r  [" Y% @  e2 O4 z         }2 \6 U3 Z: V& S# ~5 s
         System.out.println();
8 a: s6 x) v# u6 b; u      }6 j' |  t3 ~" A- {0 L/ l
   }catch(Exception e){% X; l" L2 S. J  S
      System.out.println("請輸入數字");% P7 v3 S; }' s9 C) a+ d% I* \7 D% y
      e.printStackTrace();
& ]  X/ B4 b& C# ~! F   }7 |2 m( [2 i1 d) y& w' q
0 T3 B( h( \, ?$ P: p1 U# `, D, A
  }& @0 z- P# F1 \$ H5 F# h+ k/ _/ K
}% g( P* m2 o+ ]  H  Z" |

! W0 ?7 g6 y3 m& E0 C % l7 ^. j6 U8 E3 f/ o; [+ U
TQC106
' ]8 O0 I7 A6 J; [3 {public class TQC106
. ~; c! N# |% l% L2 W: x{. Z0 _$ Q+ o7 G
   public static void main(String args[])
" Q- c/ x$ n6 X   {
3 u& X( b  `; W      int sum = 0; : h6 W% D1 C8 {7 p
      int j =  0; ) f1 [* r4 V% }) m6 Z
      int k =  0;
$ C; x  _* |( K3 X& k1 h/ f# v      for(int i=0 ; i<args.length ; i++){
8 |, Z4 @; t# p& x' ^. F         try{0 y) ?0 q0 J1 C, _9 l9 ~
            sum += Integer.parseInt(args[i]);
; o& [4 l5 Q; B) ^( ?            j++;
! e/ Q$ N: _5 a! M7 [         }
) J8 L1 {( I. S/ ^9 O% T         catch(NumberFormatException e){
2 E0 A' B/ Y9 h" Q  r            k++;; U! n$ Z/ e: p/ b
         }) v) @3 v! O/ Q1 P/ o& ]5 Z
      }/ i1 J) l8 s" S& v2 o1 Y& s) d2 Z
      System.out.println("數值之總合為:" + sum );
( j" X/ f3 a. Z3 U8 `5 {9 @      System.out.println("非數值個數為:" + k );" q) s) A: y; [6 T1 Z2 Y% D$ ~
      System.out.println("純數值個數為:" + j );7 p- V; p. r$ f& Z- |' n, l
   }
& _3 ?% z1 ?; S$ c% C. l6 F3 R" P% L}

返回列表