TQC104
1 {6 S2 D( S9 hpublic class TQC104 {
4 z! S! \# B! }2 P C% U public static void main(String[] args) {! k* X. E, g' D7 V
int a[]={0,1,2,3,4,5,6,7,8,9};6 ^: g! [* S% L0 [+ U
boolean check=false;
. S. O+ ~" i+ [( q, T4 @ int i,j,c1,c2,c3,r1,r2,r3,x1,x2;
$ M. |0 W) P! o6 _ do{
9 D7 I0 a4 b" N5 ^3 J" h, t$ z2 ?( A for( i=1 ; i<a.length ; i++){
9 V7 {" Z* D! T, U$ k$ G4 z j=(int)(Math.random()*9)+1;/ C/ q0 d& H% v% Z8 o+ `7 O4 Q6 v
a[0] = a[i];" x3 ?" X) n' n d8 u+ Z) z6 s, z
a[i] = a[j];+ ]5 l: ` g1 I+ w4 J. |: q
a[j] = a[0];
3 Q. n9 N! H' \7 [2 ] }3 ]. j) t# a( X6 b
# L% L( w" W! v/ }' N* F7 c; R c1=a[1]+a[2]+a[3];
% G$ r. O4 F* p7 Z* n+ M c2=a[4]+a[5]+a[6];
; g" ~/ ]3 G/ B* ~+ F5 \$ o4 Y+ B c3=a[7]+a[8]+a[9];
0 _/ P% e' m7 n" _& c r1=a[1]+a[4]+a[7];1 r( {3 ?' a6 p5 R! x5 \" m% Z
r2=a[2]+a[5]+a[8];
) T2 e( Q, c% ~+ A6 q r3=a[3]+a[6]+a[9];+ n' F8 y5 G0 A& v; f; y
x1=a[1]+a[5]+a[9];, r# T8 ]( t$ _$ c
x2=a[3]+a[5]+a[7]; P7 J5 l( Y* E& I( j
if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15
4 l. }* V! D# g! V) L2 j && x1==15 && x2==15){* `2 d) S9 a% x5 E
check=true;) H) U* |# }+ t( g+ r8 C+ \
}
4 y0 n& ?- @5 I; k; z4 E+ I }while(check==false);& g; ~- s) O" X3 p. e/ G
System.out.println("答案為:");( I8 f' H( N3 }, h, W/ Z0 u
0 S T3 T- J% c% X b5 {- u System.out.println(a[1] + " " + a[2] + " " + a[3] );" o d+ O. @, U1 D( p
System.out.println(a[4] + " " + a[5] + " " + a[6] );
3 l( v+ x- _& j, w System.out.println(a[7] + " " + a[8] + " " + a[9] );" `% X, p8 d( X' I, H! Y1 [! j- ~% m
: z9 ]% ], K* [4 G System.out.println("不論橫向縱向及對角線加起來的和都是:15");) h! T# s7 T" Z$ e
}. ^. _7 L; ~5 L( r2 h- v$ `1 {
}
* N2 _5 B8 B% b0 V3 |
2 Z2 J( M2 C, z4 ^6 w
6 ^( U+ D& `; j/ N: m8 n9 X7 q6 ~4 ]2 C7 S. G
TQC105! v; z% Q# q6 p2 s3 t, m8 N! Z
import java.io.*;: l! \8 P) e( U& i
public class TQC105 {
2 h' d5 N3 r4 |7 P# v' A; r
% F' x( L' {+ `* L9 C6 g7 U, c# x public static void main(String[] args){
3 B7 Q6 t# O- h) |# k8 a try{
9 z j6 z1 Z- r7 O( l# ^ j int row,col;3 F7 V- ?2 ?* j, V. V5 R
int i,j;
0 A% Z* L7 U8 b$ ^* b5 A String tmp; ~$ @4 e [/ V& n
int aryA[][];
+ p- S& z" ~8 t int aryB[][];) g% ]1 V9 m- ^& T# C
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
* ~1 c5 A6 g7 K
% N& p& s5 W7 {+ q$ T$ y* R0 w System.out.println("請輸入陣列列數:");
3 e, O; J$ I; ~ w2 M( q4 d row = Integer.parseInt(bin.readLine());0 f9 I: o6 K2 {5 v) O1 o+ C
System.out.println("每一列陣列裡要幾個數字:");( v2 Q! t) W. u
col = Integer.parseInt(bin.readLine());- p2 Z8 t# T3 b# L3 q) c& N
) A* I0 M# Z5 b# l: H$ b* Q" g' I aryA = new int[row][col];: }0 w, e9 z- n5 l
aryB = new int[col][row];4 ~5 N2 K R8 \; J7 `
+ D/ Y. A- V3 \% _* e1 H9 b
for( i=0 ; i<row ; i++){
5 N) L8 F5 `' Y( c% u( x3 s5 } System.out.println("請輸入第 "+ i +"列");9 t4 J$ L4 p5 w
tmp = bin.readLine();
* w3 |( E" z) g! v2 K3 M' B4 z String[] strA = tmp.split(" ");
0 c) f D/ _1 C2 {' O# L i5 S: S for( j=0 ; j<col ; j++ ){
p. v/ t( L" J aryA[i][j]=Integer.parseInt(strA[j]);) x; P* q; s1 _1 { R# ~
}' C* e7 X7 d* U8 k) y. ?
}, B/ B8 K- x" @9 i, T
for( i=0 ; i<row ; i++){! c r* i! A- F) {2 G
for( j=0 ; j<col ; j++){
( I0 a/ K, o3 A" B aryB[j][i]=aryA[i][j];; d3 q/ E5 C4 j- K& q( s0 ?. g0 L
}
* A# A- l: z7 ?( i* T. D: I- N }5 R. l/ z8 x( _5 S
* B/ K# k4 M$ E7 a/ x
System.out.println("陣列經行列轉換結果");/ m# h, p5 I" p
for( i=0 ; i<col ; i++){8 y; k% j+ a3 _9 G
for( j=0 ; j<row ; j++){
0 Z* f7 T- z# x) c8 ` System.out.print(aryA[j][i] + " ");
' G% b% Y- _/ ^ }
% n6 h+ a9 P! G1 ? System.out.println();
8 _5 A( j! y# q; Y; g2 e$ Q! E" i }
' `% K0 P& \5 i# h6 x# m( | }catch(Exception e){ U9 h/ q( o/ c8 [/ D
System.out.println("請輸入數字");: H' t0 | \; {( n
e.printStackTrace();) D ?1 J7 Y' [" V2 I5 q
}
) R' n* e' {- C' i6 [' H; T! w% b5 U
. F% \; L& X' p! |+ R' n$ b! h }/ b0 e: x* y* X# o& D/ I% V
}
! K u9 h1 i! `. ?1 ~
) _8 s5 U8 z7 }" b/ ^ & c2 k4 Y+ t6 H2 F
TQC106
) f, f- |/ p$ a' m- Xpublic class TQC1066 Q- u& L2 j( y( D! c: O* F9 @' ]
{
) P7 {; B& r. A: k6 Z u% w/ L public static void main(String args[])& o. H% V( d! @
{
1 {6 {% ^# S/ F4 U% A I int sum = 0; # E) T" D2 v7 Z' V
int j = 0;
- Y! I* A; }* b- B int k = 0;' j" P" p: `% ]2 ]5 b
for(int i=0 ; i<args.length ; i++){
. I1 Y- b! ]1 t; V+ c! }0 B try{
5 l6 [# w* u g sum += Integer.parseInt(args[i]);' R; w/ ]( f' s- S' K
j++;' X4 j# m) L: D) V# |6 k- ]+ D" w
}
9 k C6 H7 j7 E, b. d0 V( { catch(NumberFormatException e){
2 @8 i- M$ |5 H k++;
+ o, d" | T$ ^ }' Z9 E' D: Z) w @) W& Y5 J
}
$ S2 J7 m. f. B( u& h! o System.out.println("數值之總合為:" + sum );: C6 c2 A4 o ]) h7 Y; O
System.out.println("非數值個數為:" + k );: e5 r$ @: d# ]- ^$ D7 ^
System.out.println("純數值個數為:" + j );1 ~6 w, h; f5 s% W- n% |. G- [, _% v
}0 D, a) M4 M0 M/ \2 B
} |