【急】请问三道题目用java怎么做

2025-01-02 23:22:12
推荐回答(2个)
回答1:

package reptile;

import java.util.Scanner;

public class TestMultiplyDemo
{

public static void main(String[] args)
{
// TODO Auto-generated method stub
int n;
System.out.println("请输入魔方单行或者单列的值");

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

System.out.println("请输入魔方的每个位置的数值 以空格隔开");
sc = new Scanner(System.in);
String string = sc.nextLine();
int[][] m = new int[n][n];

System.out.println(string + "!!!!");
String[] strings = string.split(" ");
System.out.println(strings.length);
int k = 0;
boolean flag = true;

int minsum = (1 + n*n)*n/2;
for(int i = 0; i < n; i++){
int rowSum = 0;
for(int j = 0; j < n; j++){
m[i][j] = Integer.parseInt(strings[k]);
k++;
rowSum += m[i][j];
}
if(rowSum != minsum){
flag = false;
}
if(!flag){
break;
}
}

for(int i = 0; i < n; i++){
int lineSum = 0;
for(int j = 0; j < n; j++){
//m[i][j] = Integer.parseInt(strings[k]);
k++;
lineSum += m[j][i];
}
if(lineSum != minsum){
flag = false;
}
if(!flag){
break;
}
}

int X1Sum = 0;
int X2Sum = 0;
for(int i = 0; i < n; i++){

for(int j = 0; j < n; j++){
//m[i][j] = Integer.parseInt(strings[k]);
k++;
if(i == j){
X1Sum += m[j][i];
}
if(i + j == n -1){
X2Sum += m[j][i];
}
}

}
if(X1Sum != minsum || X2Sum != minsum){
flag = false;
}

if(flag){
System.out.println("这个正方形是魔方阵");
}else{
System.out.println("这个正方向不是魔方阵");
}

}

}
魔方先提交了 我觉得这个最难

package reptile;

import java.text.DecimalFormat;
import java.util.Scanner;

public class TestGradeAdd
{

public static void main(String[] args)
{
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat("######0.00");

Scanner sc = new Scanner(System.in);
System.out.println("请输入你 要输入个加数个数");
int n = sc.nextInt();
int[] m = new int[n];
double sum = 0;
for(int i = 0; i < n; i++){
m[i] = sc.nextInt();
System.out.println("输入" + m[i]);
double temp = getResult(m[i]);
df.format(temp);
sum += temp;
}
System.out.println("最后的结果是" + sum);
}

public static double getResult(int n){

double sum = 0;

for(int i = 1; i <= n;i++){
double temp = 1.0/i;

sum +=temp;
}
return sum;
}

}

回答2:

做了一个上午,挺费脑子的.