首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java经典习题

2012-08-25 
java经典练习package com.jia.mouimport java.io.UnsupportedEncodingExceptionimport java.text.Simple

java经典练习

package com.jia.mou;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Lingxing {/** * 数组a[m];b[n]都是已经排序好的数组(下面的程序是升序),要求合并成一个有序的数组c[m+n] *  * @param a * @param b * @return */public static int[] sort(int[] a, int[] b) {int m = 0;int n = 0;int i = 0, j = 0;m = a.length;n = b.length;int[] c = new int[m + n];for (; i < m;) {for (; j < n;) {if (a[i] > b[j]) {c[i + j] = b[j];j++;} else {c[i + j] = a[i];i++;break;}}if (i == m - 1 && j < n - 1) {for (; j < n; j++) {c[i + j] = b[j];}}if (j == n - 1 && i < m - 1) {for (; i < m; i++) {c[i + j] = a[i];}}}// 打印cfor (int k = 0; k < m + n; k++) {System.out.println("c[" + k + "]=" + c[k]);}return c;}/** *  * 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 * 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: * (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 (2)如果n <> * k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。 * (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 *  * @param n */public static void fenjie(int n) {System.out.print(n + "=1");int k = 2;if (n == 2) {System.out.print("*2");} else {while (n >= k) {if (n % k == 0) {System.out.print("*" + k);n = n / k;} else {k++;}}}}/** * 获得随机日期 *  * @param startDate * @param endDate * @return */public static Date randomDate(String startDate, String endDate) {try {SimpleDateFormat format = new SimpleDateFormat();Date start = format.parse(startDate);Date end = format.parse(endDate);if (start.getTime() >= end.getTime()) {return null;}long date = random(start.getTime(), end.getTime());return new Date(date);} catch (Exception e) {e.printStackTrace();}return null;}public static long random(long begin, long end) {long rtn = begin + (long) (Math.random() * (end - begin));if (rtn == begin || rtn == end) {return random(begin, end);}return rtn;}/** * 找出一个字符串中的中文部分 */public static String getChineseStr(String str) {String chineseStr = "";char[] ch = str.toCharArray();for (int i = 0; i < ch.length; i++) {try {if (isChineseChar(ch[i])) {chineseStr += ch[i];} else {chineseStr += " ";}} catch (UnsupportedEncodingException e) {e.printStackTrace();}}System.out.println("输出中文部分:" + chineseStr);return chineseStr;}/* 下面是判断一个字符是否是中文的方法 */public static boolean isChineseChar(char c)throws UnsupportedEncodingException {return String.valueOf(c).getBytes("GBK").length > 1;}/** * 获得菱形 *  */public static void getLinxing() {// TODO Auto-generated method stubint i = 0;int j = 0;int k = 0;for (i = 0; i < 5; i++) {for (j = 0; j < 5 - i; j++) {System.out.print(" ");}for (k = 0; k < 2 * i + 1; k++) {System.out.print("*");}System.out.println("");}for (i = 1; i < 5; i++) {for (j = 0; j < i + 1; j++) {System.out.print(" ");}for (k = 0; k < 2 * (5 - i) - 1; k++) {System.out.print("*");}System.out.println("");}}/** * 计算一篇文章中不同单词出现的次数 */public static void word_count() {String str = "Hello pan 1 hello ya 2 feng";Pattern pattern = Pattern.compile("[a-zA-Z]+");// 定义正则表达式匹配单词// Pattern pattern = Pattern.compile("[a-zA-Z]");str = str.toLowerCase();// 转化为小写Matcher matcher = pattern.matcher(str);// 定义str的匹配器Map myHashMap = new HashMap();// 使用map有这么一个问题:hello是第一个出现的,但在第二次出现的时候在map中的位置就不是第一个了int n = 0;// 文章中单词的总数Object word = null;// 文章中的单词Object num = null;// 出现的次数while (matcher.find()) {// 是否匹配单词word = matcher.group();// 得到一个单词-树映射的键n++;if (myHashMap.containsKey(word)) {// 如果包含该键,单词出现过num = myHashMap.get(word);// 得到单词出现的次数Integer count = (Integer) num;// 强制转化myHashMap.put(word, new Integer(count.intValue() + 1));} else {myHashMap.put(word, new Integer(1));// 否则单词第一次出现,添加到映射中}}@SuppressWarnings("rawtypes")Iterator iter = myHashMap.keySet().iterator();// 得到树映射键集合的迭代器Object key = null;System.out.println("单词总数:" + n + "个,其中不同的有 " + myHashMap.size() + "个:");while (iter.hasNext()) {// 使用迭代器遍历树映射的键key = iter.next();System.out.println(key + " 有" + myHashMap.get(key) + "个;");}}/** * 50个人围成一个圈数数,数到3或3的倍数的人出局,最后剩几号 */public static void count() {int[] man = new int[50];for (int i = 0; i < 50; i++) {man[i] = 1;}int j = 0;int num = 0;int count = 0;// 出局的人数while (true) {if (man[j % 50] == 1) {num++;if (num == 3) {man[j % 50] = 0;num = 0;count++;}}if (count == man.length) {System.out.println("数到了 " + j);System.out.println("最后一个是:" + (j % 50 + 1) + "号");break;}j++;}}/** * 随机生成a--z的20个字母,不能重复,然后排序输出 */public static void outCharactor() {int num = 0;Object ch;Set st = new HashSet();while (true) {num = 97 + (int) (Math.random() * 24);ch = (char) num;st.add(ch);if (st.size() == 20) {break;}}System.out.print("排序前:");Iterator it = st.iterator();while (it.hasNext()) {System.out.print(it.next() + " ");}System.out.print("排序后:");Object[] a = new Object[20];a = st.toArray();char t;int k = 0;for (int j = 0; j < 20; j++) {for (k = j; k < 20; k++) {if ((Character) a[j] > (Character) a[k]) {t = (Character) a[j];a[j] = (Character) a[k];a[k] = t;} else {continue;}}}for (int i = 0; i < 20; i++) {System.out.print(a[i] + " ");}}/** * 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半; 再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? */public static void niuTun() {double h = 100;double sum = 0;for (int i = 1; i <= 10; i++) {sum += h;h = h * 0.5;if (i != 10) {sum += h;}}System.out.println(sum + "," + h);}/** * 数学法 count 1 2 3 4 5 公式 经过多少米? 100 200 250 275 287.5 100*(3-1/(2^(N-2))) * 反弹多高? 50 25 12.5 6.25 3.125 100/(2^N) * @param args *  */public static void niuTunMaths(String[] args) {float H = 100f;int count = 10;System.out.println(H * (3 - 1.0 / Math.pow(2, count - 2)));System.out.println(H / Math.pow(2, count));}/** *  * @author jiashaoshan * @param args */public static void main(String[] args) {}}

热点排行