TQC104$ g) k4 }, X- c: ? p# c
public class TQC104 {
% T: A3 Z! U$ C& N( M$ r* ?9 c6 @ public static void main(String[] args) {
4 Y* |' r! X" r) m, |9 y int a[]={0,1,2,3,4,5,6,7,8,9};
" k1 |/ v6 F4 p1 j- l! P1 k boolean check=false;
1 F* T6 R* A7 h1 z int i,j,c1,c2,c3,r1,r2,r3,x1,x2;4 s7 N; F6 z; n9 v% H/ Z% [
do{
5 F9 p$ b' S/ |, _/ H! g for( i=1 ; i<a.length ; i++){: o7 v6 x1 n) Z* m" T
j=(int)(Math.random()*9)+1;" l; B. ]1 O$ w# U1 }# I+ T, d
a[0] = a[i];
& h9 d" O: {7 A8 k) K/ T a[i] = a[j];
% X! j( O% H. P* E7 j4 J/ e# V a[j] = a[0];# x9 T1 _" Y) Y% S5 v3 n) s/ D
}6 l) ^$ G1 o/ b+ ~6 C
: a( c+ I6 N1 v( }* R c1=a[1]+a[2]+a[3];7 x9 r. q- @ s- X m
c2=a[4]+a[5]+a[6]; O/ _$ N) s; b# V* R# B% ~4 K' D
c3=a[7]+a[8]+a[9];
; C7 z3 @" Y- l2 I4 Q4 E& Y+ s r1=a[1]+a[4]+a[7];% w2 N# w/ G' ^
r2=a[2]+a[5]+a[8];
! @8 V, l: C4 W/ u r3=a[3]+a[6]+a[9];, c" J/ a2 U; @3 f8 O7 R3 L" {
x1=a[1]+a[5]+a[9];
, `. _* }0 Z; |/ r' F* p x2=a[3]+a[5]+a[7];+ D/ q/ c: W+ Z2 T1 O. A' r
if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15
' e# M7 Q/ a2 K( `; T6 Y2 G && x1==15 && x2==15){
9 B7 C) i6 b4 t7 u9 A check=true;9 R6 [. w& m8 R
}8 w( B+ X% t) c. @# q
}while(check==false);: f1 i+ s$ D! m g6 J) f) n8 n" P
System.out.println("答案為:");! Z( O6 H9 O/ {# w& f& v
6 p- T- H& M N; f3 i, z
System.out.println(a[1] + " " + a[2] + " " + a[3] );
0 `) a6 @4 x1 a/ T) b8 f System.out.println(a[4] + " " + a[5] + " " + a[6] );# O; D$ o% I8 x7 g- C* [4 r) m% _4 ^
System.out.println(a[7] + " " + a[8] + " " + a[9] );+ `8 Z1 W( X. y, H' \
/ @0 d/ X+ l8 f6 m# e
System.out.println("不論橫向縱向及對角線加起來的和都是:15");4 O2 m& |6 t; A
}
* |0 k3 X9 Y0 X1 F}# ~+ O. D7 D& o3 s, _3 J
1 e9 ], N' d" K5 c6 Z# e
8 e1 w, u' I4 ^7 R6 l
' O8 {! K1 u/ M; L7 D- ~ jTQC105
, v7 w% H) l, n# r5 ?! v( `import java.io.*;# b; H# o" v2 G- j& W0 d
public class TQC105 {
3 |3 f2 D2 r1 N H* [! p p% D6 }6 [& u; K; N
public static void main(String[] args){
) K+ E2 E% c5 {5 {' y try{: U2 y9 x5 O& y( |
int row,col;6 g' k( w( B. c6 D
int i,j;
$ D5 Y& b( ^' o5 {, w, Q String tmp;
9 P; a# _! V# ?8 Y, B' {+ F8 q int aryA[][];: N5 O. Q2 ~ e' L8 n: Z
int aryB[][];( m: L# q/ p! d' y0 I* Q& _2 Z
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));2 l5 e1 G1 f+ X2 m. A. N$ D( r
+ l5 B4 b3 s3 g! i4 N System.out.println("請輸入陣列列數:");
: c) U. U4 N4 i, A; ]$ W0 V row = Integer.parseInt(bin.readLine());3 y5 h2 {( X5 ^3 R6 u. J, p
System.out.println("每一列陣列裡要幾個數字:");5 i6 U* P) M, V+ R F4 f
col = Integer.parseInt(bin.readLine());
g1 z, \- v e* E( N3 [ 8 c! A* }' Z/ D
aryA = new int[row][col];1 Q/ ~' P/ O- T' `8 U7 O$ E+ C
aryB = new int[col][row];
+ B5 Z" |3 d& ~: _, u2 S0 K
% v( w( g# H5 u* i3 J for( i=0 ; i<row ; i++){
5 e+ u% x; P, q/ G System.out.println("請輸入第 "+ i +"列");: X R* A/ v3 ~2 e) j
tmp = bin.readLine();+ T+ a6 {& I0 S& e- g# d. o
String[] strA = tmp.split(" ");# V& X* _3 a Q" E! F+ g8 `! Y
for( j=0 ; j<col ; j++ ){( h8 `: v) z, R9 }* k% Y
aryA[i][j]=Integer.parseInt(strA[j]);; S- O1 Y* k( M
}
# x+ v4 g$ d# U& F# ^ }6 M% p- m3 P* L
for( i=0 ; i<row ; i++){
; [: e/ {2 ?( @. V' v5 i for( j=0 ; j<col ; j++){
# K+ `7 v1 p7 J# q5 I aryB[j][i]=aryA[i][j];$ h5 N/ R6 s& v: V& \3 S. {0 p7 n
}, E) H# U8 s$ g3 w2 x$ ?- t; \
}7 p& ] C. d9 k3 L* F. V
. I2 c7 c# c2 ]" V
System.out.println("陣列經行列轉換結果");4 G- o9 P' ]! w& O
for( i=0 ; i<col ; i++){
) L' w' T2 d/ A$ y4 a. d for( j=0 ; j<row ; j++){
% O" Q- E% g2 ?) i: I) a1 e System.out.print(aryA[j][i] + " ");
# D4 h5 Q" g" E3 v9 @1 p }
5 p3 s; Y) }! U* ~% R, M System.out.println();
8 y! P8 b- E0 w% {, |* t8 @ }' {0 @$ c7 J% q. p
}catch(Exception e){ o7 l0 J# K R% g5 `* |0 W
System.out.println("請輸入數字");
) [2 d" G. r* _1 p5 |, s e.printStackTrace();5 i' @4 u7 U3 H( g% t6 l
}
8 U& S& ~9 ?5 y6 j0 ]# A/ h' F% N4 r
}" O, D j" H/ n/ Z+ S
}
& a. w. l, P. p! r f* c
) [5 \; U5 K- o' V
8 k/ o I" G; TTQC106
- ^$ l" f6 \2 f0 Jpublic class TQC106/ ?5 i5 n8 p* g9 c) v
{
% l1 ?+ x. Q; A5 D) f D public static void main(String args[]): M" Q7 k) x: v
{
7 q: i, n( U7 J- i0 Q" `# i3 L* i int sum = 0; 6 m" N1 O& e, k `5 Y1 f! R/ h
int j = 0;
) N- E) P7 c4 T int k = 0;
9 o7 K e" O0 ^* t% k for(int i=0 ; i<args.length ; i++){
) O* D% T# V8 p2 r7 c9 m; Z try{3 M- p5 Q0 [0 h. r5 H0 s
sum += Integer.parseInt(args[i]);9 z/ R& s o0 U Z5 u, o
j++;
- w- y" j" ?( X. B- i& h }
[" E, Z7 p: M/ q0 }$ Q: x8 Y+ O catch(NumberFormatException e){- a: s+ X* H6 T# c
k++;
+ G7 T- b3 X" @( a2 J }3 i! A- t/ _; S, P3 p
}! ?+ p; K: h5 R+ O; Y3 N; W7 H
System.out.println("數值之總合為:" + sum );
2 m$ |& v4 Q2 o; ^( N System.out.println("非數值個數為:" + k );5 X! E; ]6 M( \% R7 v3 J
System.out.println("純數值個數為:" + j );
6 `. A5 b8 e# F G2 D* v- P! C } p/ a w3 y* N7 @
} |