1.随机产生20个不能重复的字符并排序
package com.test.kaoshi;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class RadomDemo {
/**
* 随机产生20个字符串并且字符串不能重复 且进行排序
* @param random
* @param len
* @return
*/
public Set getChar(){
Set numberSet01 = new HashSet();
Random rdm = new Random();
char ch;
while(numberSet01.size()<20){
int rdGet = Math.abs(rdm.nextInt())%26+97;//产生97到122的随机数a-z值
ch=(char)rdGet;
numberSet01.add(ch);
//Set中是不能放进重复的值的,当它有20个时,就满足你的条件了
}
return numberSet01;
}
public static void main(String[] args) {
RadomDemo rd = new RadomDemo();
Set numberSet01=rd.getChar();
Set numberSet = new TreeSet(); //TreeSet值不能重复,并且按照指定排序方法进行compareTo排序 //详见: http://blog.csdn.net/you_off3/article/details/7465919
numberSet.addAll(numberSet01);
for(Iterator it=numberSet01.iterator();it.hasNext();){
System.out.print(it.next());
}
System.out.println();
for(Iterator it=numberSet.iterator();it.hasNext();){
System.out.print(it.next());
}
}
}
2.50个人围坐一圈,当数到三或者三的倍数出圈,问剩下的人是谁,原来的位置是多少
package com.test.kaoshi;
import java.util.Iterator;
import java.util.LinkedList;
public class YouXi {
public static int removeNM(int n, int m) {
LinkedList ll = new LinkedList();
for (int i = 0; i < n; i++)
ll.add(new Integer(i + 1));
int removed = -1;
while (ll.size() > 1) {
removed = (removed + m) % ll.size();
ll.remove(removed--);
}
return ((Integer) ll.get(0)).intValue();
}
public static void main(String[] args) {
System.out.println(removeNM(50, 3));
}
}
3.使用递归实现回文判断(如"abcdedcba"就是一个回文
boolean loopWord(String str, int i) {
if (str.charAt(i) == str.charAt(str.length() - 1 - i)) {
if (i == (str.length() + 1) / 2)
return true;
return loopWord(str, i + 1);
} else {
return false;
}
}参考地址:http://blog.csdn.net/somnl/article/details/7404693