java语言程序设计基础篇第八章编程练习题

版权声明:如果发现错误或者其他什么不足之处。请留言。 https://blog.csdn.net/gyhguoge01234/article/details/51872810

1

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		final int row = 3;
		final int column = 4;
		Scanner input = new Scanner(System.in);
		double[][] num = new double[row][column];
		
		for(int i = 0; i < row; ++i)
			for(int j = 0; j < column; ++j)
				num[i][j] = input.nextDouble();

		for(int i = 0; i < column; ++i){
			double sum = 0;
			for(int j = 0; j < row; ++j){
				sum += num[j][i];
			}
			System.out.println(sum);
		}
	}
}

2

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		final int rac = 4;
		Scanner input = new Scanner(System.in);
		double[][] num = new double[rac][rac];
		
		for(int i = 0; i < rac; ++i)
			for(int j = 0; j < rac; ++j)
				num[i][j] = input.nextDouble();
		double sum = 0;
		for(int i = 0; i < rac; ++i)
			sum += num[i][i];
		System.out.println(sum);
	}
}

3

public class Main{
	
	public static void main(String[] args){
		char answer[][] = {
				{'A','B','A','C','C','D','E','E','A','D'},
				{'D','B','A','B','C','A','E','E','A','D'},
				{'E','D','D','A','C','B','E','E','A','D'},
				{'C','B','A','E','D','C','E','E','A','D'},
				{'A','B','D','C','C','D','E','E','A','D'},
				{'B','B','E','C','C','D','E','E','A','D'},
				{'B','B','A','C','C','D','E','E','A','D'},
				{'E','B','E','C','C','D','E','E','A','D'}
		};
		
		char keys[] = {'D','B','D','C','C','D','A','E','A','D'};
		int[] student = new int[answer.length];
		for(int i = 0; i < answer.length; ++i){
			int correctCount = 0;
			for(int j = 0; j < answer[i].length; ++j){
				if(answer[i][j] == keys[j])
					++correctCount;
			}
			student[i] = correctCount;
		}
		java.util.Arrays.sort(student);
		
		for(int i = 0; i < student.length; ++i)
			System.out.println(student[i]);
	}
}

4

public class Main{
	
	public static void main(String[] args){
		int[][] time = {
				{2,4,3,4,5,8,8},
				{7,3,4,3,3,4,4},
				{3,3,4,3,3,2,2},
				{9,3,4,7,3,4,1},
				{3,5,4,3,6,3,8},
				{3,4,4,6,3,4,4},
				{3,7,4,8,3,8,4},
				{6,3,5,9,2,7,9}
		};
		
		int[] temp = null;
		
		for(int i = 0; i < 7; ++i){
			for(int j = i+1; j < 8; ++j){
				if(time[i][6] < time[j][6]){
					temp = time[i];
					time[i] = time[j];
					time[j] = temp;
				}
			}
		}
		
		for(int i = 0; i < 8; ++i){
			for(int j = 0; j < 7; ++j){
				System.out.print(time[i][j] + " ");
			}
			System.out.println();
		}
		
	}
}

5

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		double[][] matrix1 = new double[3][3];
		double[][] matrix2 = new double[3][3];
		System.out.print("Enter matrix1:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j)
				matrix1[i][j] = input.nextDouble();
		
		System.out.print("Enter matrix2:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j)
				matrix2[i][j] = input.nextDouble();
		
		matrix1 = addMatrix(matrix1,matrix2);
		
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				System.out.print(matrix1[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	public static double[][] addMatrix(double[][] a, double[][] b){
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j)
				a[i][j] += b[i][j];
		return a;
	}
}

6

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		double[][] matrix1 = new double[3][3];
		double[][] matrix2 = new double[3][3];
		System.out.print("Enter matrix1:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j)
				matrix1[i][j] = input.nextDouble();
		
		System.out.print("Enter matrix2:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j)
				matrix2[i][j] = input.nextDouble();
		
		double[][] result = multiplyMatrix(matrix1,matrix2);
		
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				System.out.printf("%.2f ",result[i][j]);
			}
			System.out.println();
		}
	}
	
	public static double[][] multiplyMatrix(double[][] a, double[][] b){
		double[][] result = new double[3][3];
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				double sum = 0;
				for(int v = 0; v < 3; ++v)
					sum += a[i][v]*b[v][j];
				result[i][j] = sum;
			}
		}
		return result;
	}
}

