返回列表 發帖

TQC104 ~ TQC106 答案

TQC104
5 i& V5 }. y+ c: a' Vpublic class TQC104 {
0 C9 G- Q; N6 I9 C' H  public static void main(String[] args) {  k9 E" \3 h; o; T
   int a[]={0,1,2,3,4,5,6,7,8,9};
$ j6 Y! `9 l. S/ M6 J$ G! P! P2 _   boolean check=false;
7 ]3 Z+ `% q+ D   int i,j,c1,c2,c3,r1,r2,r3,x1,x2;, ~: p5 W( A! e
   do{0 p6 J! A3 k$ I4 R' T6 p' B& u8 A
      for( i=1 ; i<a.length ; i++){; V  C& x: b) Z  e. S
         j=(int)(Math.random()*9)+1;# V8 i( e, S0 Z. \
         a[0] = a[i];" `3 P- w2 \& d9 Z1 \
         a[i] = a[j];
4 G4 g$ U- s/ h. `# W/ ^$ N4 \         a[j] = a[0];
& x5 s" p* n, g, ^3 o      }$ o& |5 z9 O3 K" ^2 l& P, w
  c6 B% {$ ]: `. a; E  y. N0 B
      c1=a[1]+a[2]+a[3];, [6 i8 O! x% ~4 P
      c2=a[4]+a[5]+a[6];
& {& p- v( m7 _      c3=a[7]+a[8]+a[9];$ V3 F; a8 n! @4 y
      r1=a[1]+a[4]+a[7];+ {+ X! W# B& T: R. H' e
      r2=a[2]+a[5]+a[8];+ F  o: x3 V+ M0 M- \* r
      r3=a[3]+a[6]+a[9];
9 S: O0 b# S9 \+ k& j4 _      x1=a[1]+a[5]+a[9];
4 N$ K( z( r0 w8 e1 g0 r      x2=a[3]+a[5]+a[7];. q: i7 O, d& |$ E: K+ S, d
      if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==159 T" m: J( l- e/ P5 b6 j. v2 g
         && x1==15 && x2==15){
/ h8 ]+ B: t; W* n4 Z  O) v$ v* A1 A         check=true;$ a/ J8 T* _7 g2 f3 R0 `
      }7 |9 p- A2 D$ \) Y4 c
   }while(check==false);
1 j  R/ V: C* p" x' z8 w$ o2 I   System.out.println("答案為:");6 k* [2 S1 [& d# I0 j

5 d# @2 s+ ?; b- Q      System.out.println(a[1] + " " + a[2] + " " + a[3] );
1 y5 y7 B6 D% B6 U" Q* ]1 r! X      System.out.println(a[4] + " " + a[5] + " " + a[6] );. I$ K* D7 |3 k0 l7 w% }% q  f8 J
      System.out.println(a[7] + " " + a[8] + " " + a[9] );
1 p/ Z) b5 k) z0 U, S- Z' V% n' f
   System.out.println("不論橫向縱向及對角線加起來的和都是:15");/ M; \5 x0 m- F: l2 y) i1 G
  }
6 |/ N! J! P) Z+ L5 x}
8 l1 W4 w4 ~" ?3 n+ o5 H( ]% l* b
* t/ i4 Q% x) Q# _/ @8 k/ B
+ K' ^# E" @% l- g
8 \/ O" L- C8 y4 b0 QTQC105# G3 M' L  p/ v9 i6 w  l7 Z
import java.io.*;5 U. L& p! ~( Q
public class TQC105 {& i( x8 o9 ]1 Z9 L/ v

$ V4 y, i3 o/ ^; g  public static void main(String[] args){# k$ X/ J& X) ~5 a! L
   try{- J' I/ M+ n$ d4 q! ^
      int row,col;8 `: R4 m) V  ^$ m( i5 }
      int i,j;6 }# ~7 f& {' a
      String tmp;
4 S( c+ U- A3 ?  ~      int aryA[][];
" D& ?2 s9 V3 L- L; U      int aryB[][];9 m5 w5 ^. }9 L; j
      BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));+ W+ X) p7 [5 i# S! Q4 N
  0 e4 g7 m7 T( w3 q
      System.out.println("請輸入陣列列數:");9 P0 j0 b- ]0 s& s4 H+ d  ^+ y
      row = Integer.parseInt(bin.readLine());5 W7 H4 @: j4 }/ U8 ?9 o* n) s; \! w
      System.out.println("每一列陣列裡要幾個數字:");
( l2 l+ n; t8 c+ |. A* r      col = Integer.parseInt(bin.readLine());
+ ~4 r8 J; x1 s  
7 R5 n$ l2 k0 a/ q) [3 V      aryA = new int[row][col];3 }. n2 x2 C5 G- S1 S, A
      aryB = new int[col][row];
- }+ u' j' ?: ~' `8 T8 G, B  
2 i2 Q8 p8 N$ Q  U      for( i=0 ; i<row ; i++){
1 \3 u' m* F3 l  p: ?5 w( A# Z. V, r         System.out.println("請輸入第 "+ i +"列");
# @- s8 V+ Z8 r4 F2 ^         tmp = bin.readLine();
+ ?- v" ]  B* c& I) k* E" S4 j         String[] strA = tmp.split(" ");
: z$ R" p. [/ e0 z# I         for( j=0 ; j<col ; j++ ){
6 @5 R! a- {: a8 E/ ^            aryA[i][j]=Integer.parseInt(strA[j]);! B9 {5 O# l4 K4 I* q0 P, U
         }7 Z$ A# C! Q) _# k  Y, ]% o  b3 a
      }
  e9 z# N: p- X1 O4 T      for( i=0 ; i<row ; i++){% s1 L0 ^* `1 c7 s- ~) J9 [
         for( j=0 ; j<col ; j++){
6 a1 N3 q0 u* z) x# b            aryB[j][i]=aryA[i][j];
$ p. F1 ^, V6 H8 M         }7 M; Y  a1 E4 U( t4 k9 H
      }% X. e# R* q+ s! j4 l& T
  
