標題:
jva 104 河洛之數
[打印本頁]
作者:
b790113g
時間:
2011-12-31 10:58
標題:
jva 104 河洛之數
1) 矩陣內數字皆不可重複出現
6 M9 ^) P0 | n- q" _! e
(2) 程式每次執行,矩陣數字排列皆不相同
3 x+ i- H/ }# q
(3) 矩陣之縱向、橫向、對角線數字總和皆為15
#include <iostream>
#include <cstdlib>
using namespace std ;
int main(){
int a[] = {1,2,3,4,5,6,7,8,9} ;
//亂數種子
srand(time(NULL));
while(true){
//交換9次
for(int i=0;i<9;i++){
int r = rand()%9 ;
//交換
int t = a[r] ;
a[r] = a[i];
a[i] = t ;
}
//列的判斷
int a1 = a[0] + a[1] + a[2] ;
int a2 = a[3] + a[4] + a[5] ;
int a3 = a[6] + a[7] + a[8] ;
//行的判斷
int a4 = a[0] + a[3] + a[6] ;
int a5 = a[1] + a[4] + a[7] ;
int a6 = a[2] + a[5] + a[8] ;
//斜的判斷
int a7 = a[0] + a[4] + a[8] ;
int a8 = a[2] + a[4] + a[6] ;
if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15){
break;
}
}
cout << a[0] << " " << a[1] << " " << a[2] << endl;
cout << a[3] << " " << a[4] << " " << a[5] << endl;
cout << a[6] << " " << a[7] << " " << a[8] << endl;
system("pause");
return 0;
}
複製代碼
作者:
johnson
時間:
2011-12-31 11:35
public class j102
: M& I% I$ v: k! X
{
. a/ {6 Y4 y& r" G! A
public static void main(String arg[])
; j( D; N h7 t! \, c
{
+ E2 Y- t. u5 u0 K( Q h( N* F$ M Q
int num []={1,2,3,4,5,6,7,8,9};
; j/ _6 |1 K, A2 V4 R) ~# S, z m
while(trun)
3 X D& Z; w1 D5 n' E
{
" _+ j% g4 ]" c$ G, t: k" {% ]* M
int r=(int)(Math.random()*9)+1;
% D5 p2 o8 _$ b' N5 z
int t=num[i];
0 Q" b H2 t8 n( }8 }
num[i]=num[r];
# w, h+ b. ?8 A: T+ m
num[r]=t;
% d) r e% A& A- u( g3 D
int a1 = num[0] + num[1] + num[2] ;
( ~2 m, F& h6 F
int a2 = num[3] + num[4] + num[5] ;
/ [9 X1 T/ \/ E& M6 h0 L; u* V
int a3 = num[6] + num[7] + num[8] ;
$ x! Q3 n0 n3 \0 ]/ |. g( S
int a4 = num[0] + num[3] + num[6] ;
7 s$ h' n- K: Q) m3 W9 C
int a5 = num[1] + num[4] + num[7] ;
, l7 r$ z& I; _; y' w2 O* m$ `2 F- E
int a6 = num[2] + num[5] + num[8] ;
, E* v% \5 G$ R5 L' O% p. \. N4 A
int a7 = num[0] + num[4] + num[8] ;
6 P6 N9 f0 L6 M: z/ r, H
int a8 = num[2] + num[4] + num[6] ;
, R: C" ~8 B# {" e9 |9 R* B( x
if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15)
) _( V2 d1 o8 c+ t# c
}break;
4 O6 q0 s* O" X, D) Q. s7 S: h
}
! r; p4 _! }; n+ ]! Z% M
}
作者:
kim
時間:
2011-12-31 11:36
#include <iostream>
#include <cstdlib>
using namespace std ;
int main(){
int a[] = {1,2,3,4,5,6,7,8,9} ;
//亂數種子
srand(time(NULL));
while(true){
//交換9次
for(int i=0;i<9;i++){
int r = rand()%9 ;
//交換
int t = a[r] ;
a[r] = a[i];
a[i] = t ;
}
//列的判斷
int a1 = a[0] + a[1] + a[2] ;
int a2 = a[3] + a[4] + a[5] ;
int a3 = a[6] + a[7] + a[8] ;
//行的判斷
int a4 = a[0] + a[3] + a[6] ;
int a5 = a[1] + a[4] + a[7] ;
int a6 = a[2] + a[5] + a[8] ;
//斜的判斷
int a7 = a[0] + a[4] + a[8] ;
int a8 = a[2] + a[4] + a[6] ;
if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15){
break;
}
}
cout << a[0] << " " << a[1] << " " << a[2] << endl;
cout << a[3] << " " << a[4] << " " << a[5] << endl;
cout << a[6] << " " << a[7] << " " << a[8] << endl;
system("pause");
return 0;
}
複製代碼
作者:
eric5802
時間:
2011-12-31 11:38
public class j102
/ c" E: j3 _5 d: [
{
& \6 r$ y/ r) P; n4 s: K9 P
public static void main(String arg[])
2 A Y; b ]& i4 }
{
) `( Z y. t( P" }, N9 u
int num []={1,2,3,4,5,6,7,8,9};
( E7 F# Z# F. a% K( R
while(trun)
! V5 \/ `9 d2 o
{
2 v& y6 L0 C7 L9 s0 Q
int r=(int)(Math.random()*9)+1;
7 a+ G1 K* q3 z+ M, O2 a2 w
int t=num[i];
& P' P1 d9 Y. ?4 ?
num[i]=num[r];
- g* V3 e, S# | f. D+ K
num[r]=t;
; a' ~2 l5 ~6 E, G% K4 G# `
int a1 = num[0] + num[1] + num[2] ;
( c7 p: g( X1 s: C1 t0 `/ L( \
int a2 = num[3] + num[4] + num[5] ;
. g$ |+ b" Y& c) t* y% [ ~
int a3 = num[6] + num[7] + num[8] ;
9 W# q+ C5 p/ N# |& ^+ |. \
int a4 = num[0] + num[3] + num[6] ;
& @9 ^& w2 {; g
int a5 = num[1] + num[4] + num[7] ;
! y0 l# @2 {! a: u+ ]
int a6 = num[2] + num[5] + num[8] ;
9 c/ S- X- V; s+ S* i4 `1 [
int a7 = num[0] + num[4] + num[8] ;
+ A$ s ~! \- M
int a8 = num[2] + num[4] + num[6] ;
, v+ z* K9 f) L3 p7 B* v- B6 L: ^
if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15)
) q7 j5 V( o+ f8 S T8 W
}break;
8 D! {; C& B' X. U# T
}
- s7 P3 x) o* \% ]* L; p
}
作者:
TOM
時間:
2011-12-31 11:49
public class j104
{
public static void main(String[]arg)
{
int num[]={1,2,3,4,5,6,7,8,9};
while(true)
{
for(int i=0;i<9;i++)
{
int r=(int)(Math.random()*9);
int t=num[i];
num[i]=num[r];
num[r]=t;
}
int a1 = num[0]+num[1]+num[2];
int a2 = num[3]+num[4]+num[5];
int a3 = num[6]+num[7]+num[8];
int a4= num[0]+num[3]+num[6];
int a5= num[1]+num[4]+num[7];
int a6= num[2]+num[5]+num[8];
int a7= num[0]+num[4]+num[8];
int a8= num[2]+num[4]+num[6];
if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15)
{
break ;
}
System.out.println("答案為:");
System.out.println(num[0]+" "+num[1]+" "+num[2]);
System.out.println(num[3]+" "+num[4]+" "+num[4]);
System.out.println(num[5]+" "+num[6]+" "+num[7]);
}
}
}
複製代碼
作者:
may
時間:
2011-12-31 12:18
[attach]563[/attach]
作者:
may
時間:
2012-1-4 17:04
[attach]564[/attach]
作者:
lon
時間:
2012-1-4 17:11
[attach]565[/attach]
作者:
may
時間:
2012-1-4 17:24
[attach]566[/attach]
作者:
kim
時間:
2012-4-4 11:44
http://site.istak.org.tw/bbs/att ... 3D&noupdate=yes
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2