java算法 文库,java笔试算法题及答案.doc

253b171540df25e1b84436cbe50dfc72.gifjava笔试算法题及答案.doc

1现在输入 n 个数字,以逗号,分开;然后可选择升或者降序排序 我的评论 本题的 splitStringByCommaString 方法纯属多余,可以用 String 的 split 方法一句话代替,且可读性也更强,下面的一段话源自 JDK1.6API, StringTokenizer 类已不再提倡使用,保留仅为旧代码。 StringTokenizer 是出于兼容性的原因而被保留的遗留类(虽然在新代码中并不鼓励使用它)。建议所有寻求此功能的人使用 String 的 split 方法或 java.util.regex 包 。 import java.util.*; public class bycomma public static String splitStringByCommaString source ifsourcenullsource.trim.equals return null; StringTokenizer commaToker new StringTokenizersource,“,“; String result new StringcommaToker.countTokens; int i0; whilecommaToker.hasMoreTokens resulti commaToker.nextToken; i; return result; public static void mainString args String s splitStringByComma“5,8,7,4,3,9,1“; int ii new ints.length; forint i 0; i0;i System.out.printlniii; 2.编写一个截取字符串的函数,输入为一个字符串和 字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如 “我 ABC“4,应该截为 “我 AB“,输入 “我 ABC 汉 DEF“,6,应该输出为 “我 ABC“而不是 “我 ABC汉的半个 “。 代码 public static boolean isLetterchar c int k0X80; return c/k0truefalse; public static int lengthsString strSrc if strSrcnull return 0; int len0; char strCharstrSrc.toCharArray; for int i0;ilengthsorigin return origin; byte strBytenew bytelen; System.arraycopyorigin.getBytes,0,strByte,0,len; int count0; for int i0;i1 partitiondata,low,k-1; if high-k1 partitiondata,k1,high; public static int partitionint data int low,int high, int pivot do while datalowpivot; swapdata,low,high; while low 0; i String tmp origin.substringi - 1, i; result tmp; return result; 10、给你一组字符如 1, 3, 4, 7, 2, 1, 1, 5, 2,让你输出里面出现次数最多且数值最大的一个,出现几次 public void fun4 int a 4, 1, 2, 4, 5, 1, 1, 1, 5, 1, 3, 4, 5 ; Arrays.sorta; for int i 0; i 0; i curNumber ai; if ai ai - 1 curCount; else System.out.println“i“ i “,curCount“ curCount “,maxCount“ maxCount “,maxNumber“ maxNumber; if curCount maxCount maxCount curCount; maxNumber curNumber; curCount 1; if curCount maxCount maxCount curCount; maxNumber curNumber; System.out.println“curCount“ curCount “,maxCount“ maxCount “,maxNumber“ maxNumber; 11、求两个数的公约数, M, N int divisor 1; for int i 2; i aj1 int temp aj; aj aj1; aj1 temp; Conica.printa; 14、编程显示某一文件目录下的文件名 public void fun10 File file new File“G03月份 “; iffile.exists iffile.isDirectory String files file.list; Conica.printlnfiles; 15、从键盘输入 4个十进制数字字 符,将其转换为 4位时间之数并显示出来 16、编程实现统计文本文件中某个单词的出现频率,并输出统计结果 用 HashMap 来解决 假设单词不存在跨行的 ,每个单词用 ,. ;分割 public static void countNum throws IOException BufferedReader br null; try br new BufferedReadernew FileReader“cfile.txt“; Map map new HashMap; for String s br.readLine; s null; s br.readLine StringTokenizer st new StringTokenizers, “,. ;“; while st.hasMoreTokens String temp st.nextToken; if map.containsKeytemp map.puttemp, new IntegerIntegermap.gettemp 1; else map.puttemp, new Integer1; for Iterator it map.entrySet.iterator; it.hasNext; Map.Entry entry Map.Entry it.next; System.out.printlnentry.getKey entry.getValue “times“; finally br.close; 17、编程模仿 DOS 下的 dir 命令,列出某个目录下的内容 18、编程说明 String 和 StringBuffer 字符串的区别 19、编程计算 N的程序,一个使用递归方法,一个不用递归方法 递归 long fuctionint n if n0 return 1; else return n* fuctionn-1; 不递 long s1; forint i2;i max maxString os; maxList.addos; else if value max maxList.addos; int index 0; for int i 0; i (一千零一拾一元整)输出。 package test.money; import java.text.Numberat; import java.util.HashMap; public class SimpleMoneyat public static final String EMPTY ; public static final String ZERO “零 “; public static final String ONE “壹 “; public static final String TWO “贰 “; public static final String THREE “叁 “; public static final String FOUR “肆 “; public static final String FIVE “伍 “; public static final String SIX “陆 “; public static final String SEVEN “柒 “; public static final String EIGHT “捌 “; public static final String NINE “玖 “; public static final String TEN “拾 “; public static final String HUNDRED “佰 “; public static final String THOUSAND “仟 “; public static final String TEN_THOUSAND “万 “; public static final String HUNDRED_MILLION “亿 “; public static final String YUAN “元 “; public static final String JIAO “角 “; public static final String FEN “分 “; public static final String DOT “.“; private static SimpleMoneyat atter null; private HashMap chineseNumberMap new HashMap; private HashMap chineseMoneyPattern new HashMap; private Numberat numberat Numberat.getInstance; private SimpleMoneyat numberat.setMaximumFractionDigits4; numberat.setMinimumFractionDigits2; numberat.setGroupingUsedfalse; chineseNumberMap.put“0“, ZERO; chineseNumberMap.put“1“, ONE; chineseNumberMap.put“2“, TWO; chineseNumberMap.put“3“, THREE; chineseNumberMap.put“4“, FOUR; chineseNumberMap.put“5“, FIVE; chineseNumberMap.put“6“, SIX; chineseNumberMap.put“7“, SEVEN; chineseNumberMap.put“8“, EIGHT; chineseNumberMap.put“9“, NINE; chineseNumberMap.putDOT, DOT; chineseMoneyPattern.put“1“, TEN; chineseMoneyPattern.put“2“, HUNDRED; chineseMoneyPattern.put“3“, THOUSAND; chineseMoneyPattern.put“4“, TEN_THOUSAND; chineseMoneyPattern.put“5“, TEN; chineseMoneyPattern.put“6“, HUNDRED; chineseMoneyPattern.put“7“, THOUSAND; chineseMoneyPattern.put“8“, HUNDRED_MILLION; public synchronized static SimpleMoneyat getInstance if atter null atter new SimpleMoneyat; return atter; public String atString moneyStr checkPrecisionmoneyStr; String result; result convertToChineseNumbermoneyStr; result addUnitsToChineseMoneyStringresult; return result; public String atdouble moneyDouble return atnumberat.atmoneyDouble; public String atint moneyInt return atnumberat.atmoneyInt; public String atlong moneyLong return atnumberat.atmoneyLong; public String atNumber moneyNum return atnumberat.atmoneyNum; private String convertToChineseNumberString moneyStr String result; StringBuffer cMoneyStringBuffer new StringBuffer; for int i 0; i 0; i cMoneyStringBuffer.inserti, chineseMoneyPattern.getEMPTY moneyPatternCursor; moneyPatternCursor moneyPatternCursor 8 1 moneyPatternCursor 1; String fractionPart cMoneyStringBuffer.substringcMoneyStringBuffer .indexOf“.“; cMoneyStringBuffer.deletecMoneyStringBuffer.indexOf“.“, cMoneyStringBuffer.length; while cMoneyStringBuffer.indexOf“零拾 “ -1 inclusive. exclusive. cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零拾 “, cMoneyStringBuffer.indexOf“零拾 “ 2, ZERO; while cMoneyStringBuffer.indexOf“零佰 “ -1 cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零佰 “, cMoneyStringBuffer.indexOf“零佰 “ 2, ZERO; while cMoneyStringBuffer.indexOf“零仟 “ -1 cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零仟 “, cMoneyStringBuffer.indexOf“零仟 “ 2, ZERO; while cMoneyStringBuffer.indexOf“零万 “ -1 cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零万 “, cMoneyStringBuffer.indexOf“零万 “ 2, TEN_THOUSAND; while cMoneyStringBuffer.indexOf“零亿 “ -1 cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零亿 “, cMoneyStringBuffer.indexOf“零亿 “ 2, HUNDRED_MILLION; while cMoneyStringBuffer.indexOf“零零 “ -1 cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零零 “, cMoneyStringBuffer.indexOf“零零 “ 2, ZERO; if cMoneyStringBuffer.lastIndexOfZERO cMoneyStringBuffer.length - 1 cMoneyStringBuffer.deletecMoneyStringBuffer.length - 1, cMoneyStringBuffer.length; cMoneyStringBuffer.appendfractionPart; result cMoneyStringBuffer.toString; return result; private String addUnitsToChineseMoneyStringString moneyStr String result; StringBuffer cMoneyStringBuffer new StringBuffermoneyStr; int indexOfDot cMoneyStringBuffer.indexOfDOT; cMoneyStringBuffer.replaceindexOfDot, indexOfDot 1, YUAN; cMoneyStringBuffer.insertcMoneyStringBuffer.length - 1, JIAO; cMoneyStringBuffer.insertcMoneyStringBuffer.length, FEN; if cMoneyStringBuffer.indexOf“零角零分 “ -1 没有零头,加整 cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零角零分 “, cMoneyStringBuffer.length, “整 “; else if cMoneyStringBuffer.indexOf“零分 “ -1 没有零分,加整 cMoneyStringBuffer.replacecMoneyStringBuffer.indexOf“零分 “, cMoneyStringBuffer.length, “整 “; else if cMoneyStringBuffer.indexOf“零角 “ -1 cMoneyStringBuffer.deletecMoneyStringBuffer.indexOf“零角 “, cMoneyStringBuffer.indexOf“零角 “ 2; tmpBuffer.append“整 “; result cMoneyStringBuffer.toString; return result; private void checkPrecisionString moneyStr 5336.53663 10-5-1 int fractionDigits moneyStr.length - moneyStr.indexOfDOT - 1; if fractionDigits 2 throw new RuntimeException“金额 “ moneyStr “的小数位多于两位。 “; 精度不能比分低 public static void mainString args System.out.printlngetInstance.atnew Double8951.11; .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值