7
。。。。。。。。。

8

import java.util.Scanner;
//暂时是这样想的,还可以用一个二维数组存储所有的解,然后遍历就好了
public class Main{
	
	public static void main(String[] args){
		final int inf = 999999999;
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number points: ");
		int numberOfPoints = input.nextInt();
		
		double[][] points = new double[numberOfPoints][4];
		
		for(int i = 0; i < points.length; ++i){
			java.util.Arrays.fill(points[i], inf);
		}
		
		System.out.print("Enter " + numberOfPoints + " points:");
		for(int i = 0; i < numberOfPoints; ++i){
			points[i][0] = input.nextDouble();
			points[i][1] = input.nextDouble();
		}
		
		int p1 = 0, p2 = 1;
		double shortestDistance = distance(points[p1][0],points[p1][1]
				,points[p2][0],points[p2][1]);
		
		for(int i = 0; i < points.length; ++i){
			for(int j = i+1; j < points.length; ++j){
				double distance = distance(points[i][0],points[i][1]
						,points[j][0],points[j][1]);
				
				if(distance < points[i][2]){
					points[i][2] = distance;
					points[i][3] = j;
				}
				if(distance < points[j][2]){
					points[j][2] = distance;
					points[j][3] = i;
				}
				
				if(shortestDistance > distance){
					p1 = i;
					p2 = j;
					shortestDistance = distance;
				}
			}
		}
		for(int i = 0; i < points.length; ++i){
			if(points[i][2] == shortestDistance){
				System.out.println("(" + points[i][0] + "," + points[i][1] + ")" + " (" 
						+ points[(int)points[i][3]][0] + "," + points[(int)points[i][3]][1] + ")" + "");
			}
		}
		System.out.println("Their diatance is " + shortestDistance);
	}
	
	public static double distance(double x1, double y1, double x2, double y2){
		return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	}
}

9

import java.util.Scanner;

public class Main{
	static char[][] ch = new char[3][3];
	static int flag = 0;
	static Scanner cin = new Scanner(System.in);
	public static void main(String[] args){
		for(int i = 0; i < 3; ++i)
			java.util.Arrays.fill(ch[i], ' ');
		printMap();
		while(true){
			goingX();
			printMap();
			if(flag == 1)
				break;
			going0();
			printMap();
			if(flag == 1)
				break;
		}
		
	}
	
	public static void going0(){
		System.out.print("Enter a row(0,1 or 2) for plyaer 0:");
		int x = cin.nextInt();
		System.out.print("Enter a column(0,1 or 2) for plyaer 0:");
		int y = cin.nextInt();
		ch[x][y] = '0';

	}
	
	public static void goingX(){
		System.out.print("Enter a row(0,1 or 2) for plyaer X:");
		int x = cin.nextInt();
		System.out.print("Enter a column(0,1 or 2) for plyaer X:");
		int y = cin.nextInt();
		ch[x][y] = 'X';

	}
	
	public static void printMap(){
		System.out.printf("-------------\n");
		System.out.printf("| %c | %c | %c |\n",ch[0][0],ch[0][1],ch[0][2]);
		System.out.printf("-------------\n");
		System.out.printf("| %c | %c | %c |\n",ch[1][0],ch[1][1],ch[1][2]);
		System.out.printf("-------------\n");
		System.out.printf("| %c | %c | %c |\n",ch[2][0],ch[2][1],ch[2][2]);
		System.out.printf("-------------\n");
		if(check() == 1){
			System.out.println("X player win");
			flag = 1;
		}
		else if(check() == 2){
			System.out.println("0 player win");
			flag =1; 
		}
	}
	
