返回列表 發帖

TQC104 ~ TQC106 答案

TQC104
' d/ k* `/ b( p% f9 o+ Vpublic class TQC104 {8 x8 D! v4 g, r" p( F+ K9 ^
  public static void main(String[] args) {) R; @6 ?. ?0 H' k
   int a[]={0,1,2,3,4,5,6,7,8,9};
3 i1 Z! p  o, P  J( ~   boolean check=false;
  F& y6 [8 w8 L" N4 Y$ }2 S! u" s   int i,j,c1,c2,c3,r1,r2,r3,x1,x2;
+ R4 o% f! G- }0 [+ Z   do{1 a& Z1 R$ g! l8 H; F6 l2 ^
      for( i=1 ; i<a.length ; i++){
3 p- t, P* `' @  F" D         j=(int)(Math.random()*9)+1;
) n' t0 e0 k6 t         a[0] = a[i];
2 u. }. J3 k/ A; v- G, [: m         a[i] = a[j];  P* Y* M  L& t0 m- D- A% ~
         a[j] = a[0];
& ]9 q+ H* p( T      }) l8 ?  Z) I+ p5 [1 v8 t

0 @! j) Q9 g! p  U' u, |      c1=a[1]+a[2]+a[3];
( c6 o( T5 l: N; N5 s      c2=a[4]+a[5]+a[6];+ `. Q, \0 ?8 ?- L
      c3=a[7]+a[8]+a[9];  r5 d& j5 U+ |1 q# S# g# `1 T' H6 ]
      r1=a[1]+a[4]+a[7];
* r8 e; \9 O; s      r2=a[2]+a[5]+a[8];
# X; N' N" _+ F& K# |8 b      r3=a[3]+a[6]+a[9];
0 y( g. i; i$ B( i8 z8 |      x1=a[1]+a[5]+a[9];0 ?  m5 W- l% i2 J0 N( |# y
      x2=a[3]+a[5]+a[7];
2 M* N. k$ i; a8 g5 ^& W      if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15
+ x# [/ F  K  ^0 x         && x1==15 && x2==15){2 S4 }- I, _6 d
         check=true;
9 \: Z  B, `2 o( {4 Z      }
& t' `, _+ j! U# T( D$ l( X+ m   }while(check==false);8 Q6 u/ [) H. o. @
   System.out.println("答案為:");
: m( K& X# G7 v/ s. m
% ~7 I  o4 q  h      System.out.println(a[1] + " " + a[2] + " " + a[3] );+ [  t8 I8 Z, B
      System.out.println(a[4] + " " + a[5] + " " + a[6] );
9 i2 _9 q6 C3 d; V2 U* S6 m- I      System.out.println(a[7] + " " + a[8] + " " + a[9] );
4 }1 |, p5 q8 D5 b
$ @2 P0 l4 F& k3 O   System.out.println("不論橫向縱向及對角線加起來的和都是:15");
% X: u, x% \- l" ]# Y" `/ }) v7 R  }
. J. U9 ]- W3 h}
5 i' q0 k4 F7 z+ n+ g- |  D/ p) Y$ a- x- u# T
  y- E7 s( q) }, m
) s/ `7 n0 ^& Y/ e0 o( _/ @
TQC105
. I% d7 L+ t& W/ Q  cimport java.io.*;
; e* h) F& W' ~) Wpublic class TQC105 {
7 Z$ }8 M4 I3 `% Y7 i( S
% F' N' g  l2 x' ?0 y& a  public static void main(String[] args){
# k  m4 p( G/ E9 v   try{* F" l8 V6 R( G# r  b
      int row,col;$ [, v6 G2 e& S: h2 z
      int i,j;
' c% l9 }- r* R0 |( r      String tmp;
" V1 v& \* r/ b      int aryA[][];- `* {1 A3 F! a' H
      int aryB[][];1 ^  S# y, S% C# X
      BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
; S! H* d: t" o# B$ q7 G  
5 a$ f- G- C! T- Z1 y) f: N      System.out.println("請輸入陣列列數:");
( e% P" A. d4 `' O% B7 g' ^: E      row = Integer.parseInt(bin.readLine());; P/ O9 p8 k" v5 \- F
      System.out.println("每一列陣列裡要幾個數字:");
0 d& b3 W# y% h& a8 I6 l) L      col = Integer.parseInt(bin.readLine());6 M6 R% _# b; ?% _0 R
  
2 r- E  m3 I3 K( z+ B* X      aryA = new int[row][col];) \6 m  R3 Y. k' L6 M6 \
      aryB = new int[col][row];
( K0 L& W( Y5 P: n2 v% w0 g    U# u* l) t! o( x# K$ W5 W
      for( i=0 ; i<row ; i++){6 S% f& s7 `7 q+ S
         System.out.println("請輸入第 "+ i +"列");: ]5 _/ f( U3 ~  @: w$ o- o2 q
         tmp = bin.readLine();
5 m) M0 G0 V8 {+ _         String[] strA = tmp.split(" ");3 j9 g: R/ R) m3 T# ?
         for( j=0 ; j<col ; j++ ){
. K# u  E; W7 h" r" c+ O7 Q: x* v0 x            aryA[i][j]=Integer.parseInt(strA[j]);
1 Z# z: I; N3 V6 L  v' G         }
* w; s0 D, n9 ^0 [# |      }
0 B3 A1 Z: f  ^8 Y+ i      for( i=0 ; i<row ; i++){
8 S' G' _/ e  i5 j2 j& c9 j1 [         for( j=0 ; j<col ; j++){$ K, f5 ~* Y( k. C1 K
            aryB[j][i]=aryA[i][j];- m$ ]* ?7 {( b( m6 N8 k+ q
         }( Z+ N6 y' ~) Y
      }' r4 m5 m* ~9 y3 x6 B4 S
  / j9 f  k+ S/ Z- g3 R8 B  D$ h
      System.out.println("陣列經行列轉換結果");+ c$ n4 y+ F& W" S
      for( i=0 ; i<col ; i++){
  D1 N- `1 L2 k2 t# S/ r  U         for( j=0 ; j<row ; j++){
& D% r9 Y6 F5 Z* v            System.out.print(aryA[j][i] + " ");( Q' a8 y7 b8 J8 _! t% N
         }
" C7 F/ |/ j; r0 C3 _         System.out.println();# H& r" x4 o* o. G+ E% w/ M5 b
      }
0 ]! m( G1 w" Q; y( H4 R) h, Y& f   }catch(Exception e){7 l3 U6 k% ?" q. @* t, b  q, {- O
      System.out.println("請輸入數字");
  U+ h  _1 n6 {0 o      e.printStackTrace();
* G+ C. P  ~# O' r   }/ |) P. t. I( h8 a9 a; c

  h0 H( e" t% H% m6 U7 i  }
3 t1 Z- {0 q/ X5 g}* Z+ r# H* Y3 x# \  w

5 n, K# d1 ~" _5 i, I & @6 Q3 I" V/ c6 U
TQC106: o" v$ f, J! f- y4 Q
public class TQC106, F$ |" @3 n" y8 O
{  q. `0 L' w& T, F' K. l) m, c
   public static void main(String args[])
. K: ]/ ?- B8 R, K: I2 t( r" k3 n) C   {& m' G% R; A9 g
      int sum = 0; # S9 J% s* B$ e. E5 a2 n% H  Z
      int j =  0;   B! N" b: l+ I5 ?# O
      int k =  0;: f, o0 V; q4 n0 @% t1 E* x" W; _+ }2 @
      for(int i=0 ; i<args.length ; i++){
7 C1 \6 ?9 U  {. |) F% T2 b         try{
, ~7 l+ ~9 D' |; L/ i  Z0 U" e            sum += Integer.parseInt(args[i]);
- c0 R1 I- w- J; s$ J* ~/ e$ w2 p% ^            j++;8 j5 G0 ~4 C0 M5 t
         }* ^4 \$ d0 t0 Z" Y
         catch(NumberFormatException e){
: p* ]9 b6 t4 |9 i# a            k++;% B8 n; @) t4 V3 @. k( G$ e1 y; S
         }6 q! |2 ?% n; A0 f& k% v0 h+ g
      }
& a7 h0 y" D4 {% Y/ |      System.out.println("數值之總合為:" + sum );1 }8 k. w3 }' l7 C
      System.out.println("非數值個數為:" + k );& {1 r. V- y5 v0 r# s8 `9 ^
      System.out.println("純數值個數為:" + j );- M' c# s9 u8 {  s- }
   }6 ~! F; w3 I/ x0 ]& l: }
}

返回列表