標題:
jva 104 河洛之數
[打印本頁]
作者:
b790113g
時間:
2011-12-31 10:58
標題:
jva 104 河洛之數
1) 矩陣內數字皆不可重複出現
4 u( v9 h5 Q) L4 V/ d# R, n
(2) 程式每次執行,矩陣數字排列皆不相同
2 P; G$ v J2 J1 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
0 C) [7 \& U: v$ h3 f# d
{
4 F6 }: J) ?- |5 a, G6 X i6 i
public static void main(String arg[])
! w \/ \% A- [, @$ F5 k ]
{
+ T9 [) E4 P! |8 }8 }* X
int num []={1,2,3,4,5,6,7,8,9};
2 D n. K* c4 ?
while(trun)
* H% R. S) h3 m5 }
{
% c0 M% y) X. ~" C' X/ Q
int r=(int)(Math.random()*9)+1;
, ]. ]8 l; B% R
int t=num[i];
6 B7 d6 A0 D( d& O# {
num[i]=num[r];
) I5 {8 L& U' r2 o5 K4 l
num[r]=t;
! i4 U. t' G/ h
int a1 = num[0] + num[1] + num[2] ;
6 W. W# A% t/ j% L, N7 i
int a2 = num[3] + num[4] + num[5] ;
0 ^0 P! t1 n4 @) A1 m; s
int a3 = num[6] + num[7] + num[8] ;
. A) d" ?7 }. Q
int a4 = num[0] + num[3] + num[6] ;
8 d5 Y& ~" G) S
int a5 = num[1] + num[4] + num[7] ;
. N" P# u. _* X% i' }. \
int a6 = num[2] + num[5] + num[8] ;
& T/ f' c. b) f) d3 H% g D
int a7 = num[0] + num[4] + num[8] ;
( r- i1 z5 [$ z0 i0 S$ D! W
int a8 = num[2] + num[4] + num[6] ;
" ?1 K' m. W* V+ d3 F
if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15)
) h8 S: ]; M2 @* m
}break;
) d9 a) S2 ]: i6 t) l- L
}
4 B d' S# Y# U% \4 s& Q
}
作者:
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
4 ?" a3 a# k9 z- T, `% p" b1 t
{
; F3 U. E9 o, p
public static void main(String arg[])
, y. z* S5 ?* @7 z: |0 q. b
{
+ N, b9 B: o3 o" _ c% b" |& k( u
int num []={1,2,3,4,5,6,7,8,9};
+ ~/ V' x, J2 g
while(trun)
- ` p& k! G6 s# b7 L
{
$ o7 Z: Z9 z, S9 j- T( z" R4 R
int r=(int)(Math.random()*9)+1;
! [0 A; l; v$ F: ~, ]7 F
int t=num[i];
1 P$ c+ Z/ b0 ^, `, z, s
num[i]=num[r];
4 ~9 X8 f! u) O' L) Y
num[r]=t;
9 @) p4 C, O9 ]: ^
int a1 = num[0] + num[1] + num[2] ;
, z: B$ f$ R2 a
int a2 = num[3] + num[4] + num[5] ;
4 b/ H2 q K. z8 M' N6 f) M; F' [+ E
int a3 = num[6] + num[7] + num[8] ;
+ c3 K8 s9 M: x. ^, |. K+ d; O: q
int a4 = num[0] + num[3] + num[6] ;
* ^$ H6 U; f4 e4 p- v9 ]& Y
int a5 = num[1] + num[4] + num[7] ;
! S( z6 B$ t6 A& j# ^: L2 p! W
int a6 = num[2] + num[5] + num[8] ;
3 }4 m/ B$ y) j" J9 I
int a7 = num[0] + num[4] + num[8] ;
$ ]% e: m+ ~2 R1 |/ n; l
int a8 = num[2] + num[4] + num[6] ;
& S: _* {5 c3 g) g1 v( }2 }" O
if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15)
8 i* e7 G: J2 `- V" a" K7 c
}break;
1 k( F" H! C6 F5 [; H
}
$ h {% ^' R7 o3 K+ \) E) v
}
作者:
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://istak.org.tw/seed/)
Powered by Discuz! 7.2