	public static int check(){
		if(ch[0][0] == 'X' && ch[0][0] == ch[0][1] && ch[0][0] == ch[0][2])
			return 1;
		if(ch[0][0] == 'X' && ch[0][0] == ch[1][0] && ch[0][0] == ch[2][0])
			return 1;
		if(ch[0][0] == 'X' && ch[0][0] == ch[1][1] && ch[0][0] == ch[2][2])
			return 1;
		if(ch[1][1] == 'X' && ch[0][2] == ch[1][1] && ch[0][2] == ch[2][0])
			return 1;
		
		if(ch[0][0] == '0' && ch[0][0] == ch[0][1] && ch[0][0] == ch[0][2])
			return 2;
		if(ch[0][0] == '0' && ch[0][0] == ch[1][0] && ch[0][0] == ch[2][0])
			return 2;
		if(ch[0][0] == '0' && ch[0][0] == ch[1][1] && ch[0][0] == ch[2][2])
			return 2;
		if(ch[1][1] == '0' && ch[0][2] == ch[1][1] && ch[0][2] == ch[2][0])
			return 2;
		
		return 0;
	}
	
}

10

import java.util.Scanner;

public class Main{
	
	public static void main(String[] args){
		int[][] nums = new int[4][4];
		for(int i = 0; i < 4; ++i){
			for(int j = 0; j < 4; ++j){
				//此处竟然出现负数,不懂,只好取绝对值
				nums[i][j] = Math.abs((((int)System.currentTimeMillis())/(i+j+1))%2);
			}
		}

		int rowIndex = 0;
		int columnIndex =0 ;
		int rcount = 0;
		int rmaxCount = 0;
		int ccount = 0, cmaxCount = 0;
		for(int i = 0; i < 4; ++i){
			rcount = 0;
			ccount = 0;
			for(int j = 0; j < 4; ++j){
				if(nums[i][j] == 1)
					++rcount;
			}
			if(rcount > rmaxCount){
				rmaxCount = rcount;
				rowIndex = i;
			}
			
			for(int j = 0; j < 4; ++j){
				if(nums[j][i] == 1)
					++ccount;
			}
			if(ccount  > cmaxCount){
				cmaxCount = ccount;
				columnIndex = i;
			}
		}
		for(int i = 0; i < 4; ++i){
			for(int j = 0; j < 4; ++j){
				System.out.print(nums[i][j] + " ");
			}
			System.out.println();
		}
		System.out.println("The largest row index:" + rowIndex);
		System.out.println("The largest column index:" + columnIndex);
	}
}

11

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter a number between 0 and 511:");
		int num = cin.nextInt();
		int[] bits = new int[9];
		java.util.Arrays.fill(bits, 0);
		int i = 0;
		while(num > 0){
			bits[i] = num%2;
			num /= 2;
			++i;
		}
		System.out.println();
		
		for(int j = 8; j >= 0; --j){
			if(bits[j] == 1)
				System.out.print("T");
			else
				System.out.print("H");
			if(j%3 == 0)
				System.out.println();
		}
	}
}

12

。。。。。。。

13

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		int rows = cin.nextInt();
		int columns = cin.nextInt();
		double[][] a = new double[rows][columns];
		for(int i = 0; i < rows; ++i){
			for(int j = 0; j < columns; ++j){
				a[i][j] = cin.nextDouble();
			}
		}
		int[] result = new int[2];
		result = locateLargest(a);
		System.out.println("(" + result[0] + "," + result[1] + ")");
	}
	
	public static int[] locateLargest(double[][] a){
		int[] result = new int[2];
		double max = 0;
		for(int i = 0; i < a.length; ++i){
			for(int j = 0; j < a[i].length; ++j){
				if(a[i][j] > max){
					max = a[i][j];
					result[0] = i;
					result[1] = j;
				}
			}
		}
		return result;
	}
}

