}
================================================================
package demo;
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[][] m1 = new double[3][3];
double[][] m2 = new double[3][3];
System.out.println(“Enter matrix1:”);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
m1[i][j] = input.nextDouble();
System.out.println(“Enter matrix2:”);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
m2[i][j] = input.nextDouble();
double[][] nums = multiMatrix(m1, m2);
System.out.println(m1[0][0] + " " + m1[0][1] + " " + m1[0][2] + " " + m2[0][0] + " " + m2[0][1] + " "
- m2[0][2] + " " + nums[0][0] + " " + nums[0][1] + " " + nums[0][2]);
System.out.println(m1[1][0] + " " + m1[1][1] + " " + m1[1][2] + " * " + m2[1][0] + " " + m2[1][1] + " "
- m2[1][2] + " = " + nums[1][0] + " " + nums[1][1] + " " + nums[1][2]);
System.out.println(m1[2][0] + " " + m1[2][1] + " " + m1[2][2] + " " + m2[2][0] + " " + m2[2][1] + " "
- m2[2][2] + " " + nums[2][0] + " " + nums[2][1] + " " + nums[2][2]);
}
public static double[][] multiMatrix(double[][] a,double[][] b){
int rows = a.length;
int columns = b[0].length;
double[][] nums = new double[rows][columns];
for(int i=0;i<rows;i++)
for(int j=0;j<rows;j++){
for(int k=0;k<a[0].length;k++)
nums[i][j]+=a[i][k]*b[k][j];
}
return nums;
}
}
================================================================
package demo;
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of points: ");
int num = input.nextInt();
System.out.println("Enter " + num + " points: ");
double[][] points = getArray(num);
int p1 = 0;
int p2 = 1;
double shortDistance = distance(points[p1][0], points[p1][1], points[p1][2], points[p2][0], points[p2][1],
points[p2][2]);
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[i][2], points[j][0], points[j][1],
points[j][2]);
if (shortDistance > distance) {
p1 = i;
p2 = j;
shortDistance = distance;
}
}
}
System.out.println(“Two recent point (” + points[p1][0] + ", " + points[p1][1] + ", " + points[p1][2] + “)and(”
- points[p2][0] + ", " + points[p2][1] + ", " + points[p2][2] + “)”);
System.out.println("The distance is " + shortDistance);
}
public static double[][] getArray(int n) {
Scanner input = new Scanner(System.in);
double[][] points = new double[n][3];
for (int i = 0; i < points.length; i++)
for (int j = 0; j < 3; j++)
points[i][j] = input.nextDouble();
return points;
}
public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
}
}
================================================================
开始看错题目了,费了好大劲写了个三维的,结果看题是二维,人都傻了。。。。现在,又改过来了。
package demo;
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the point of number: ");
int num = input.nextInt();
System.out.println("Enter " + num + " points: ");
double[][] points = getArray(num);
int p1 = 0;
int p2 = 1;
double shortDistance = 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 (shortDistance > distance) {
p1 = i;
p2 = j;
shortDistance = distance;
}
}
}
System.out.println("Their distance is " + shortDistance);
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 == shortDistance) {
p1 = i;
p2 = j;
System.out.println(“The closest two points are (” + points[p1][0] + ", " + points[p1][1] + “)and(”
- points[p2][0] + ", " + points[p2][1] + “)”);
}
}
}
System.out.println("Their distance is " + shortDistance);
}
public static double[][] getArray(int n) {
Scanner input = new Scanner(System.in);
double[][] points = new double[n][2];
for (int i = 0; i < points.length; i++)
for (int j = 0; j < 2; j++)
points[i][j] = input.nextDouble();
return points;
}
public static double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
}
================================================================
package demo;
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char[][] str = new char[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
str[i][j] = ‘a’;
int xo1 = 0;
char[] xo2 ={‘X’,‘O’};
while(true){
printMap(str);
int row;
int column;
boolean a = true;
while(a){
System.out.println(“Enter a row (0,1, or 2) for player “+xo2[xo1%2]+”:”);
row = input.nextInt();
System.out.println(“Enter a column (0,1, or 2) for player “+xo2[xo1%2]+”:”);
column = input.nextInt();
if(str[row][column] != ‘X’&&str[row][column] != ‘O’){
str[row][column]=xo2[xo1%2];
break;
}
System.out.print(“it has been selected,pick another!\n”);
}
xo1++;
if(checkWin(str,‘X’)){
System.out.println(“X player won”);
break;
}else if(checkWin(str,‘O’)){
System.out.println(“O player won”);
break;
}
}
}
public static void printMap(char[][] str){
System.out.println(“…”);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(“|”);
if(str[i][j]==‘a’)
System.out.print(" ");
else
System.out.print(str[i][j]);
}
System.out.println(“|”);
System.out.println(“…”);
}
}
public static boolean checkWin(char[][] str,char xo){
if(xo==‘X’){
for(int i=0;i<3;i++)
if(str[i][0]‘X’&&str[i][1]‘X’&&str[i][2]==‘X’)
return true;
for(int i=0;i<3;i++)
if(str[0][i]‘X’&&str[1][i]‘X’&&str[2][i]==‘X’)
return true;
if(str[0][0]‘X’&&str[1][1]‘X’&&str[2][2]==‘X’)
return true;
if(str[0][2]‘X’&&str[1][1]‘X’&&str[2][0]==‘X’)
return true;
return false;
}else{
for(int i=0;i<3;i++)
if(str[i][0]‘O’&&str[i][1]‘O’&&str[i][2]==‘O’)
return true;
for(int i=0;i<3;i++)
if(str[0][i]‘O’&&str[1][i]‘O’&&str[2][i]==‘O’)
return true;
if(str[0][0]‘O’&&str[1][1]‘O’&&str[2][2]==‘O’)
return true;
if(str[0][2]‘O’&&str[1][1]‘O’&&str[2][0]==‘O’)
return true;
return false;
}
}
}
=================================================================
package demo;
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {
int[][] ary = new int[4][4];
for (int i = 0; i < ary.length; i++) {
for (int j = 0; j < ary[i].length; j++) {
ary[i][j] = (int) (Math.random() * 2);
System.out.print(ary[i][j]);
}
System.out.println();
}
int maxR = 0;
int maxC = 0;
int row = 0;
int col = 0;
for (int i = 0; i < ary.length; i++) {
int numR = 0;
int numC = 0;
for (int j = 0; j < ary[i].length; j++) {
if (ary[i][j] == 1) {
numR++;
}
if (ary[j][i] == 1) {
numC++;
}
}
if (numR > maxR) {
maxR = numR;
row = i;
}
if (numC > maxC) {
maxC = numC;
col = i;
}
}
System.out.println("The largest row index: " + (row + 1));
System.out.println("The largest column index: " + (col + 1));
}
}
=================================================================
package demo;
import java.util.Scanner;
public class dibazhang {
public static void main(String[] args) {