解题思路:
用矩阵存放输入的数据;
输入的时候记录每个数出现的次数
从0开始到9结束
找这个数字在矩阵中出现的最边缘位置(即找该数字出现在矩阵的最靠右下角和左上角的位置的坐标)
用这个坐标计算出所谓矩形的面积 面积内所包含的元素数与之前记录的元素数是否相同,不相同直接输出NO
注意事项:
参考代码:
import java.util.*; public class 整理玩具 { static boolean isOk(int ma[][],int n,int m,int times[]) {//判断符不符合规定 for(int i = 0; i < 10;++i) { if(times[i] <=1)continue;//如果只出现一次或者0次 直接跳过就好 int maxrow = 0;//出现位置行最大值 int minrow = 100;//行最小值 int maxcol = 0;//出现位置的列最大值 int mincol = 100;//列最小值 //遍历矩阵 找对角线两点的坐标 for(int row = 0;row < n;++row) { for(int col = 0; col < m;++col) { if(ma[row][col] !=i)continue; //更新 if(row>maxrow)maxrow = row; if(row<minrow)minrow = row; if(col>maxcol)maxcol = col; if(col<mincol)mincol = col; } } //如果记录的次数与矩形面积不符直接返回false if(times[i]!=(maxcol-mincol+1)*(maxrow-minrow+1))return false; } return true; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int num = sc.nextInt();//需要判断的个数 for(int i = 0; i < num;++i) { int n = sc.nextInt();//N int m = sc.nextInt();//M sc.nextLine();//吞掉回车 int times[] = new int[10];//记录每个数字出现的次数 int ma[][] = new int [n][m]; for(int row = 0; row < n;++row) { String s = sc.nextLine();//一下子读一行 for(int col = 0;col<m;++col) { String temp = s.substring(col, col+1);//取出一个值 int tempInt = Integer.parseInt(temp);//转成int times[tempInt]++;//该数出现的次数+1 ma[row][col] = tempInt;//赋给矩阵 } } if(isOk(ma,n,m,times) == true)System.out.println("YES"); else System.out.println("NO"); } } }
0.0分
2 人评分
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:589 |
钟神赛车 (C语言代码)浏览:869 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:608 |
计算质因子 (C++代码)浏览:1617 |
简单的a+b (C语言代码)浏览:543 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:713 |
【偶数求和】 (C语言代码)浏览:432 |
小九九 (C语言代码)浏览:500 |
1063题 初学者,求帮忙看下,不知道哪错了浏览:232 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:502 |