锁定老帖子 主题:几个笔试题目(2010-09-18)
精华帖 (1) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-09-19
99直线解:
p(n) = 2 (n=1) p(n) = p(n-1)+n (n>1) |
|
返回顶楼 | |
发表时间:2010-09-19
bonny 写道 i5cn 写道 yangkunlin 写道 基础题:通项公式为an = a(n-1)+n;a1=2;则a999=999001 fn(x=1) = 2 fn(x) = fn(x-1) + x = fn(x-2) + x-1 + x = fn(x-3) + x-2 + x-1 + x = fn(x-4) + x-3 + x-2 + x-1 + x = x*(x+1)/2 + 1 fn(x=999) = 499501 应该是2(N*(N-1)/2+1) = 997004 结果好像是:fn(x=999) = 499501 |
|
返回顶楼 | |
发表时间:2010-09-19
最后修改:2010-09-19
那道钱转换为中文的,贴一个我的解法:用最简方法获得中文,然后再进一步处理中文(“零”问题)
class NumberToChinse { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Long number = scanner.nextLong(); //开始处理 System.out.println(trim(conversion(number))); } /*转换*/ public static String conversion(Long number){ String [] lib1 = {"零","一","二","三","四","五","六","七","八","九"}; String [] lib2 = {"","十","百","千","万","十","百","千","亿","十","百","千","兆","十","百","千"}; Map<Integer,String> map1 = new HashMap<Integer,String>(); //汉字映射 Map<Integer,String> map2 = new HashMap<Integer,String>(); //单位映射 for (int i = 0; i < lib2.length; i++) { map2.put(i+1, lib2[i]); if (i<10)map1.put(i, lib1[i]); } char[] str_number = String.valueOf(number).toCharArray(); //得到输入数字的字符串数组 String result = ""; for (int i = 0; i < str_number.length; i++) { result += map1.get(Integer.valueOf(String.valueOf(str_number[i]))) + map2.get(str_number.length-i); } return result; } /*处理“零”问题*/ private static String trim(String result) { result = result.replaceAll("(零.零$)+", ""); result = result.replaceAll("(零.零)+(.)", "零$2"); //可斟酌 return result; } } ========== |
|
返回顶楼 | |
发表时间:2010-09-19
第一道题换种思路
称不让它清零 把1和2放上去,然后把2分别换3,4……10,如果那个重量变化了,就是哪箱,如果都没有变化,就是第一箱 不知道这样行不行 |
|
返回顶楼 | |
发表时间:2010-09-19
cesul 写道 那道钱转换为中文的,贴一个我的解法:用最简方法获得中文,然后再进一步处理中文(“零”问题) class NumberToChinse { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Long number = scanner.nextLong(); //开始处理 System.out.println(trim(conversion(number))); } /*转换*/ public static String conversion(Long number){ String [] lib1 = {"零","一","二","三","四","五","六","七","八","九"}; String [] lib2 = {"","十","百","千","万","十","百","千","亿","十","百","千","兆","十","百","千"}; Map<Integer,String> map1 = new HashMap<Integer,String>(); //汉字映射 Map<Integer,String> map2 = new HashMap<Integer,String>(); //单位映射 for (int i = 0; i < lib2.length; i++) { map2.put(i+1, lib2[i]); if (i<10)map1.put(i, lib1[i]); } char[] str_number = String.valueOf(number).toCharArray(); //得到输入数字的字符串数组 String result = ""; for (int i = 0; i < str_number.length; i++) { result += map1.get(Integer.valueOf(String.valueOf(str_number[i]))) + map2.get(str_number.length-i); } return result; } /*处理“零”问题*/ private static String trim(String result) { result = result.replaceAll("(零.零$)+", ""); result = result.replaceAll("(零.零)+(.)", "零$2"); //可斟酌 return result; } } ========== 是一种蛮好的思路,不走传统路线。不过在零问题出来上有点问题。比如:1230013转换会成为:一百二十三万零百一十三。 |
|
返回顶楼 | |
发表时间:2010-09-19
cr0w 写道 第一道题换种思路 称不让它清零 把1和2放上去,然后把2分别换3,4……10,如果那个重量变化了,就是哪箱,如果都没有变化,就是第一箱 不知道这样行不行 你这样相当于称了很多次,题目要求只能称一次。 |
|
返回顶楼 | |
发表时间:2010-09-19
polaris1119 写道 stupidmxx 写道 看到这段的时候,我就囧了
chineseMap.put(0, "零"); //... chineseMap.put(9, "九"); 您就不会String[] chineseMappingArray = {"零",...,"九"}; 么? 呵呵。谢谢。 String chineseMappingArray = "零...九"; 这样不是更好么? |
|
返回顶楼 | |
发表时间:2010-09-19
public class MoneyTrans { private static String[] ChinaDigit = {"零","一","二","三","四","五","六","七","八","九"}; private static String[] UNIT = {"","","十","百","千"}; private static String[] BIGUNIT = {"","万","亿"}; private char[] digit; public String trans(int n){ StringBuffer buff = new StringBuffer(); digit = String.valueOf(n).toCharArray(); int length = digit.length; int pos = (length - 1)/4; int headLength = (length - 1)%4 + 1; buff.append(partTrans(0,headLength) + BIGUNIT[pos--]); for (int i = headLength;i < length ; i = i + 4) { buff.append(partTrans(i , i + 4) + BIGUNIT[pos--]) ; } return buff.toString(); } private String partTrans(int start, int end) { StringBuffer buff = new StringBuffer(); boolean isPreDigitZero = false; for (int i = start; i < end; i++) { int cur = digit[i] - '0'; if(cur != 0 ){ if(isPreDigitZero == true){ buff.append(ChinaDigit[0]); } buff.append(ChinaDigit[cur] + UNIT[end - i]); isPreDigitZero = false; } else { isPreDigitZero = true; } } return buff.toString(); } public static void main(String[] args) { MoneyTrans transtor = new MoneyTrans(); String money = transtor.trans(23005602); System.out.println(money); } } |
|
返回顶楼 | |
发表时间:2010-09-19
polaris1119 写道 yangkunlin 写道 基础题:通项公式为an = a(n-1)+n;a1=2;则a999=999001
关键这个是需要证明的。结果我想很多人都会。证明应该有点麻烦。 这不是高斯定理吗,高斯8岁的时候就证明过了吧!。。。 |
|
返回顶楼 | |
发表时间:2010-09-19
hackpro 写道 polaris1119 写道 yangkunlin 写道 基础题:通项公式为an = a(n-1)+n;a1=2;则a999=999001 关键这个是需要证明的。结果我想很多人都会。证明应该有点麻烦。 这不是高斯定理吗,高斯8岁的时候就证明过了吧!。。。 高斯有这样的定理?我孤弱寡闻了。一般这么证明:(1)(2)a1=2成立,假设ak = a(k-1) +k成立,证明a(k+1) = ak + (k+1)成立。这里的问题主要是,如何证明这步?也就是如何确定知道当从k条直线变为k+1条直线增加了多少部分。 |
|
返回顶楼 | |