鱼C论坛

 找回密码
 立即注册
查看: 1875|回复: 1

求助数组编程题

[复制链接]
发表于 2021-11-9 19:46:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在本题中,你需要为每一个单元格统计出它周围的一样数字的个数,
每个单元格只统计上,下,左,右四个单元格,且每组测试样例都为4*4矩阵,
如下图,
输入样例为
2 2 4 4
2 3 5 7
4 3 5 8
4 5 8 8  
输出结果为
2 1 1 1
1 1 1 0
1 1 1 1
1 0 1 2


  1. package practice1109;

  2. import java.util.Scanner;

  3. public class Matrix01 {
  4.         public static void main(String[] args) {
  5.                 // TODO Auto-generated method stub
  6.                 int[][] m=new int[4][4];
  7.                 int i,j;
  8.                 Scanner sc=new Scanner(System.in);
  9.                
  10.                 for(i=0;i<4;i++) {
  11.                         for(j=0;j<4;j++) {
  12.                                 m[i][j]=sc.nextInt();
  13.                         }
  14.                 }
  15.                 /*
  16.                 for(i=0;i<4;i++) {
  17.                         for(j=0;j<4;j++) {
  18.                                 if(j==0) {
  19.                                         System.out.print(m[i][j]);
  20.                                 }
  21.                                 else {
  22.                                         System.out.print(" "+m[i][j]);
  23.                                 }
  24.                                 
  25.                                 if(j==3) {
  26.                                         System.out.println();
  27.                                 }
  28.                         }
  29.                 }*/
  30.                 int [][] res= new int [4][4];
  31.                
  32.                 for(i=0;i<4;i++) {
  33.                         for(j=0;j<4;j++) {
  34.                                 int count;
  35.                                 //四个角的
  36.                                 if(i==0&&j==0) {//[0][0]
  37.                                         count=0;
  38.                                         if(m[i][j]==m[i][j+1]) count++;
  39.                                         if(m[i][j]==m[i+1][j]) count++;
  40.                                         res[i][j]=count;
  41.                                 }
  42.                                 else if(i==0&&j==3) {//[0][3]
  43.                                         count=0;
  44.                                         if(m[i][j]==m[i][j-1]) count++;
  45.                                         if(m[i][j]==m[i+1][j]) count++;
  46.                                         res[i][j]=count;
  47.                                 }
  48.                                 else if(i==3&&j==0) {//[3][0]
  49.                                         count=0;
  50.                                         if(m[i][j]==m[i][j+1]) count++;
  51.                                         if(m[i][j]==m[i-1][j]) count++;
  52.                                         res[i][j]=count;
  53.                                 }
  54.                                 else if(i==3&&j==3) {//[3][3]
  55.                                         count=0;
  56.                                         if(m[i][j]==m[i][j-1]) count++;
  57.                                         if(m[i][j]==m[i-1][j]) count++;
  58.                                         res[i][j]=count;
  59.                                 }
  60.                                 //第一行 不是第一列和最后一列的元素
  61.                                 else if(i==0&&(j==1||j==2)) {//[0][1]  [0][2]
  62.                                         if(j==1) {
  63.                                                 count=0;
  64.                                                 if(m[i][j]==m[i][j-1]) count++;
  65.                                                 if(m[i][j]==m[i][j+1]) count++;
  66.                                                 if(m[i][j]==m[i+1][j]) count++;
  67.                                                 res[i][j]=count;
  68.                                         }
  69.                                         else if(j==2) {
  70.                                                 count=0;
  71.                                                 if(m[i][j]==m[i][j-1]) count++;//和左右下比
  72.                                                 if(m[i][j]==m[i][j+1]) count++;
  73.                                                 if(m[i][j]==m[i+1][j]) count++;
  74.                                                 res[i][j]=count;
  75.                                         }
  76.                                 }
  77.                                 //最后一行
  78.                                 else if(i==3&&(j==1||j==2)) {//[3][1]  [3][2]
  79.                                         if(j==1) {
  80.                                                 count=0;
  81.                                                 if(m[i][j]==m[i][j-1]) count++;
  82.                                                 if(m[i][j]==m[i][j+1]) count++;
  83.                                                 if(m[i][j]==m[i-1][j]) count++;
  84.                                                 res[i][j]=count;
  85.                                         }
  86.                                         else if(j==2) {
  87.                                                 count=0;
  88.                                                 if(m[i][j]==m[i][j-1]) count++;//和左右下比
  89.                                                 if(m[i][j]==m[i][j+1]) count++;
  90.                                                 if(m[i][j]==m[i-1][j]) count++;
  91.                                                 res[i][j]=count;
  92.                                         }
  93.                                 }
  94.                                 //第一列
  95.                                 else if(j==0&&(i==1||i==2)) {
  96.                                         if(i==1) {
  97.                                                 count=0;
  98.                                                 if(m[i][j]==m[i+1][j]) count++;
  99.                                                 if(m[i][j]==m[i-1][j]) count++;
  100.                                                 if(m[i][j]==m[i][j+1]) count++;
  101.                                                 res[i][j]=count;
  102.                                         }
  103.                                         else if(i==2) {
  104.                                                 count=0;
  105.                                                 if(m[i][j]==m[i+1][j]) count++;
  106.                                                 if(m[i][j]==m[i-1][j]) count++;
  107.                                                 if(m[i][j]==m[i][j+1]) count++;
  108.                                                 res[i][j]=count;
  109.                                         }
  110.                                 }
  111.                                 //最后列
  112.                                 else if(j==3&&(i==1||i==2)) {
  113.                                         if(i==1) {
  114.                                                 count=0;
  115.                                                 if(m[i][j]==m[i+1][j]) count++;
  116.                                                 if(m[i][j]==m[i-1][j]) count++;
  117.                                                 if(m[i][j]==m[i][j-1]) count++;
  118.                                                 res[i][j]=count;
  119.                                         }
  120.                                         else if(i==2) {
  121.                                                 count=0;
  122.                                                 if(m[i][j]==m[i+1][j]) count++;
  123.                                                 if(m[i][j]==m[i-1][j]) count++;
  124.                                                 if(m[i][j]==m[i][j-1]) count++;
  125.                                                 res[i][j]=count;
  126.                                         }
  127.                                 }
  128.                                 else {
  129.                                         count=0;
  130.                                         if(m[i][j]==m[i][j-1]) count++;//和左右上下比
  131.                                         if(m[i][j]==m[i][j+1]) count++;
  132.                                         if(m[i][j]==m[i-1][j]) count++;
  133.                                         if(m[i][j]==m[i+1][j]) count++;
  134.                                         res[i][j]=count;
  135.                                 }
  136.                                 
  137.                                 
  138.                 }}
  139.                 for(i=0;i<4;i++) {
  140.                         for(j=0;j<4;j++) {
  141.                                 if(j==0) {
  142.                                         System.out.print(res[i][j]);
  143.                                 }
  144.                                 else {
  145.                                         System.out.print(" "+res[i][j]);
  146.                                 }
  147.                                 
  148.                                 if(j==3) {
  149.                                         System.out.println();
  150.                                 }
  151.                         }
  152.                 }
  153.                
  154. }
  155. }
复制代码


我的方法:用Java写的,和C没有区别。
if-else 分类讨论一下四个角的元素和首行首列的元素,再把其他元素上下左右相邻的都讨论一遍,感觉方法好笨,代码冗余,有没有好一点的实现方法
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-9 20:14:31 | 显示全部楼层
本帖最后由 jhq999 于 2021-11-10 05:40 编辑
  1. 例如:数组[8][9],数组2[8][9]={0};
  2. [i][j]=>[i-1][j],[i][j-1],[i+1][j],[i][j+1];
  3. 范围(8>i+1 ,i-1>=0,j-1>=0,j+1<9;
  4. for(i=0;i<8;i++)
  5. {
  6.    for(j=0;j<9;j++)
  7. {
  8.    if(i+1<8)if(数组[i+1][j]==数组[i][j])数组2[i][j]++;
  9.   if(i-1)>=0)if(数组[i-1][j]==数组[i][j])数组2[i][j]++;
  10.   if(j-1)>=0)if(数组[i][j-1]==数组[i][j])数组2[i][j]++;
  11.   if(j+1)<9)if(数组[i][j+1]==数组[i][j])数组2[i][j]++;
  12. }
  13. }
  14. 输出数组2
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 17:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表