贵阳网络公司网站建设,公网ip 做网站,中国专门做统计网站,营销型网站建设申请域名一、题目 给你两个字符串 coordinate1 和 coordinate2#xff0c;代表 8 x 8 国际象棋棋盘上的两个方格的坐标。   以下是棋盘的参考图。    如果这两个方格颜色相同#xff0c;返回 true#xff0c;否则返回 false。   坐标总是表示有效的棋盘方格。坐标的格式总是先… 
一、题目 给你两个字符串 coordinate1 和 coordinate2代表 8 x 8 国际象棋棋盘上的两个方格的坐标。   以下是棋盘的参考图。    如果这两个方格颜色相同返回 true否则返回 false。   坐标总是表示有效的棋盘方格。坐标的格式总是先字母表示列再数字表示行。 
二、示例 
示例 1
输入 coordinate1  a1, coordinate2  c3
输出 true
解释
两个方格均为黑色。示例 2
输入 coordinate1  a1, coordinate2  h3
输出 false
解释
方格 a1 是黑色而 h3 是白色。提示 
coordinate1.length  coordinate2.length  2‘a’  coordinate1[0], coordinate2[0]  ‘h’‘1’  coordinate1[1], coordinate2[1]  ‘8’ 
三、思路 把 coordinate1 和 coordinate2 简记为 s 和 t。 根据题目中的图片如果 s[0] 和 s[1] 的 ASCII 值的奇偶性相同那么格子是黑格否则是白格。 进一步地由于 奇数  奇数  偶数偶数  偶数  偶数所以如果 (s[0]  s[1]) mod 2 是偶数那么格子是黑格否则 奇数  偶数  奇数格子是白格。 如果 (s[0]  s[1]) mod 2  (t[0]  t[1]) mod 2那么两个格子颜色相同否则不同。 
四、解法 
package com.example.demo.test;public class CheckTwoChessboards {public boolean checkTwoChessboards(String coordinate1, String coordinate2) {// 查找差值int dif1  coordinate2.charAt(0) - coordinate1.charAt(0);int dif2  coordinate2.charAt(1) - coordinate1.charAt(1);return (dif1  1)  (dif2  1);}public static boolean checkTwoChessboards1(String coordinate1, String coordinate2) {// 查找差值int dif1  Math.abs(coordinate2.charAt(0) - coordinate1.charAt(0));int dif2  Math.abs(coordinate2.charAt(1) - coordinate1.charAt(1));// 奇偶性相同return (dif1 % 2)  (dif2 % 2);}public static boolean checkTwoChessboards2(String coordinate1, String coordinate2) {int dif1  coordinate1.charAt(0)  coordinate1.charAt(1);int dif2  coordinate2.charAt(0)  coordinate2.charAt(1);// 奇偶性相同return (dif1 % 2)  (dif2 % 2);}public static void main(String[] args) {String a  h7;String b  c8;System.out.println(checkTwoChessboards1(a, b));}
}
 文章转载自: http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.ckctj.cn.gov.cn.ckctj.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.kwnnx.cn.gov.cn.kwnnx.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.xtdtt.cn.gov.cn.xtdtt.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn