日期:2014-05-20 浏览次数:20835 次
import java.util.*; public class Test { public static void main(String[] args) throws Throwable { q1(); q2(); } public static void q1() throws Throwable { List<Integer> l1 = new ArrayList<Integer>(Arrays.asList(new Integer[]{0,2,4,6,8})); List<Integer> l2 = new ArrayList<Integer>(Arrays.asList(new Integer[]{0,1,3,5,7,9})); List<Integer> list = new ArrayList<Integer>(); for (int i=0; i<4; i++) { list.add(l1.remove((int)(Math.random()*l1.size()))); list.add(l2.remove((int)(Math.random()*l2.size()))); } int idx = 0; do { idx = (int)(Math.random()*list.size()); } while (list.get(idx) == 0); int sum = list.remove(idx); for (int i=1; i<8; i++) { sum = sum*10 + list.remove((int)(Math.random()*list.size())); } System.out.printf("随机8位数是: %d\n", sum); char[] c = String.valueOf(sum).toCharArray(); int start = 0; if (sum%2 == 0) { System.out.println("奇数位是:"); } else { System.out.println("偶数位是:"); start = 1; } for (; start < c.length; start+=2) { System.out.printf("%c ", c[start]); } System.out.println(); } public static void q2() throws Throwable { List<String> list = new ArrayList<String>(Arrays.asList("123456".split("(?<=\\d)"))); System.out.println("原始6位数(123456)"); for (int i=0; ! list.get(0).equals("0"); i++) { System.out.println("----------我是分隔线----------"); String s = list.get(0); int idx = Integer.valueOf(s); System.out.printf("第一个1个数是: %s, 左移%s位\n", s, s); for (int j=0; j<idx%6; j++) { list.add(list.remove(0)); } System.out.println(list.toString().replaceAll("\\D+", "")); System.out.printf("第%s位+%s\n", s, s); int v = (Integer.valueOf(list.get(idx%6-1)) + idx)%10; list.set(idx%6-1, String.valueOf(v)); System.out.println(list.toString().replaceAll("\\D+", "")); if (i > 6) {break;} //测试6次 } } }