xocoder提升之路:

来源:互联网 发布:2017nba新秀前10数据 编辑:程序博客网 时间:2024/05/16 17:48

测试题

题1:计算一个数的阶乘

public class Factorial {@Testpublic void testFactorial() {Factorial jc = new Factorial();for (int i = 0; i < 10; i++) {System.out.println(i + "!=" + jc.jc(i));}}public int jc(int i) {if (i <= 1)return 1;elsereturn i * jc(i - 1);}}

题2:在一个字符串中查找第一个非重复的字符

public class FindFirstNoRepeatStr {String s = "undefined behaviour";/** * 解决大部分情况,但是在临界处可能还存在问题 * 通过的临界情况有: * "undefinedundefined " * "undefined undefined " */@Test// 最原始方式:不利用任何的数据结构public void getFirstNonRepeatedCharFromArray() {char[] cs = s.toCharArray();// String --> Char[]int length = s.length();boolean[] count = new boolean[length];//一旦发现相同字符,就修改标记状态for (int i = 0; i < length; i++) {count[i] = false;}for (int i = 0; i < length; i++) {for (int j = 0; j < length; j++) {//★必须全部扫描,不能从i+1个处扫描,否则count[i]可能为true-->falseif (cs[i] == cs[j]&&i!=j) {count[i]=true;}}}for (int i = 0; i < length; i++) {/*必须加“=”,否则如果是最后一个为“第一个非重复字符”将验证不出来还有一种思路就是标记已经检测出来的字符,这种方式是以空间换时间*/if (count[i]==false) {System.out.println(cs[i]);return;//退出整个循环,与上面的continue不同}if(i==length-1){System.out.println("不含非重复字符");return ;}}}@Testpublic void testfirstNonRepeatedCharacter(){char a = new FindFirstNoRepeatStr().firstNonRepeatedCharacter(s);System.out.println(a); //f}//第一种方式public char getFirstNonRepeatedChar(String str) {Map<Character, Integer> counts = new LinkedHashMap<Character, Integer>(str.length());for (char c : str.toCharArray()) {counts.put(c, counts.containsKey(c) ? counts.get(c) + 1 : 1);}for (Entry<Character, Integer> entry : counts.entrySet()) {if (entry.getValue() == 1) {return entry.getKey();}}throw new RuntimeException("didn't find any non repeated Character");}//第二种方式public char firstNonRepeatingChar(String word) {Set<Character> repeating = new HashSet<Character>();List<Character> nonRepeating = new ArrayList<Character>();for (int i = 0; i < word.length(); i++) {char letter = word.charAt(i);if (repeating.contains(letter)) {continue;}if (nonRepeating.contains(letter)) {nonRepeating.remove((Character) letter);repeating.add(letter);} else {nonRepeating.add(letter);}}return nonRepeating.get(0);}//第三种方式public char firstNonRepeatedCharacter(String word) {HashMap<Character, Integer> scoreboard = new HashMap<Character, Integer>();// build table [char -> count]for (int i = 0; i < word.length(); i++) {char c = word.charAt(i);if (scoreboard.containsKey(c)) {scoreboard.put(c, scoreboard.get(c) + 1);} else {scoreboard.put(c, 1);}}// since HashMap doesn't maintain order, going through string againfor (int i = 0; i < word.length(); i++) {char c = word.charAt(i);if (scoreboard.get(c) == 1) {return c;}}throw new RuntimeException("Undefined behaviour");}}

题3:计算时钟与分钟的夹角:6:58

public class MainTestAng {public static void main(String[] args) {MainTestAng mta = new MainTestAng(6,58);mta.printAng();}private float hour;private float minute;MainTestAng(float h,float m){this.hour = h;this.minute = m;}public void printAng(){float minAng,houAng;//分钟与0点之间的夹角if(minute<=30){minAng = minute/5*30;}else{minAng = 360-(minute/5*30);}//时钟与0点之间的夹角if(hour<6){houAng = hour*30+(minute/60*30);}else{houAng =  360-hour*30-(minute/60*30);}//求出最终夹角System.out.println(Math.abs(houAng - minAng));}}



细节加深:

//1. 测试方法必须有public void开头,方法不含任何参数(没有public会报错)//2. 构造方法必须没有返回值,public可写可不写//3. 字符串转为字符数组String s = "mytest from zhonghua=";char[] cs = s.toCharArray();//String转字符数组:String --> Char[]//4. 定义并初始化数组//通过直接给定数组初始值初始化数组int num[] = { 0 }; //这里num数组中只有 一个元素num[0] = 2;num[1] = 1;//报错:ArrayIndexOfBoundsException//初始化固定长度的数组,其默认值依据数组的类型而定int[] intArray = new int[10]; //★★System.out.println(intArray.length)//求解text数组长度length的同时,检测数组不为空的高级写法if (null == text || (length = text.length()) == 0) 



0 0