14

import java.util.Scanner;
//对角线不写了
public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		int size = cin.nextInt();
		int[][] matrix = new int[size][size];
		for(int i = 0; i < size; ++i){
			for(int j = 0; j < size; ++j){
				matrix[i][j] = (((int)System.currentTimeMillis())/(i+j+1))%2;
			}
		}

		for(int i = 0; i < size; ++i){
			for(int j = 0; j < size; ++j){
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}
		
		int flag = 0;
		int count = 0;
		for(int i = 0; i < size; ++i){
			flag = 0;
			for(int j = 0; j < size; ++j){
				if(matrix[i][j] == 1)
					++flag;
				else if(matrix[i][j] == -1)
					--flag;
			}
			if(flag == size){
				System.out.println("All 1s on row " + i);
				++count;
			}
			else if(flag == -size){
				System.out.println("All 0s on row " + i);
				++count;
			}
		}
		if(count == 0)
			System.out.println("no same numbers on a row");
		count = 0;
		for(int j = 0; j < size; ++j){
			flag = 0;
			for(int i = 0; i < size; ++i){
				if(matrix[i][j] == 1)
					++flag;
				else if(matrix[i][j] == 0)
					--flag;
			}
			if(flag == size){
				System.out.println("All 1s on column " + j);
				++count;
			}
			else if(flag == -size){
				System.out.println("All 1s on column " + j);
				++count;
			}
		}
		if(count == 0)
			System.out.println("no same numbers on a column");
	}
}

15

。。。。。。

16

import java.util.Scanner;
//写完发现要求和书上写的不一样,不过去掉selectSort函数就和书上一样了
public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		int[][] arr = new int[6][2];
		for(int i = 0; i < 6; ++i){
			arr[i][0] = cin.nextInt();
			arr[i][1] = cin.nextInt();
		}
		
		sort(arr);
		
		for(int i = 0; i < 6; ++i){
			System.out.println(arr[i][0] +  " " + arr[i][1]);
		}
	}
	
	public static void sort(int m[][]){
		for(int i = 0; i < (int)m.length; ++i){
			selectSort(m[i]);
		}
		
		int[] temp = null;
		for(int i = 0; i < (int)m.length - 1; ++i){
			for(int j = i+1; j < (int)m.length; ++j){
				if(compareInt(m[i],m[j]) > 0){
					temp = m[i];
					m[i] = m[j];
					m[j] = temp;
				}
			}
		}
	}
	
	public static int compareInt(int[] a, int[] b){
		int len = a.length < b.length ? a.length : b.length;
		for(int i = 0; i < len; ++i){
			if(a[i] > b[i])
				return 1;
			else if(a[i] < b[i])
				return -1;
		}
		if(a.length > b.length)
			return 1;
		else if(a.length < b.length)
			return -1;
		else
			return 0;
	}
	
	public static void selectSort(int arr[]){
		for(int i = 0; i < (int)arr.length - 1; ++i){
			for(int j = i+1; j < (int)arr.length; ++j){
				if(arr[i] > arr[j]){
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}
	}
}

17

import java.util.Scanner;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		double limit = cin.nextDouble();
		double[][] borrower = new double[n][n];
		double[] balance = new double[n];
		double[] total = new double[n];
		boolean[] mark = new boolean[n];
		
		for(int i = 0; i < n; ++i) Arrays.fill(borrower[i], 0.0);
		//输入数据
		for(int i = 0; i < n; ++i){
			balance[i] = cin.nextDouble();
			int cnt = cin.nextInt();
			for(int j = 0; j < cnt; ++j){
				int index = cin.nextInt();
				borrower[i][index] = cin.nextDouble();
			}
		}
		//假设都没放贷款的总资产
		for(int i = 0; i < n; ++i){
			total[i] = balance[i];
			for(int j = 0; j < n; ++j){
				total[i] += borrower[i][j];
			}
		}
		Arrays.fill(mark,true);
		for(int i = 0; i < n; ++i){
			if(total[i] < limit && mark[i]){
				//把危险的借贷都剪掉
				mark[i] = false;
				for(int j = 0; j < n; ++j){
					borrower[j][i] = 0;
				}
				//重新计算总资产
				for(int v = 0; v < n; ++v){
					total[v] = balance[v];
					for(int j = 0; j < n; ++j){
						total[v] += borrower[v][j];
					}
				}
				//从头开始检查
				i = -1;
			}
		}
		
		for(int i = 0; i < n; ++i)
			if(!mark[i])
				System.out.print(i + " ");
	}
}


