第八章第二十三题(游戏:找到翻转的单元格)(Game: find flipped cells)

第八章第二十三题(游戏:找到翻转的单元格)(Game: find flipped cells)

  • *8.23(游戏:找到翻转的单元格)假设给定一个填满0和1的6×6矩阵,所有的行和列都有偶数个1。让用户翻转一个单元(即从1翻成0或者从0翻成1),编写一个程序,找到哪个单元格被翻转了。程序应该提示用户输入一个6x6的填满0和1的矩阵,并且找到一个不符合具有偶数个1的特征的r行以及c列(即1的数目不是偶数),则该翻转的单元格位于(r,c)。下面是一个运行示例:
    Enter a 6-by-6 matrix row by row:
    1 1 1 0 1 1
    1 1 1 1 0 0
    0 1 0 1 1 1
    1 1 1 1 1 1
    0 1 1 1 1 0
    1 0 0 0 0 1
    The flipped cell is at(0,1)
    *8.23(Game: find flipped cells)Given a 6 × 6 matrix filled with 0 and 1, all rows and columns have even ones. Let the user flip a cell (from 1 to 0 or from 0 to 1), write a program to find which cell has been flipped. The program should prompt the user to enter a 6x6 matrix filled with 0 and 1, and find a row R and column C that do not meet the characteristic of even 1 (i.e. the number of 1 is not even), then the flipped cell is located in (R, c). Here is a running example:
    Enter a 6-by-6 matrix row by row:
    1 1 1 0 1 1
    1 1 1 1 0 0
    0 1 0 1 1 1
    1 1 1 1 1 1
    0 1 1 1 1 0
    1 0 0 0 0 1
    The flipped cell is at(0,1)

  • 参考代码:

    package chapter08;
    
    import java.util.Scanner;
    
    public class Code_23 {
        public static void main(String[] args) {
    
            Scanner input=new Scanner(System.in);
            System.out.println("Enter a 6-by-6 matrix row by row:");
            int[][] matrix =new int[6][6];
    
            for(int i=0;i<matrix.length;i++){
                for (int j=0;j<matrix[i].length;j++){
                    matrix[i][j]=input.nextInt();
                }
            }
    
            System.out.println("The flipped cell is at("+findTheRow(matrix)+","+findTheColumn(matrix)+")");
        }
        public static int findTheRow(int[][] matrix){
            int countRow=0;
            int row=0;
            for(int i=0;i<matrix.length;i++){
                for(int j=0;j<matrix[i].length;j++){
                    if (matrix[i][j]==1)
                        countRow++;
                }
                if(countRow%2!=0){
                    row=i;
                    break;
                }
            }
            return row;
        }
        public static int findTheColumn(int[][] matrix){
            int countColumn=0;
            int column=0;
    
            for(int j=0;j<matrix[0].length;j++) {
                for (int i = 0; i < matrix.length; i++) {
                    if (matrix[i][j] == 1)
                        countColumn++;
                }
                if (countColumn % 2 != 0) {
                    column = j;
                    break;
                }
            }
    
            return column;
        }
    }
    
    
  • 结果显示:

    Enter a 6-by-6 matrix row by row:
    1 1 1 0 1 1 
    1 1 1 1 0 0
    0 1 0 1 1 1
    1 1 1 1 1 1 
    0 1 1 1 1 0
    1 0 0 0 0 1
    The flipped cell is at(0,1)
    
    Process finished with exit code 0
    
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值