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);
}
}
首先贴代码
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();
}
}
}
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
问题1:实现一个函数:公共静态int返回Sum(intx,inty){} 输入两个整数,x和y。返回状态周围的整数之和。 示例:输入: x=2,y=4,输出:3
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++){
int newX = x + _x[i];
int newY = y + _y[i];
if(newX >= 0 && newX < 5 && newY >= 0 && newY < 5){
sum += state[newX][newY];
}
}
return sum;
}
问题2:实现一个函数:公共静态空白goNextState(){} 当调用此函数时,状态变量应该更改为下一个状态的值。 例如:如果我们现在调用这个函数,状态的值应该改变如下所示:
public static void goNextState(){
int[][] tempState = new int[5][5];
for (int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
tempState[i][j] = returnSurroundSum(i, j) - state[i][j];
}
}
state = tempState;
}
问题3:在每个状态的更新后,我们需要在HashMap中计算当前状态的值。请实现一个函数来更新HashMap计数。该函数应该类似于这样的:public static void updateCount(){}
public static void updateCount(){
count.clear(); // 清空计数器
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
if (state[i][j] != 0) {
count.put(state[i][j], count.getOrDefault(state[i][j], 0) + 1); // 更新计数器
}
}
}
}
问题4:借助上述功能,完成主功能内的整个问题。
public static void main(String[] args) {
for (int i = 0; i < n; i++){
goNextState(); // 更新状态
updateCount(); // 更新计数器
}
int aimVal = -1, aimCount = -1;
for (int key : count.keySet()) {
if (count.get(key) > aimCount){
aimCount = count.get(key);
aimVal = key;
}
if (count.get(key) == aimCount && aimVal < key){
aimVal = key;
}
}
System.out.println(aimVal);
System.out.println(aimCount);
}
这个程序会先进行 n 次更新状态和计数器操作,然后遍历计数器找到出现次数最多的数值(如果有多个数值出现次数相同,输出最大的那个)。最后输出这个数值和它的出现次数。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
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();
}
}
}