18

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		int[][] m = {{1,2},{3,4},{5,6},{7,8},{9,10}};
		shuffle(m);
		for(int i = 0; i < m.length; ++i){
			for(int j = 0; j < m[i].length; ++j){
				System.out.print(m[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	public static void shuffle(int[][] m){
		int xlen = m.length;
		int ylen = m[0].length;
		for(int i = 0; i < xlen; ++i){
			for(int j = 0; j < ylen; ++j){
				int n = (int)(Math.random()*ylen);
				int temp = m[i][n];
				m[i][n] = m[i][j];
				m[i][j] = temp;
			}
		}
	}
}

19

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		int[][] m = new int[5][5];
		for(int i = 0; i < 5; ++i){
			for(int j = 0; j < 5; ++j){
				m[i][j] = cin.nextInt();
			}
		}
		System.out.println(isConsecutiveFour(m));
	}
	
	public static boolean isConsecutiveFour(int[][] values){
		//行
		int count = 0;
		for(int i = 0; i < (int)values.length; ++i){
			count = 1;
			for(int j = 1; j < (int)values[i].length; ++j){
				if(values[i][j] == values[i][j-1])
					++count;
				else
					count = 1;
				if(count == 4){
					return true;
				}
			}
		}
		//列
		count = 0;
		int len = (int)values.length;
		for(int i = 0; i < len; ++i){
			count = 1;
			for(int j = 1; j < len; ++j){
				if(values[j][i] == values[j-1][i])
					++count;
				else
					count = 1;
				if(count == 4){
					return true;
				}
			}
		}
		//对角线
		for(int i = 1; i < len; ++i){
			count = 1;
			for(int j = 1; j < len-1; ++j){
				if(values[i][j] == values[i-1][j-1])
					++count;
				else
					count = 1;
				if(count == 4)
					return true;
			}
		}
		
		for(int i = 1; i < len; ++i){
			count = 1;
			for(int j = len-1-1; j >= 0; --j){
				if(values[i][j] == values[i-1][j+1])
					++count;
				else
					count = 1;
				if(count == 4)
					return true;
			}
		}
		return false;
	}
}

20

。。。。。。。

21

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter the number of cities:");
		int num = cin.nextInt();
		double[][] coordinate = new double[num][2];
		
		for(int i = 0; i < num; ++i){
			coordinate[i][0] = cin.nextDouble();
			coordinate[i][1] = cin.nextDouble();
		}
		
		double distance = 0;
		double sumDis = 0;
		double minSum = 999999999999.0;
		double x = 0,y = 0;
		for(int i = 0; i < num; ++i){
			sumDis = 0;
			for(int j = 0; j < num; ++j){
				distance = Math.sqrt(Math.pow(coordinate[i][0]-coordinate[j][0], 2) 
						+ Math.pow(coordinate[i][1]-coordinate[j][1], 2));
				sumDis += distance;
			}
			if(sumDis < minSum){
				minSum = sumDis;
				x = coordinate[i][0];
				y = coordinate[i][1];
			}
		}
		System.out.println("(" + x + "," + y + ")\n" + minSum);
	}
}

22

。。。。。。

