TQC104' V e# q9 H$ @# @
public class TQC104 {
; D4 M) V# l2 K5 Z; y- d; w, H8 y( v public static void main(String[] args) {" X( K8 v7 E9 i. Y# P7 m
int a[]={0,1,2,3,4,5,6,7,8,9};
! S4 ~; f4 X( q& W- v7 t( r" b boolean check=false;
* ]) K6 x4 ?; W int i,j,c1,c2,c3,r1,r2,r3,x1,x2;( V' R! b- w0 T+ `( [
do{* r4 `2 D3 ]4 P/ h9 w7 N9 l1 n, v
for( i=1 ; i<a.length ; i++){: x5 M+ Z& T0 N- t
j=(int)(Math.random()*9)+1;) ^4 P6 C2 R* O! Y6 v7 ?. q
a[0] = a[i];
4 J; b5 ^" ~3 Q# o a[i] = a[j];
' Q3 E8 U! t& d7 k( i a[j] = a[0];
; C6 A: G2 d* L# f) D }, N1 H- y) o+ p# L
- k# |9 d8 Y1 s9 p! o0 |
c1=a[1]+a[2]+a[3];
# X" {0 L8 `' e1 A+ x% E8 L3 q E a c2=a[4]+a[5]+a[6];
# A1 m7 |( ~) o9 g& z) D9 ^" ? c3=a[7]+a[8]+a[9];
0 X/ d Z5 r; I) G& I5 p8 s. ? r1=a[1]+a[4]+a[7];
- K& S2 c' n2 c r2=a[2]+a[5]+a[8];
" X3 b p, n0 y2 m r3=a[3]+a[6]+a[9];+ c) \0 g1 t. K1 _+ x- T6 j* p
x1=a[1]+a[5]+a[9];
9 t/ V5 ^0 s3 L x2=a[3]+a[5]+a[7];
9 T! N9 Q1 g/ f5 K7 W% a5 S+ E if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==159 c0 @/ w4 w$ g) I o
&& x1==15 && x2==15){
* x: i2 D3 O2 Y- d2 ^ check=true;
3 N& |9 e$ I; G: t }$ ^, c0 f& ?6 F% S/ B- W
}while(check==false);: e8 n7 J/ z" r3 G2 m9 s! `/ ~, p
System.out.println("答案為:");9 R. _2 ?9 }; \
8 B5 R6 [8 x4 J6 _) M+ P; S6 G
System.out.println(a[1] + " " + a[2] + " " + a[3] );& J2 H9 y% O8 e; k
System.out.println(a[4] + " " + a[5] + " " + a[6] );: E: `! W& F. i9 O
System.out.println(a[7] + " " + a[8] + " " + a[9] );
# k- w3 h# v! F8 J3 [, G
& F1 W# a8 p3 P4 Z- D( C& v System.out.println("不論橫向縱向及對角線加起來的和都是:15");
( u) G1 {# P, C }% p% Q8 c. q) F3 q
}
* L0 v1 h- B' T: U1 u7 u" A" z- r$ T) B8 i/ J( Y# U6 x5 W
/ j! Z @6 [7 k/ w) [; P5 b! A" W: p4 a) j0 j4 _) r
TQC105
$ u/ c3 q4 a. Z8 ~import java.io.*;
/ A! G/ z* G$ a8 `: Cpublic class TQC105 {
) r& _2 k" ~% b1 w
9 N- l# M% W3 F, r7 o% o/ x6 }, J public static void main(String[] args){
: J8 x! } ?, F. }! D f try{
0 K3 `, B: n) A" I* L g, c int row,col;* J( Z2 F" M; U# t D) [% T
int i,j;6 K+ Y h/ C, k0 X( p9 e, A
String tmp; R$ K& c( J; S; q$ x& Q
int aryA[][];! }4 ^7 c: T3 E$ c6 h) V
int aryB[][];+ \% q+ z7 C2 L3 Q; q' t
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));9 _; f. J9 }( x3 o9 t% d
; m. U: N6 C+ P$ K5 M
System.out.println("請輸入陣列列數:");
$ Z9 M" G8 `1 X. f row = Integer.parseInt(bin.readLine());
8 m l9 ~" z: z. g System.out.println("每一列陣列裡要幾個數字:");( i" Y( C3 B) ]) Y4 ~
col = Integer.parseInt(bin.readLine());* Q' S5 f/ n/ R2 }" u
6 L/ c% s; P1 X
aryA = new int[row][col];' Q4 {8 A/ q, v; h, J$ X# C
aryB = new int[col][row];
, p/ [, a8 R; _# x( I ! \) F6 |* T- z7 T
for( i=0 ; i<row ; i++){
4 g* p% l/ [2 c/ j# q System.out.println("請輸入第 "+ i +"列");
5 z" k$ r/ [& L% m tmp = bin.readLine();
/ _) D; N% M+ D- c% \& U String[] strA = tmp.split(" ");
+ p. v; G, k$ {5 S' U for( j=0 ; j<col ; j++ ){- V+ k" w; G8 m# V
aryA[i][j]=Integer.parseInt(strA[j]);
6 M: V0 ^" ?, ~' Y: e }0 e3 v' ?8 G* s! s) [
}5 i- g9 K: E: p7 `# G# t; B( A
for( i=0 ; i<row ; i++){4 G1 x1 y- |9 `3 c, V+ x
for( j=0 ; j<col ; j++){
6 j! q. M( s. N7 q# w5 ` aryB[j][i]=aryA[i][j];/ r- }+ C. [- q' K$ ^
}
1 c" C# E+ x( } b, H }
4 h9 L; g+ `. ]- \: T5 s 0 T; s* `0 `' n& v1 a
System.out.println("陣列經行列轉換結果");1 {2 X- g+ @, G+ A; d: V$ j
for( i=0 ; i<col ; i++){
, V, r# w$ B' K/ ?! w2 Z for( j=0 ; j<row ; j++){
$ r7 |3 Y$ a! E8 \0 J System.out.print(aryA[j][i] + " ");! C) V+ a$ c$ {# V5 J, B
}
. G/ E b2 Z4 i2 T System.out.println();+ T* G* T0 `( @/ |+ J
}
$ i9 M% S0 v* Z9 H }catch(Exception e){
- J( i" v# i% G( x) @ System.out.println("請輸入數字");
& \ \, d+ y) }4 H8 P e.printStackTrace();+ b$ L- G% u5 z. c ]0 V
}* W+ V6 X5 K9 G% w* F- w4 p* E! E: j N
! d0 H! V( _! T
}
4 P! w3 C Q# ` W: J8 d- u}
9 a( N. \: W- ?% ? B' ~+ {9 H7 i9 w7 P0 h4 H9 u
8 F/ z+ W( p9 z. gTQC106
2 {! ?! k( R9 K2 [8 H# Tpublic class TQC106
/ L" _7 j! ?' K( z{
0 K9 \# |8 h. B9 C% k' [) J# [& Z public static void main(String args[])
/ Q& w6 U c* P: a {* f/ o x: Y. S# A c3 G
int sum = 0; 9 E3 W! X9 T, [. \6 G
int j = 0;
3 d- V1 J* F9 T+ K int k = 0;, M6 `# G6 j9 y8 `# ]7 `5 `
for(int i=0 ; i<args.length ; i++){
' B; O2 u! ]$ J, T try{
0 L4 W2 _9 D, d' V4 s sum += Integer.parseInt(args[i]);
/ \- R0 {* s7 Q7 f j++;
* p3 j0 U$ c% m6 H3 r1 c }$ h2 s9 M+ Z2 a6 F: o8 e# O+ [/ ?4 @: i
catch(NumberFormatException e){
7 k, X5 ~% |3 c8 K k++;
" s1 }4 p& {# l( m3 Y% Q2 t }
$ t' `* H9 q) j# a, r# Y" S4 y$ O; m }
4 ]0 `( Z' W8 L, P' m) S2 I/ j System.out.println("數值之總合為:" + sum );/ _, y& s$ Z6 p k" ?
System.out.println("非數值個數為:" + k );8 v7 H: e L1 p+ [2 x
System.out.println("純數值個數為:" + j );% W' m1 t9 z: h2 X
}4 J. M; |$ ]: \2 d
} |