* P: j9 I! L0 M3 U: C- M; x$ Q      System.out.println("陣列經行列轉換結果");
1 f' {$ Q; U3 Y5 ~7 j      for( i=0 ; i<col ; i++){  d& ?: K$ u2 [5 B5 |- w) f
         for( j=0 ; j<row ; j++){
8 J' ^9 q( U2 H6 H. E+ i1 f            System.out.print(aryA[j][i] + " ");3 k6 x0 ?" d4 G4 {7 u8 ~1 @
         }
/ q: z3 {5 t1 [# M; L         System.out.println();! f! |" x* `; K' H7 A; @
      }
) N6 e1 k6 [. d% E   }catch(Exception e){' e+ c' {- a6 h
      System.out.println("請輸入數字");
5 \5 O- t7 S  ~+ P% a0 W' r1 h/ y      e.printStackTrace();
  p& n: w1 n5 q, K; N* r4 ?3 s0 s   }8 `7 s! u4 x6 @3 ]  S
/ f4 D* i) {0 D/ `! F# F  w- Y
  }
6 }. G; |, y) {& Q% B- `}
) f) e4 ]4 W+ ?5 S: o8 ?
! ?7 Y0 U$ n  ]& \8 H - f* e/ x& _6 i3 [6 W1 H
TQC106
! H* G% F) p  U3 N8 spublic class TQC106; G1 L% |0 X3 \# k
{/ U; O! Q: L" n$ x5 a. N* Y
   public static void main(String args[])4 D/ c3 {: G0 ~4 f8 {
   {
; l1 U& ^7 X+ j; X! d      int sum = 0; 4 Z0 g' m! }2 c9 p' G% h2 q7 n6 K  Z
      int j =  0; * ~0 |, F1 ~- z6 Y' W: ~
      int k =  0;
" Q8 b, Z/ E, I! X      for(int i=0 ; i<args.length ; i++){- O$ [( N% y) A# S8 i
         try{1 |1 A4 t/ ?4 {* e& p8 X
            sum += Integer.parseInt(args[i]);
) y5 x  ?/ k/ M; `6 ~$ x            j++;
6 d0 F. ]+ r8 b  f0 ^) A+ J3 R, x/ D         }' g# h6 {* O$ J! c" F" F
         catch(NumberFormatException e){
- |: _' f  Y: r8 l            k++;
3 {' g: ]! X. r7 _$ I- X2 Z         }
% s) A$ C  V" b4 r' y      }
* _6 F  \5 Y5 @: m      System.out.println("數值之總合為:" + sum );
# p: n. u2 i/ \- n' H: v      System.out.println("非數值個數為:" + k );
3 l" j0 w% n$ ]9 y. |      System.out.println("純數值個數為:" + j );
! }' N% _  t' R% s6 x   }
9 ]! T) V/ {1 l( ]9 r" E0 e}

返回列表