public class Solution
    {
        public int MatrixScore(int[][] A)
        {
            var row = A.GetLength(0);
            var col = A[0].GetLength(0);
            //判断最高位是否为1
            for (int i = 0; i < row; i++)
            {
                if (!(A[i][0] == 1))//不是1,则移动此行
                {
                    for (int j = 0; j < col; j++)
                    {
                        if (A[i][j] == 0)
                        {
                            A[i][j] = 1;
                        }
                        else
                        {
                            A[i][j] = 0;
                        }
                    }
                }
            }

            //遍历每一列,计算0和1的数量,如果0多,则移动
            for (int j = 0; j < col; j++)
            {
                var sum0 = 0;
                var sum1 = 0;
                for (int i = 0; i < row; i++)
                {
                    if (A[i][j] == 0)
                    {
                        sum0++;
                    }
                    else
                    {
                        sum1++;
                    }
                }
                if (sum0 > sum1)
                {
                    for (int i = 0; i < row; i++)
                    {
                        if (A[i][j] == 0)
                        {
                            A[i][j] = 1;
                        }
                        else
                        {
                            A[i][j] = 0;
                        }
                    }
                }
            }

            //计算最后的二进制数
            var sum = 0;
            for (int i = 0; i < row; i++)
            {
                var rowsum = 0;
                for (int j = col - 1; j >= 0; j--)
                {
                    var cur = A[i][j] * (int)Math.Pow(2, col - 1 - j);
                    rowsum += cur;
                }
                sum += rowsum;
            }

            return sum;
        }
    }

 

posted on 2018-10-09 11:31  Sempron2800+  阅读(125)  评论(0编辑  收藏  举报