23

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		System.out.println("Enter a 6-by-6 matrix row by row:");
		int[][] matrix = new int[6][6];
		for(int i = 0; i < 6; ++i){
			for(int j = 0; j < 6; ++j){
				matrix[i][j] = cin.nextInt();
			}
		}
		//row
		int[][] coor1 = new int[6][2];
		int[][] coor2 = new int[6][2];
		int count = 0;
		for(int i = 0; i < 6; ++i){
			count = 0;
			for(int j = 0; j < 6; ++j){
				if(matrix[i][j] == 1)
					++count;
			}
			
			if(count%2 == 1){
				for(int j = 0; j < 6; ++j){
					coor1[j][0] = i;
					coor1[j][1] = j;
				}
				break;
			}
		}
		//column
		for(int i = 0; i < 6; ++i){
			count = 0;
			for(int j = 0; j < 6; ++j){
				if(matrix[j][i] == 1)
					++count;
			}
			
			if(count%2 == 1){
				for(int j = 0; j < 6; ++j){
					coor2[j][0] = j;
					coor2[j][1] = i;
				}
				break;
			}
		}
		
		int flag = 0;
		int mark = 0;
		for(int i = 0; i < 6; ++i){
			for(int j = 0; j < 6; ++j){
				if(coor1[i][0] == coor2[j][0] && coor1[i][1] == coor2[j][1]){
					flag = 1;
					mark = i;
					break;
				}
			}
			if(flag == 1)
				break;
		}
		if(flag == 1){
			System.out.println(coor1[mark][0] + "," + coor1[mark][1]);
		}
		
	}
}

24

package yongheng;
import java.util.Scanner;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) {
		int staSum = 45;
		int[][] matrix = new int[9][9];
		Scanner cin = new Scanner(System.in);
		int sum = 0;
		boolean flag = false;
		for(int i = 0; i < 9; ++i){
			sum = 0;
			for(int j = 0; j < 9; ++j){
				matrix[i][j] = cin.nextInt();
				sum += matrix[i][j];
			}
			if(sum != staSum)
				flag = true;
		}
		
		if(flag)
			System.out.println("invalid solution");
		else{
			for(int i = 0; i < 9; ++i){
				sum = 0;
				for(int j = 0; j < 9; ++j){
					sum += matrix[j][i];
				}
				if(sum != staSum){
					flag = true;
					break;
				}
			}
		}
		if(flag)
			System.out.println("invalid solution");
		else
			System.out.println("valid solution");
	}
}


25 26

。。。。。。


27

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		System.out.println("Enter a 3-by-3 matrix row by row:");
		double[][] matrix = new double[3][3];
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				matrix[i][j] = cin.nextDouble();
			}
		}
		
		matrix = sortColumns(matrix);
		
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	public static double[][] sortColumns(double[][] m){
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				for(int v = j+1; v < 3; ++v){
					if(m[j][i] > m[v][i]){
						double temp = m[j][i];
						m[j][i] = m[v][i];
						m[v][i] = temp;
					}
				}
			}
		}
		return m;
	}
}

28
import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		int[][] num1 = new int[3][3];
		int[][] num2 = new int[3][3];
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter list1:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j){
				num1[i][j] = cin.nextInt();
			}
		
		System.out.print("Enter list2:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j){
				num2[i][j] = cin.nextInt();
			}
		
		boolean flag = true;
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				if(num1[i][j] != num2[i][j]){
					flag = false;
					break;
				}
			}
		}

		System.out.println(flag);
	}
}

29

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		int[][] num1 = new int[3][3];
		int[][] num2 = new int[3][3];
		Scanner cin = new Scanner(System.in);
		System.out.print("Enter list1:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j){
				num1[i][j] = cin.nextInt();
			}
		
		System.out.print("Enter list2:");
		for(int i = 0; i < 3; ++i)
			for(int j = 0; j < 3; ++j){
				num2[i][j] = cin.nextInt();
			}
		
		System.out.println(equals(num1,num2));
	}
	
	public static boolean equals(int[][] m1, int[][] m2){
		int[] l1 = new int[9];
		int[] l2 = new int[9];
		
		int count = 0;
		for(int i = 0; i < 3; ++i){
			for(int j = 0; j < 3; ++j){
				l1[count] = m1[i][j];
				l2[count] = m2[i][j];
				++count;
			}
		}
		
		java.util.Arrays.sort(l1);
		java.util.Arrays.sort(l2);
		
		boolean flag = true;
		for(int i = 0; i < 9; ++i){
			if(l1[i] != l2[i]){
				flag = false;
				break;
			}
		}
		return flag;
	}
}

