4_10
import java.util.*;
class Demo4_10{
public static void main(String[] args){
resolve2();
}
public static void resolve1(){
int[] nums=new int[100];
Scanner scanner=new Scanner(System.in);
System.out.print("Enter intager:");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}else{
nums[num-1]++;
}
}
for(int i=0;i<100;i++){
if(nums[i]!=0){
if(nums[i]==1){
System.out.println((i+1)+" occurs "+1+" time");
}else{
System.out.println((i+1)+" occurs "+nums[i]+" time");
}
}
}
}
public static void resolve2(){
int[] nums=new int[0];
Scanner scanner=new Scanner(System.in);
System.out.print("Enter intager:");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
nums=Arrays.copyOf(nums,nums.length+1);
nums[nums.length-1]=num;
}
InsertSort(nums);
for(int i=0;i<nums.length;){
int count=1;
for(int j=i+1;j<nums.length&&nums[j]==nums[i];j++){
count++;
}
if(count==1){
System.out.println(nums[i]+" occurs "+1+" time");
}else{
System.out.println(nums[i]+" occurs "+count+" times");
}
i=i+count;
}
}
public static int[] InsertSort(int[] arr){
for(int i=0;i<arr.length;i++){
int j;
int e=arr[i];
for(j=i;j>0&&arr[j-1]>e;j--){
arr[j]=arr[j-1];
}
arr[j]=e;
}
return arr;
}
}
4_11
import java.util.*;
class Demo4_11{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int[] arr=new int[0];
boolean flag=true;
System.out.print("Enter intager:");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
for(int i=0;i<arr.length;i++){
if(num==arr[i]){
flag=false;
break;
}
}
if(flag){
arr= Arrays.copyOf(arr,arr.length+1);
arr[arr.length-1]=num;
}
flag=true;
}
System.out.println(Arrays.toString(arr));
InsertSort(arr);
System.out.println("The number of distinct number is " + arr.length);
System.out.println(Arrays.toString(arr));
}
public static int[] InsertSort(int[] arr){
for(int i=0;i<arr.length;i++){
int j;
int e=arr[i];
for(j=i;j>0&&arr[j-1]>e;j--){
arr[j]=arr[j-1];
}
arr[j]=e;
}
return arr;
}
}
4_12
import java.util.*;
class Demo4_12{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int[] arr=new int[0];
System.out.print("Enter intager:");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
arr=Arrays.copyOf(arr,arr.length+1);
arr[arr.length-1]=num;
}
int[] oldArr=new int[0];
for(int i=0;i<arr.length;i++){
oldArr=Arrays.copyOf(oldArr,oldArr.length+1);
oldArr[oldArr.length-1]=arr[i];
}
arr=InsertSort(arr);
System.out.println("arr"+Arrays.toString(arr));
System.out.println("Arr"+Arrays.toString(oldArr));
if(isEqual(arr,oldArr)){
System.out.println("The list already sorted");
}else{
System.out.println("The list not sorted");
}
}
public static int[] InsertSort(int[] arr){
for(int i=0;i<arr.length;i++){
int j;
int e=arr[i];
for(j=i;j>0&&arr[j-1]>=e;j--){
arr[j]=arr[j-1];
}
arr[j]=e;
}
return arr;
}
public static boolean isEqual(int[] arr1,int[] arr2){
for(int i=0;i<arr1.length;i++){
if(arr1[i]!=arr2[i]){
return false;
}
}
return true;
}
}
4_13
import java.util.*;
class Demo4_13{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the number of bslls to drop:");
int ballsCount=scanner.nextInt();//定义球的个数
System.out.print("Enter the number of solts in the bean machine:");
int soltsCount=scanner.nextInt();//定义槽子的个数
String[] paths=new String[ballsCount];//路径
int[] solts=new int[soltsCount];//槽中球的个数
paths=Lujing(paths,soltsCount);
solts=count(paths,solts);
print(paths);
print(solts);
}
public static String[] Lujing(String[] paths,int soltsCount){
for(int i=0;i<paths.length;i++){
String path="";
for(int j=0;j<soltsCount-1;j++){
if((int)(Math.random()*2)==0){
path+="L";
}else{
path+="R";
}
}
paths[i]=path;
}
return paths;
}
public static int[] count(String[] paths,int[] solts){
for(int i=0;i<paths.length;i++){
int count=0;
String path=paths[i];
for(int j=0;j<path.length();j++){
if(path.charAt(j)=='R'){
count++;
}
}
solts[count]++;
}
return solts;
}
public static void print(String[] paths){
for(int i=0;i<paths.length;i++){
System.out.println(paths[i]);
}
}
public static void print(int[] solts){
for(int i=0;i<solts.length;i++){
System.out.println("第"+(i+1)+"个槽:"+solts[i]);
}
}
}
4_14
import java.util.Scanner;
class Demo4_14{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("输入学生的个数:");
int count=scanner.nextInt();//学生的人数
boolean[] status=new boolean[20];
//默认值false->关
for(int i=0;i<count;i++){//从第1~第100个学生
for(int j=i;j<20;){
status[j]=!status[j];
j+=i+1;
}
}
for(int i=0;i<status.length;i++){
String s=status[i]==true?"开":"关";
System.out.print(s+" ");
}
}
}
4_15
import java.util.*;
class Demo4_15{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter list1:");
int[] list1=new int[scanner.nextInt()];
for(int i=0;i<list1.length;i++){
list1[i]=scanner.nextInt();
}
System.out.print("Enter list2:");
int[] list2=new int[scanner.nextInt()];
for(int i=0;i<list2.length;i++){
list2[i]=scanner.nextInt();
}
System.out.println(equals(list1,list2));
}
public static boolean equals(int[] l1,int[] l2){
if(l1.length==l2.length){
for(int i=0;i<l1.length;i++){
if(l1[i]!=l2[i]){
return false;
}
}
return true;
}else{
return false;
}
}
}
4_16
import java.util.*;
class Demo4_16{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter list1:");
int[] list1=new int[scanner.nextInt()];
for(int i=0;i<list1.length;i++){
list1[i]=scanner.nextInt();
}
list1=InsertSort(list1);
System.out.print("Enter list2:");
int[] list2=new int[scanner.nextInt()];
for(int i=0;i<list2.length;i++){
list2[i]=scanner.nextInt();
}
list2=InsertSort(list2);
System.out.println(equals(list1,list2));
}
public static boolean equals(int[] l1,int[] l2){
if(l1.length==l2.length){
for(int i=0;i<l1.length;i++){
if(l1[i]!=l2[i]){
return false;
}
}
return true;
}else{
return false;
}
}
public static int[] InsertSort(int[] arr){
for(int i=0;i<arr.length;i++){
int j;
int e=arr[i];
for(j=i;j>0&&arr[j-1]>=e;j--){
arr[j]=arr[j-1];
}
arr[j]=e;
}
return arr;
}
}
4_17
import java.util.Scanner;
class Demo4_17{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the number of values:");
int[] arr=new int[scanner.nextInt()];
System.out.print("Enter values:");
for(int i=0;i<arr.length;i++){
arr[i]=scanner.nextInt();
}
int maxCount=0;
for(int i=0;i<arr.length;){
int count=1;
for(int j=i+1;j<arr.length;j++){
if(arr[i]==arr[j]){
count++;
}else{
break;
}
}
if(count>maxCount){
maxCount=count;
}
i+=count;
}
if(maxCount>=4){
System.out.println("有");
}else{
System.out.println("无");
}
}
}
4_18
import java.util.*;
class Demo4_18{
public static void main(String[] args){
int[] arr1={1,2,3,4,5};
int[] arr2={6,7,8,9,10};
int[] arr3;
arr3=merge(arr1,arr2);
System.out.println(Arrays.toString(arr3));
}
public static int[] merge(int[] arr1,int[] arr2){
int[] arr3=new int[arr1.length+arr2.length];
for(int i=0;i<arr1.length;i++){
arr3[i]=arr1[i];
}
for(int i=0;i<arr2.length;i++){
arr3[i+arr1.length]=arr2[i];
}
return arr3;
}
}
4_19
import java.util.Scanner;
class Demo4_19{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
double[][] matrix=new double[3][4];
System.out.println("Enter a 3-by-4 matrix row by row:");
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[i].length;j++){
matrix[i][j]=scanner.nextDouble();
}
}
for(int columnIndex=0;columnIndex<matrix[0].length;columnIndex++){
System.out.println("col "+columnIndex+"'s "+sumColumn(matrix,columnIndex));
}
}
public static double sumColumn(double[][] m,int columnIndex){
double sum=0;
for(int rowIndex=0;rowIndex<m.length;rowIndex++){
sum+=m[rowIndex][columnIndex];
}
return sum;
}
}
4_20
import java.util.Scanner;
class Demo4_20{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
double[][] matrix=new double[4][4];
System.out.println("Enter a 4-by-4 matrix row by row:");
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[i].length;j++){
matrix[i][j]=scanner.nextDouble();
}
}
System.out.println("对角线之和为"+sumColumn(matrix));
}
public static double sumColumn(double[][] m){
double sum=0;
for(int i=0;i<m.length;i++){
for(int j=0;j<m[i].length;j++){
if(i==j){
sum+=m[i][j];
}
}
}
return sum;
}
}
4_22
import java.util.Scanner;
class Demo4_22{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter m p n:");
int m=scanner.nextInt();
int p=scanner.nextInt();
int n=scanner.nextInt();
double[][] matrix1=new double[m][p];
double[][] matrix2=new double[p][n];
System.out.println("Enter a "+m+"-by-"+p+" matrix1 row by row:");
for(int i=0;i<matrix1.length;i++){
for(int j=0;j<matrix1[i].length;j++){
matrix1[i][j]=scanner.nextDouble();
}
}
System.out.println("Enter a "+p+"-by-"+n+" matrix2 row by row:");
for(int i=0;i<matrix2.length;i++){
for(int j=0;j<matrix2[i].length;j++){
matrix2[i][j]=scanner.nextDouble();
}
}
multiplyMatrix(matrix1,matrix2);
}
public static void multiplyMatrix(double[][] a,double[][] b){
double[][] matrix3=new double[a.length][b[0].length];
for(int i=0;i<a.length;i++){
for(int j=0;j<b[0].length;j++){
double sum=0;
for(int k=0;k<b.length;k++){
sum+=a[i][k]*b[k][j];
}
matrix3[i][j]=sum;
System.out.print(matrix3[i][j]+" ");
}
System.out.println();
}
}
}
4_23
class Demo4_23{
public static void main(String[] args){
int[][] m=new int[4][4];
for(int i=0;i<m.length;i++){
for(int j=0;j<m[i].length;j++){
m[i][j]=(int)(Math.random()*2);
System.out.print(m[i][j]+" ");
}
System.out.println();
}
int maxRow=0;
int maxCol=0;
int maxCount=0;
for(int i=0;i<m.length;i++){
int count=0;
for(int j=0;j<m[i].length;j++){
if(m[i][j]==1){
count++;
}
}
if(count>maxCount){
maxCount=count;
maxRow=i;
}
}
System.out.println("The largest row index:"+(maxRow+1));
maxCount=0;
for(int j=0;j<m.length;j++){
int count=0;
for(int i=0;i<m.length;i++){
if(m[i][j]==1){
count++;
}
}
if(count>maxCount){
maxCount=count;
maxCol=j;
}
}
System.out.println("The largest col index:"+(maxCol+1));
}
}
4_24
class Demo4_24{
public static void main(String[] args){
int[][] m=new int[][]{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16},
{17,18,19,20}
};
int maxLine=m.length-1;
int minLine=0;
int midLine=(maxLine+minLine)/2;
int key=10;
while(true){
if(key<m[midLine][0]){
maxLine=midLine-1;
}
if(key>m[midLine][m[0].length-1]){
minLine=midLine+1;
}
if(key>=m[midLine][0]&&key<=m[midLine][m[0].length-1]){
int max=m[0].length-1;
int min=0;
int mid=(min+max)/2;
while(key!=m[midLine][mid]){
if(key<m[midLine][mid]){
max=mid-1;
}
if(key>m[midLine][mid]){
min=mid+1;
}
mid=(min+max)/2;
if(min>max){
System.out.println("找不到!!");
return;
}
}
System.out.println(midLine+":"+mid);
return;
}
midLine=(maxLine+minLine)/2;
if(minLine>maxLine){
System.out.println("找不到!");
return;
}
}
}
}
4_25
import java.util.Scanner;
class Demo4_25{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入方阵的长度:");
int size=scanner.nextInt();
int[][] m=new int[size][size];
for(int i=0;i<m.length;i++){
for(int j=0;j<m.length;j++){
m[i][j]=(int)(Math.random()*2);
System.out.print(m[i][j]+" ");
}
System.out.println();
}
findRow(m);
findCol(m);
findDiagonal(m);
findSubDiagonal(m);
}
public static void findRow(int[][] m){
for(int i=0;i<m.length;i++){
boolean flag=true;
for(int j=1;j<m.length;j++){
if(m[i][0]!=m[i][j]){
flag=false;
break;
}
}
if(flag){
System.out.println("All "+m[i][0]+" on row "+(i+1));
}
}
}
public static void findCol(int[][] m){
for(int j=0;j<m.length;j++){
boolean flag=true;
for(int i=1;i<m.length;i++){
if(m[0][j]!=m[i][j]){
flag=false;
break;
}
}
if(flag){
System.out.println("All "+m[0][j]+" on col "+(j+1));
}
}
}
public static void findDiagonal(int[][] m){
boolean flag=true;
for(int i=0;i<m.length;i++){
if(m[0][0]!=m[i][i]){
flag=false;
break;
}
}
if(flag){
System.out.println("All "+m[0][0]+" on diagonal ");
}
}
public static void findSubDiagonal(int[][] m){
boolean flag=true;
for(int i=0;i<m.length;i++){
if(m[0][m.length-1]!=m[i][m.length-1-i]){
flag=false;
break;
}
}
if(flag){
System.out.println("All "+m[0][m.length-1]+" on subdiagonal ");
}
}
}
4_26
/*五子棋*/
import java.util.Scanner;
class Demo4_26{
public static Scanner scanner=new Scanner(System.in);
public static String[][] board=new String[15][15];
public static void main(String[] args){
chuangBoard();
printBoard();
startGame();
}
public static void startGame(){
int player=0;
while(isGameContinue()){
if(player%2==0){//黑方下棋
System.out.println(">>>黑方");
if(!luozi("X")){
continue;
}
}else{//白方下棋
System.out.println(">>>白方");
if(!luozi("O")){
continue;
}
}
player++;
}
if((player-1)%2==0){
System.out.println("黑棋赢");
}else{
System.out.println("白棋赢");
}
}
public static void chuangBoard(){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
board[i][j]="+";
}
}
}
public static boolean isGameContinue(){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
if(!board[i][j].equals("+")){
boolean flag=true;
//右
if(j<11){
for(int y=1;y<=4;y++){
if(!board[i][j].equals(board[i][j+y])){
flag=false;
break;
}
}
if(flag){
return false;
}
}
//下
if(i<11){
flag=true;
for(int x=1;x<=4;x++){
if(!board[i][j].equals(board[i+x][j])){
flag=false;
break;
}
}
}
if(flag){
return false;
}
//右下
if(i<11&&j<11){
flag=true;
for(int k=1;k<=4;k++){
if(!board[i][j].equals(board[i+k][j+k])){
flag=false;
break;
}
}
if(flag){
return false;
}
}
//右上
if(i>3&&j<11){
flag=true;
for(int k=1;k<=4;k++){
if(!board[i][j].equals(board[i-k][j+k])){
flag=false;
break;
}
}
if(flag){
return false;
}
}
}
}
}
boolean flag=true;
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
if(!board[i][j].equals("+")){
flag=false;
}else{
flag=true;
}
}
}
if(!flag){
System.out.println("和棋");
}
return true;
}
public static boolean luozi(String chess){
System.out.print(">>>X=");
int x=scanner.nextInt()-1;
System.out.print(">>>Y=");
int y=scanner.nextInt()-1;
if(board[x][y].equals("+")){
board[x][y]=chess;
printBoard();
return true;
}else{
System.out.println("此处已有落子");
return false;
}
}
public static void printBoard(){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
}
}