Problem1: Magic Square
实验环境:
- java: java 9.0.4
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode) - git: git version 2.16.1.windows.1
- IDE: intellij IDEA
实验要求:
part1:
检验一个矩阵是否为Magic Square:
- 每一行、每一列、两条对角线数字加和相等
- 每个数字必须为正整数
- 每一行的数字之间必须用‘\t’分割
part2:
public static boolean generateMagicSquare(int n) { int magic[][] = new int[n][n]; int row = 0, col = n / 2, i, j, square = n * n; for (i = 1; i <= square; i++) {magic[row][col] = i; if (i % n == 0) row++; else { if (row == 0) row = n - 1; else row--; if (col == (n - 1)) col = 0; else col++; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) System.out.print(magic[i][j] + "\t"); System.out.println(); } return true;
}
实验思路:
part1:
- 用fileInput方法读入文件,并判断上述合法性的2、3点以及是否是一个矩阵(行、列数是否一致)
- 对file文件尝试读取,若读入为空则输出”File Null!“,并退出程序
- 每次读取一行,读取第一行后用string.spilt(”\t“)对该行进行分割,形成字符串数组strings_first
- 获得strings_first.length记为n,默认为该矩阵列数,并实例化二位数组array
- 对strings_first中每一个字符串做正则表达式匹配判断是,否为正整数,若有非正整数则输出”not a Integer!“并退出
- 循环读完该文件并重复进行2、3、4操作,若某一行的数字数不等于n,则说明分隔符错误,输出”Se'parator error!“并退出
- 每读一次记录下行数m,在最后判断m和n是否相等,若不相等则输出”Matrix error!“并退出
static boolean fileInput(File file)
- 在isLegalMagicSquare方法中判断矩阵的每一行、每一列、两条对角线加和是否相等
static boolean isLegalMagicSquare(String fileName)
实验收获:
- 注释的使用,在自己的IDEA中已经设置完毕
- static的使用
源代码
Problem1: Magic Square相关推荐
- cf369 B Chris and Magic Square
B. Chris and Magic Square time limit per test2 seconds memory limit per test256 megabytes inputstand ...
- Chris and Magic Square CodeForces - 711B
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...
- B. Chris and Magic Square
B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 文章标题 Chris and Magic Square
ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】
B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces 711B- Chris and Magic Square
B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Chris and Magic Square
Chris and Magic Square ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There ...
- CodeForces - 711B Chris and Magic Square
CodeForces - 711B Chris and Magic Square 题意:给你一个N*N矩阵,其中0代表未知的那个数,让你在这个位置填上一个数使整个矩阵的 每一行.每一列.主对角线 ...
- C#,幻方(Magic Square)的算法与源代码
什么是幻方? 幻方(Magic Square)是一种将数字安排在正方形格子中,使每行.列和对角线上的数字和都相等的方法. 幻方也是一种中国传统游戏.旧时在官府.学堂多见.它是将从一到若干个数的自然数排 ...
最新文章
- 大咖来信|浪潮刘军:AI计算将成为“新基建”核心支撑之一
- 先睹为快:Visual Studio 11测试版已于2.29在微软官方网站正式发布
- 常考数据结构与算法:容器盛水问题
- NFS mount.nfs: access denied by server while mounting 一个解决办法
- Fedora换源:换成aliyun镜像源
- 随机验证码。 * 随机生成十组六位字符组成的验证码。 * 验证码由大小写字母、数字字符组成。
- 动态改变eachers图表高_让你的Excel图表动起来
- django orm关联查询_Django ORM 聚合查询和分组查询实现详解
- asp.net使用include包含文件中文乱码_C++: 编写自己的头文件
- IE9 和 IE11 安装及相关补丁
- php使用blob加密视频,javascript实现blob加密视频源地址的方法
- python win32api sendmessage_win32api win32gui win32con 窗口句柄 发送消息 常用方法
- 一个真实的Windows XP SP 3镜像包 571MB大小
- SpringBoot自动装配的魔力
- MyBatisPlus-基础CRUD操作
- 忍者安全渗透系统(NINJITSU OS V3)的安装详细过程,亲测新旧vm版本都可安装,附带下载来源
- 人脸识别:路在何方?| 爱莫受邀参加VALSE Webinar报告会
- 986-Golang的chan数据结构
- debian配置ip
- 在Linux中查找和删除重复文件的4种方法