30 31 32 33

。。。。。

34

import java.util.Scanner;

public class Main{
	
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		System.out.println("Enter 6 points:");
		double[][] points = new double[6][2];
		
		for(int i = 0; i < 6; ++i){
			points[i][0] = cin.nextDouble();
			points[i][1] = cin.nextDouble();
		}
		
		double[] point = getRightmostLowestPoint(points);
		
		System.out.println(point[0] + "," + point[1]);
	}
	
	public static double[] getRightmostLowestPoint(double[][] points){
		double[][] sum = new double[6][3];
		for(int i = 0; i < 6; ++i){
			sum[i][0] = points[i][0];
			sum[i][1] = points[i][1];
			sum[i][2] = Math.abs(points[i][0]) + Math.abs(points[i][0]);
		}
		selectSort(sum);
		double[] result = sum[5];
		for(int i = 5; i >= 0; --i){
			if(sum[i][0] >= 0 && sum[i][1] <= 0){
				result = sum[i];
				break;
			}
		}
		return result;
		
	}
	
	public static void selectSort(double[][] num){
		for(int i = 0; i < 5; ++i){
			for(int j = i+1; j < 6; ++j){
				if(num[i][2] > num[j][2]){
					double[] temp = num[i];
					num[i] = num[j];
					num[j] = temp;
				}
			}
		}
	}
	
}

35

暴力枚举,虽然时间复杂度很高,但是这个题并没有时间限制。

//这是最大子矩阵
import java.util.Scanner;

public class Main
{
	public static void main(String args[]){
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		int temp = 0;
		int[][] matrix = new int[n][n];
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j){
				temp = cin.nextInt();
				if(temp == 0)
					matrix[i][j] = -100;
				else
					matrix[i][j] = 1;
			}
		}
		int res = maxSubMatrix(n,matrix);
		System.out.println(res);
	}
	
    public static int maxSubArray(int n,int a[])
    {
        int b=0,sum=-10000000;
        for(int i=0;i<n;i++)
        {
            if(b>0) b+=a[i];
            else b=a[i];
            if(b>sum) sum=b;
        }
        return sum;  
    }
     
    public static int maxSubMatrix(int n,int[][] array)
    {
        int i,j,k,max=0,sum=-100000000;
        int b[]=new int[101];
        for(i=0;i<n;i++)
        {
            for(k=0;k<n;k++)
            {
                b[k]=0;
            }
            for(j=i;j<n;j++)
            {
                for(k=0;k<n;k++)
                {
                    b[k]+=array[j][k];
                }
                max=maxSubArray(k,b); 
                
                if(max>sum)
           		{
               	    sum=max;
                }
           }
        }
        return sum;
    }
}

36

import java.util.Scanner;
import java.util.Arrays;

public class Main{
	
	public static void main(String args[]){
		System.out.print("Enter number n:");
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		char[][] matrix = new char[n][n];
		
		String temp = null;
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j){
				temp = cin.next();
				matrix[i][j] = temp.charAt(0);
			}
		}
		int[] record = new int[n];
		Arrays.fill(record, 0);
		
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j){
				++record[(int)(matrix[i][j]-'A')];
			}
		}
		
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j){
				++record[(int)(matrix[j][i] - 'A')];
			}
		}
		boolean flag = true;
		for(int i = 0; i < n; ++i){
			if(record[i] != 2*n){
				flag = false;
			}
		}
		
		System.out.println(flag);
	}
}

37

。。。。。。。



展开阅读全文

没有更多推荐了,返回首页