成都山而网站建设公司,内部网站建设软件,巩义郑州网站建设,香河建设局网站概述 
算法是一个程序员的核心竞争力#xff0c;也是面试最重要的考查环节。 
试题 
判断一个罗马数字是否有效 
罗马数字包含七种字符#xff1a;I#xff0c;V#xff0c;X#xff0c;L#xff0c;C#xff0c;D和M#xff0c;如下 
字符数值I1V5X10L50C100D500M1000…概述 
算法是一个程序员的核心竞争力也是面试最重要的考查环节。 
试题 
判断一个罗马数字是否有效 
罗马数字包含七种字符IVXLCD和M如下 
字符数值I1V5X10L50C100D500M1000
规则 
不能出现连续相同的4个及以上字符但是IIII4MMMM4000除外不能存在特定组合如DD因为DDM小数字只能在大数字的右边表示这些数字相加得到的数但存在违反上述规则的特例小数字在大数字的左边表示大数减小数得到的数只适用于以下六种情况 I 可以放在 V(5) 和 X (10) 的左边表示4和9X 可以放在 L(50) 和 C (100) 的左边表示40和90C 可以放在 D(500) 和 M (1000) 的左边表示400和900  
在线阿拉伯数字和罗马数字互相转换 https://www.onlineconversion.com/roman_numerals_advanced.htm The input must be in the range of 1 - 4999, or I to MMMMCMXCIX. 罗马数字最大只能表示到4999。 
比如输入LVIIID系统提示LVIIID is not a valid input  https://www.luomashuzi.com/  使用正则表达式来判断一个给定的字符串是不是合法的罗马数字字符串 
public static boolean isRoman(String s) {return !s.isEmpty() s.matches(M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}));
}阿拉伯数字转罗马数字 
将一个阿拉伯数字转换成罗马数字显然需要考虑罗马数字的有效性问题。 
public static String convertToRoman(int num) {if (num  5000) {return ;}String result  ;if (num  1000) {result  repeat(M, (int) Math.floor(1.0 * num / 1000));num % 1000;}if (num  100) {result  geneBase((int) Math.floor(1.0 * num / 100), new String[]{C, D, M});num % 100;}if (num  10) {result  geneBase((int) Math.floor(1.0 * num / 10), new String[]{X, L, C});num % 10;}if (num  1) {result  geneBase((int) Math.floor(num), new String[]{I, V, X});}return result;
}private static String geneBase(int num, String[] arr) {String result  ;if (num  1  num  3) {result  repeat(arr[0], num);}if (num  4) {result  arr[0]    arr[1];}if (num  5  num  8) {result  arr[1]    repeat(arr[0], num - 5);}if (num  9) {result  arr[0]    arr[2];}return result;
}/*** 重复多次*/
private static String repeat(String str, int count) {StringBuilder result  new StringBuilder();for (int i  0; i  count; i) {result.append(str);}return result.toString();
}罗马数字转数字 
给定一个罗马数字表示的字符串将其转换成数字即阿拉伯数字。 
注可以假定给定的罗马数字字符串是合法的。 
public static int romanToInt(String s) {int n  0;for (int i  0; i  s.length(); ) {char c  s.charAt(i);if (c  I) {if (i  1  s.length()) {if (s.charAt(i  1)  V) {n  4;i  2;} else if (s.charAt(i  1)  X) {n  9;i  2;} else {n  1;i;}} else {n  1;i;}} else if (c  X) {if (i  1  s.length()) {if (s.charAt(i  1)  L) {n  40;i  2;} else if (s.charAt(i  1)  C) {n  90;i  2;} else {n  10;i;}} else {n  10;i;}} else if (c  C) {if (i  1  s.length()) {if (s.charAt(i  1)  D) {n  400;i  2;} else if (s.charAt(i  1)  M) {n  900;i  2;} else {n  100;i;}} else {n  100;i;}} else if (c  V) {n  5;i;} else if (c  L) {n  50;i;} else if (c  D) {n  500;i;} else if (c  M) {n  1000;i;}}return n;
}数字转Excel列 
给定一个数字将其转换成Excel里面的列。 
背景知识 
Excel的行是用阿拉伯数字表示列则用大写英文字母表示Excel 2003最多有65536行256列Excel 2007、2010最多有1048576行16384列列用A到Z表示第27列是AA第53列是BA第26  26 * 26  1列是AAA以此类推……最后一列是XFD  
public static String convertToTitle(int n) {if (n  0) {return ;}StringBuilder sb  new StringBuilder();while (n  0) {n--;sb.append((char) (n % 26  A));n  n / 26;}return sb.reverse().toString();
}Excel列转数字 
public static int excelColumnNameToNumber(String columnName) {int sum  0;if (columnName.equals()) {return sum;}for (int i  0; i  columnName.length(); i) {sum * 26;sum  (columnName.charAt(i) - A  1);}return sum;
}参考 
检查罗马数字的有效性 文章转载自: http://www.morning.rtlth.cn.gov.cn.rtlth.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.cbqqz.cn.gov.cn.cbqqz.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.nmymn.cn.gov.cn.nmymn.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.rqqn.cn.gov.cn.rqqn.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.jtkfm.cn.gov.cn.jtkfm.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.ryjqh.cn.gov.cn.ryjqh.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.zrhhb.cn.gov.cn.zrhhb.cn http://www.morning.bfmrq.cn.gov.cn.bfmrq.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.dqxnd.cn.gov.cn.dqxnd.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn