给定一个整数表示元素集合个数 会随机生成num个序偶,打印出来。并自动判断关系 入输入0,表示退出。
import java. util. Random ;
import java. util. Scanner ;
public class Test {
public static void main ( String [ ] args) {
Scanner scanner= new Scanner ( System . in) ;
int i= scanner. nextInt ( ) ;
while ( i!= 0 ) {
new ZuoYe ( i) ;
i= scanner. nextInt ( ) ;
}
System . out. println ( "退出" ) ;
}
}
class ZuoYe {
int Max ;
int [ ] [ ] arr;
int num;
Random random;
public ZuoYe ( int max) {
this. Max = max;
arr = new int [ max] [ max] ;
this . random = new Random ( ) ;
this . num = this . random. nextInt ( max * max) ;
int row;
int line;
if ( num== 0 ) {
System . out. print ( "空" ) ;
} else {
for ( int i = 0 ; i < this . num; i++ ) {
row = random. nextInt ( max) ;
line = random. nextInt ( max) ;
if ( arr[ row] [ line] == 1 ) {
-- i;
} else {
arr[ row] [ line] = 1 ;
if ( i== this . num- 1 )
System . out. print ( new Node ( row+ 1 , line+ 1 ) . toString ( ) ) ;
else
System . out. print ( new Node ( row+ 1 , line+ 1 ) . toString ( ) + " ," ) ;
}
}
}
System . out. println ( ) ;
isR ( ) ;
isS ( ) ;
isT ( ) ;
}
public void isR ( ) {
for ( int i = 0 ; i < this. Max; i++ ) {
if ( arr[ i] [ i] == 0 ) {
System . out. println ( "反自反性" ) ;
return ;
}
}
System . out. println ( "自反性" ) ;
}
public void isS ( ) {
for ( int i = 0 ; i < this. Max; i++ ) {
for ( int j = 0 ; j < this. Max; j++ ) {
if ( arr[ i] [ j] == 1 ) {
if ( arr[ j] [ i] == 0 ) {
System . out. println ( "反对称性" ) ;
return ;
}
}
}
}
System . out. println ( "对称性" ) ;
}
public void isT ( ) {
for ( int a = 0 ; a < this. Max; a++ ) {
for ( int b = 0 ; b < this. Max; b++ ) {
if ( arr[ a] [ b] == 1 ) {
for ( int c = 0 ; c < this. Max; c++ ) {
if ( arr[ b] [ c] == 1 ) {
if ( arr[ a] [ c] == 0 ) {
System . out. println ( "没有传递性" ) ;
return ;
}
}
}
}
}
}
System . out. println ( "具有传递性" ) ;
}
public static class Node {
int i;
int j;
public Node ( int i, int j) {
this . i= i;
this . j= j;
}
@Override
public String toString ( ) {
return "< " +
i +
", " + j +
" >" ;
}
}
}