TQC104
1 _" @" B ^3 f# s+ y+ n; j( zpublic class TQC104 {
$ d( W# _2 d$ q- L$ S public static void main(String[] args) {
6 r" m. k; |7 _1 } int a[]={0,1,2,3,4,5,6,7,8,9};
" k3 y) o/ X, h+ Z0 y0 p# C boolean check=false;/ D0 M) G. c, p R
int i,j,c1,c2,c3,r1,r2,r3,x1,x2;
, p3 c' |8 d% _" @% d do{
$ v6 M; H) ^ X ? for( i=1 ; i<a.length ; i++){
& s) y! F R/ S5 A$ {0 W j=(int)(Math.random()*9)+1;. e/ P2 p# i" [3 N
a[0] = a[i];
: B S& N9 w% D+ E4 A9 D- B* K9 [ a[i] = a[j];
j& `5 n8 T' W8 \ a[j] = a[0];* G3 w8 E; t# ?0 }' G% f$ }
}' Q1 u0 n9 ] w6 i1 ^) Z$ J
" i- n, B# ]+ [. O c1=a[1]+a[2]+a[3];' ]* a1 M' n& `- y0 N8 g& E/ p! ~
c2=a[4]+a[5]+a[6];
5 q) {; P# Y) {9 r$ G c3=a[7]+a[8]+a[9];
6 q4 D6 N/ ]! O/ [4 b6 D r1=a[1]+a[4]+a[7];
. i: H" V+ e& b; o$ ]9 g r2=a[2]+a[5]+a[8];6 ~8 V" m0 G7 x0 I( Q
r3=a[3]+a[6]+a[9];0 {( ^+ _5 s* N2 `* _4 R
x1=a[1]+a[5]+a[9];; [; p3 ]: Y& ]: b8 |
x2=a[3]+a[5]+a[7];: T% g( H) p4 |+ ]2 O- @3 E
if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15
; I0 e# q1 Y# w && x1==15 && x2==15){" U% i+ D% N. s$ o0 y0 r
check=true;
, G" I8 j* L# t& k" u' W, P4 ? }
. U0 A* q' ?0 h" F( {4 { B$ J }while(check==false);1 e: m* f- `' p* f
System.out.println("答案為:");
8 }& Z$ [ Z- U$ y
% Q5 N! x [* t5 \9 o System.out.println(a[1] + " " + a[2] + " " + a[3] );
5 l/ S/ A& O5 p+ _5 h) ~ System.out.println(a[4] + " " + a[5] + " " + a[6] );* a* w( ^* C0 q' R
System.out.println(a[7] + " " + a[8] + " " + a[9] );
) |2 G) s$ }' p a# o) T3 G. w- e& ]7 G% x* X
System.out.println("不論橫向縱向及對角線加起來的和都是:15");; w* l6 p5 D" n- e1 e. X I
}5 S7 _( E( v# J# I* Q4 s1 ]
}( n F6 r9 h' @: \: E% _
7 F" L* w. ~6 |6 C& ^8 e7 A& Y% k8 G4 n& r
l( ]- Y* I' {; H, n+ ZTQC105' W$ [: E' u$ t- ^& p, p. s
import java.io.*;
0 A& B" D* _" t& v8 h5 k. O: k' o5 {public class TQC105 {
& a- S( e! ~+ R, q) s4 Y5 `7 G) B2 ^8 |& q/ [& T+ N8 o/ j
public static void main(String[] args){
( {% h7 {3 a* I# X9 y. L0 ^ try{- p# O8 o; g3 k% S2 v" o- }$ `# T$ e
int row,col;. q' [2 c7 f4 O- R: m, P Q$ o
int i,j;5 L) H8 s' z# W. ~/ }! H
String tmp;
0 P: S4 x8 L0 r+ k: U& \ int aryA[][];# L: S+ U9 l- ~- {( a. n
int aryB[][];
4 l9 O. A! u# q- j BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
0 L4 | U- b7 f B& A. W& P, | 0 k: P5 u0 T9 O! Y
System.out.println("請輸入陣列列數:");3 o( j- O1 z0 r/ B1 G
row = Integer.parseInt(bin.readLine());
/ V2 B) X( m3 F! M0 B$ B System.out.println("每一列陣列裡要幾個數字:");8 A0 c* C9 `/ X$ X, `
col = Integer.parseInt(bin.readLine());! x4 c5 @- `( |0 @; K
9 a. }' ?* @: W0 p" D
aryA = new int[row][col];
2 [. G0 h; O- S e7 a' G6 e t aryB = new int[col][row];, S! q4 a! D& I
6 M x' @3 B0 X. a
for( i=0 ; i<row ; i++){
6 l& V8 Q7 d1 g/ V# F% ` System.out.println("請輸入第 "+ i +"列");
( d, w6 [& P, Z, N M+ G" Z$ r tmp = bin.readLine();0 u y2 X E! f( I: l
String[] strA = tmp.split(" "); v$ p3 F/ f" e* N
for( j=0 ; j<col ; j++ ){2 @: p* V, Z e. B, @- {8 i
aryA[i][j]=Integer.parseInt(strA[j]);
& e5 L' h% |" Z3 p1 R0 A: B! m! l8 U" X }% m9 r) U2 M! L; U# K4 H
}1 c& y0 L K+ N4 @; W
for( i=0 ; i<row ; i++){
! R8 S) \2 _% u1 X9 O( F7 m for( j=0 ; j<col ; j++){, T( A0 L- g! @, \; j0 _* m. \: x
aryB[j][i]=aryA[i][j];
; M+ V7 u6 C: G2 R f6 M$ ^7 W }
3 ~8 h, N4 R/ I }
% ~) s+ F. f8 U9 _% h+ P
5 e* x$ X& R+ W; N4 A System.out.println("陣列經行列轉換結果");
' E- F2 {& H5 a; N' {0 U! P* [ for( i=0 ; i<col ; i++){) z" t0 C( F& p# B' \
for( j=0 ; j<row ; j++){6 Y M" ?0 R: t* r$ B) a
System.out.print(aryA[j][i] + " ");
5 {& ^3 D: R8 y3 ^4 n9 M }
- }0 I7 E% _: M! r. z System.out.println();
# p. |/ u8 y9 A' Z5 l }. g0 b! i( W+ L7 S& w
}catch(Exception e){
( |/ J# ~. H; Y( r: Z$ i System.out.println("請輸入數字");6 |6 S9 J# a! ~/ s) {) X n; M
e.printStackTrace();: q: ~" l3 L. `, k+ [
}
8 \- v2 ^4 I& J" S2 x
7 p: o g3 ?/ o) C7 l }
) M& Z# g- j- ?6 \0 R+ B}
3 a# d7 b K" j6 v1 L' v7 K- g( R _% O; m7 [
( X( f' l$ g/ ?6 JTQC106
0 P9 E+ ]8 ^" H# {/ y/ \public class TQC106! B% _* |+ q' f1 W. L/ }/ y* H i
{
* U. J2 i" k- ~. F9 J: N public static void main(String args[])
& [$ b9 X4 P* c+ T6 t) F {
; d2 n' x7 _' g int sum = 0;
- q' W* q% w) P8 B: k int j = 0; . N( n! c( t1 S4 g# n0 A0 C
int k = 0;
0 c$ M5 t+ }3 P/ W, ^ for(int i=0 ; i<args.length ; i++){: U! d0 |+ N9 s+ w; f3 x7 [9 Y' U
try{
# @1 N0 X# [& K/ E sum += Integer.parseInt(args[i]);
/ c8 ^8 q o" f1 w- Q! ^ j++;
4 p! l" \; w/ L9 H# L |* c$ I }
" h6 H* e/ p% n catch(NumberFormatException e){
. U, H3 X* s6 R3 O8 f [ k++;
- ?8 M( [6 U; c5 D" L3 d }
0 [( v: I5 B0 _( N8 p' g5 I }
1 q& z- ~1 V: B: I5 v. X System.out.println("數值之總合為:" + sum );
" C2 r! K3 B: B: G System.out.println("非數值個數為:" + k );
, i3 d* x1 T; ], b( b7 [$ G System.out.println("純數值個數為:" + j );
5 l! Z1 P# [+ g. q% I- V }
1 Q+ V. {- ~" F} |