厉掣 2023-07-25 10:07 采纳率: 100%
浏览 77
已结题

Java矩阵问题求解

java矩阵问题求解

假设我们有一个5*5倍的整数矩阵。现在让我们来设计一个游戏规则。对于每一次,矩阵中的每个整数都将变成它周围的整数的和(不包括它本身)。让我们来看一个例子。 初始状态如下示:

1 0 0 0 0

1 0 0 0 0

1 0 0 0 0

1 0 0 0 0

1 0 0 0 0

对于下一步,矩阵应该是这样的:

2 3 0 0 3

2 3 0 0 3

2 3 0 0 3

2 3 0 0 3

2 3 0 0 3

现在我们给出了一个初始矩阵和步数n。我们需要你返回两个整数,代表重复的数量的值矩阵最高的数量(如果有值与相同数量的重复,输出最大的)的重复(排除初始状态)和重复的数量n步骤,分别。现在,请逐步完成以下问题。

import java.util.HashMap;

public class AddingMatrix {

public static int[][] state = {

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0}};

public static int n = 10;

public static HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();

}

问题1:实现一个函数:公共静态int返回Sum(intx,inty){} 输入两个整数,x和y。返回状态周围的整数之和。 示例:输入: x=2,y=4,输出:3

问题2:实现一个函数:公共静态空白goNextState(){} 当调用此函数时,状态变量应该更改为下一个状态的值。 例如:如果我们现在调用这个函数,状态的值应该改变如下所示:


state = {

{2, 3, 0, 0, 2},

{2, 3, 0, 0, 2},

{2, 3, 0, 0, 2},

{2, 3, 0, 0, 2},

{2, 3, 0, 0, 2}};

问题3:在每个状态的更新后,我们需要在HashMap中计算当前状态的值。请实现一个函数来更新HashMap计数。该函数应该类似于这样的:public static void updateCount(){}

示例:在第一个状态的更新后,计数的值应该类似于这个: {0=10,2=5,3=10}

问题4:借助上述功能,完成主功能内的整个问题。

import java.util.HashMap;

public class AddingMatrix {

public static int[][] state = {

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0},

{1, 0, 0, 0, 0}};

public static int n = 10;

public static HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();

public static int returnSurroundSum(int x, int y){

int sum = 0;

int[] _x = {-1, -1, -1, 0, 0, 1, 1, 1};

int[] _y = {-1, 0, 1, -1, 1, -1, 0, 1};

for(int i = 0; i < 8; i++){

}

return sum;

}

public static void goNextState(){

int[][] tempState = new int[5][5];

for (int i = 0; i < 5; i++)

}

public static void printState(int[][] intput){

for (int i = 0; i < 5; i++){

for ()

System.out.print(intput[i][j]);

System.out.println();

}

}

public static void updateCount(){

for (int i = 0; i < 5; i++){

for (int j = 0; j < 5; j++){

}

}

}

public static void main(String[] args) {

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

}

int aimVal = -1, aimCount = -1;

for (int key : count.keySet()) {

if (count.get(key) > aimCount){

}

if (count.get(key) == aimCount && aimVal < key)

aimVal = key;

}

System.out.println(aimVal);

System.out.println(aimCount);

}

}



  • 写回答

3条回答 默认 最新

  • 藏柏 2023-07-25 13:24
    关注

    首先贴代码

    
    public class AddingMatrix {
    
        public static int[][] state = {
                {1, 0, 0, 0, 0},
                {1, 0, 0, 0, 0},
                {1, 0, 0, 0, 0},
                {1, 0, 0, 0, 0},
                {1, 0, 0, 0, 0}
        };
    
        public static int n = 1;
    
        public static HashMap<Integer, Integer> count = new HashMap<>();
    
        public static void main(String[] args) {
            System.out.println("初始矩阵:");
            printState(state);
            System.out.println(returnSurroundSum(2, 4));
    
            for (int i = 0; i < n; i++) {
                goNextState();
                updateCount();
            }
    
            System.out.println("经过一次变换后的矩阵:");
            printState(state);
            System.out.println(returnSurroundSum(2, 4));
    
    
            int aimVal = -1, aimCount = -1;
    
            for (int key : count.keySet()) {
                if (count.get(key) > aimCount) {
                    aimCount = count.get(key);
                    aimVal = key;
                } else if (count.get(key) == aimCount && key > aimVal) {
                    aimVal = key;
                }
            }
    
            System.out.println("最高重复的值:" + aimVal);
            System.out.println("最高重复的次数:" + aimCount);
    
        }
    
        public static void goNextState() {
            int[][] nextState = new int[5][5];
    
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    nextState[i][j] = returnSurroundSum(i, j);
                }
            }
    
            state = nextState; // 将nextState赋值给state,更新矩阵为下一步的状态
        }
    
        public static int returnSurroundSum(int x, int y) {
            int sum = 0;
            int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
            int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};
    
            for (int i = 0; i < 8; i++) {
                int newX = x + dx[i];
                if (newX < 0) {
                    newX = 4;
                }
                if (newX >= 5) {
                    newX = 0;
                }
                int newY = y + dy[i];
                if (newY < 0) {
                    newY = 4;
                }
                if (newY >= 5) {
                    newY = 0;
                }
                if (newX >= 0 && newX < state.length && newY >= 0 && newY < state[0].length) {
                    sum += state[newX][newY];
                }
            }
    
            return sum;
        }
    
        public static void updateCount() {
            count.clear();
    
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    int value = state[i][j];
                    if (value != 0) {
                        count.put(value, count.getOrDefault(value, 0) + 1);
                    }
                }
            }
        }
    
        public static void printState(int[][] matrix) {
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    System.out.print(matrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月2日
  • 已采纳回答 7月25日
  • 创建了问题 7月25日

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上