实验环境:

  1. 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)
  2. git:     git version 2.16.1.windows.1
  3. IDE:      intellij IDEA

实验要求:

part1:

检验一个矩阵是否为Magic Square:

  1. 每一行、每一列、两条对角线数字加和相等
  2. 每个数字必须为正整数
  3. 每一行的数字之间必须用‘\t’分割
给出5个文件    1.txt、2.txt、……5.txt,分别检验其是否为Magic Square,结果返回true或false;

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点以及是否是一个矩阵(行、列数是否一致)
在该方法中,操作如下:
  1. 对file文件尝试读取,若读入为空则输出”File Null!“,并退出程序
  2. 每次读取一行,读取第一行后用string.spilt(”\t“)对该行进行分割,形成字符串数组strings_first
  3. 获得strings_first.length记为n,默认为该矩阵列数,并实例化二位数组array
  4. 对strings_first中每一个字符串做正则表达式匹配判断是,否为正整数,若有非正整数则输出”not a Integer!“并退出
  5. 循环读完该文件并重复进行2、3、4操作,若某一行的数字数不等于n,则说明分隔符错误,输出”Se'parator error!“并退出
  6. 每读一次记录下行数m,在最后判断m和n是否相等,若不相等则输出”Matrix error!“并退出
static boolean fileInput(File file)
  • 在isLegalMagicSquare方法中判断矩阵的每一行、每一列、两条对角线加和是否相等
static boolean isLegalMagicSquare(String fileName)

实验收获:

  1. 注释的使用,在自己的IDEA中已经设置完毕
  2. static的使用

源代码

Problem1: Magic Square相关推荐

  1. cf369 B Chris and Magic Square

    B. Chris and Magic Square time limit per test2 seconds memory limit per test256 megabytes inputstand ...

  2. 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 ...

  3. B. Chris and Magic Square

    B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. 文章标题 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 ...

  5. 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 ...

  6. 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 ...

  7. Chris and Magic Square

    Chris and Magic Square ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There ...

  8. CodeForces - 711B Chris and Magic Square

    CodeForces - 711B  Chris and Magic Square   题意:给你一个N*N矩阵,其中0代表未知的那个数,让你在这个位置填上一个数使整个矩阵的 每一行.每一列.主对角线 ...

  9. C#,幻方(Magic Square)的算法与源代码

    什么是幻方? 幻方(Magic Square)是一种将数字安排在正方形格子中,使每行.列和对角线上的数字和都相等的方法. 幻方也是一种中国传统游戏.旧时在官府.学堂多见.它是将从一到若干个数的自然数排 ...

最新文章

  1. 大咖来信|浪潮刘军:AI计算将成为“新基建”核心支撑之一
  2. 先睹为快:Visual Studio 11测试版已于2.29在微软官方网站正式发布
  3. 常考数据结构与算法:容器盛水问题
  4. NFS mount.nfs: access denied by server while mounting 一个解决办法
  5. Fedora换源:换成aliyun镜像源
  6. 随机验证码。 * 随机生成十组六位字符组成的验证码。 * 验证码由大小写字母、数字字符组成。
  7. 动态改变eachers图表高_让你的Excel图表动起来
  8. django orm关联查询_Django ORM 聚合查询和分组查询实现详解
  9. asp.net使用include包含文件中文乱码_C++: 编写自己的头文件
  10. IE9 和 IE11 安装及相关补丁
  11. php使用blob加密视频,javascript实现blob加密视频源地址的方法
  12. python win32api sendmessage_win32api win32gui win32con 窗口句柄 发送消息 常用方法
  13. 一个真实的Windows XP SP 3镜像包 571MB大小
  14. SpringBoot自动装配的魔力
  15. MyBatisPlus-基础CRUD操作
  16. 忍者安全渗透系统(NINJITSU OS V3)的安装详细过程,亲测新旧vm版本都可安装,附带下载来源
  17. 人脸识别:路在何方?| 爱莫受邀参加VALSE Webinar报告会
  18. 986-Golang的chan数据结构
  19. debian配置ip
  20. 在Linux中查找和删除重复文件的4种方法

热门文章

  1. 商越程序:企业在采购数字化变革设计思路上的“四个改变”
  2. Java后台框架篇--Struts与SSH
  3. php教程燕十八_2PHP 3小时光速入门视频教程【燕十八】
  4. 学习新时代编程语言Rust-14一箱裤子
  5. python+selenium实现网易邮箱登陆
  6. Nginx 配置示例
  7. 给自己的博客园装饰萌萝莉(live2d)
  8. 怎样写一个拼写检查器
  9. 山西民生养老认证显示服务器异常,民生山西养老认证
  10. 场内基金和场外基金区别