【问题标题】:Why does it print "NO" even after so many conditions attached to it?为什么即使附加了这么多条件,它也会打印“NO”?
【发布时间】:2020-05-10 03:40:39
【问题描述】:

我在 Hyperskill 上发现了这个问题,并且一直在努力解决这个问题。解决方案也在那里,但对我来说毫无意义。

在某些设计风格中,如果 4x4 矩阵图案不是由相同颜色的 2x2 矩阵组成,则它被认为看起来很漂亮。你的任务是编写程序,如果 4x4 矩阵看起来很漂亮,则输出“YES”,否则输出“NO”。

输入包含4行,每行包含4个符号,不同的符号代表不同的颜色:W代表白色,B代表黑色,R代表红色,G代表绿色,Y代表黄色。

示例输入 1: 万维网 BBBB 万维网 年年年年

示例输出 1: 是的

示例输入 2: BBBB BWWB BWWB BBBB

示例输出 2: 没有

    String[] arr = new String[4];
    for (int i = 0; i < 4; i++) {
        arr[i] = scanner.next();
    }

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (arr[i].charAt(j) == arr[i + 1].charAt(j) && arr[i].charAt(j) == arr[i].charAt(j + 1)
                    && arr[i].charAt(j) == arr[i + 1].charAt(j + 1)) {
                System.out.println("NO");
                return;
            }
        }
    }

    System.out.println("YES");

【问题讨论】:

  • 那到底是什么问题呢?
  • 4x4 矩阵“看起来很漂亮”如何?
  • @Kayaman 这就是我的想法!但也许 OP 有不同的口味
  • 我不明白问题的标题。它打印NO,因为所有这些条件都为真。即使有一百万个条件,如果它们都评估为真,它仍然会打印NO。这就是编程逻辑的工作原理。有问题的算法是最明显的算法,比较矩阵中每个位置内相同颜色的 2x2 块。
  • 我不明白这个解决方案如何工作以提供所需的结果......

标签: java arrays for-loop


【解决方案1】:

我会给出我的解决方案并解释它为什么有效,我希望这会有所帮助。

String x="";
while(scanner.hasNextLine()){
    x+=scanner.nextLine();
}
//The above just combines the entire thing into one string length 16 to make it faster to code

boolean yes= true;
//preseted to true, if a uniformly colored 2by2 is found this becomes false

int[] topLefts = {0,1,2,4,5,6,8,9,10,12,13,14} 
//top Left corner of each possible 2 by 2 matrix

for(int i: top Lefts){//iterating over an array
    if(x.charAt(i)==x.charAt(i+1)&&x.charAt(i+4)==x.charAt(i+1)
         &&x.charAt(i+4)==x.charAt(i+5)) yes = false;
}
//This checks if the chars representing each box in the 2by2 are the same
//If even one 2by2 is colored the same, yes becomes true.

if(yes) System.out.println("YES");
else System.out.println("NO");

【讨论】:

  • 这甚至不能编译。并且还有运行时异常java.lang.StringIndexOutOfBoundsException
【解决方案2】:

当所有“附加到它的许多条件”都匹配时,它会打印“NO”。 该程序创建一个包含 4 个字母的 4 个字符串的数组。然后它依次检查整个矩阵,如下所示:

if (1.1 == 2.1 && 1.1 == 1.2 && 1.1 == 2.2) 
    print "NO"
  • _。 1. 2 3 4
  • 1. X X B B
  • 2。 X X DB
  • 3。 B C D B
  • 4。 B B B B

然后它向右移动 1 并再次检查,直到到达字符串的末尾。发生这种情况后,它向下移动 1 并从头开始检查字符串 2 和 3。字符串 3 和 4 相同。 如果程序找到相同字符的 2x2 矩阵,它会认为整个矩阵“不漂亮”并打印“NO”。

【讨论】:

    【解决方案3】:

    这是我的解决方案。我希望这会有所帮助:

    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            char[][] c = new char[4][4];
            for (int i = 0; i < 4; i++) {
                c[i] = s.next().toCharArray();
            }
            boolean pretty = true;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (c[i][j] == c[i][j + 1] &&
                            c[i][j] == c[i + 1][j] &&
                            c[i][j] == c[i + 1][j + 1]) {
                        pretty = false;
                    }
                }
            }
            System.out.println(pretty ? "YES" : "NO");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2020-09-10
      • 2020-05-19
      相关资源
      最近更新 更多