日期:2014-05-20 浏览次数:20671 次
static String[][] sp(final String[] cs, int tlen) { if (cs == null || tlen < 0) { return null; } String[] c = new String[cs.length]; System.arraycopy(cs, 0, c, 0, c.length); //打乱 Random ran = new Random(); for (int i = c.length; i > 1; i--) { //产生随机数 int r = ran.nextInt(i); //交换 String buf = c[r]; c[r] = c[i - 1]; c[i - 1] = buf; } //分组后的组数 int len = cs.length / tlen; if (cs.length > len * tlen) { len++; } String[][] re = new String[len][]; //分组 for (int i = 0, t = 0; i < cs.length; i += tlen, t++) { int clen; if (i + tlen > cs.length) { clen = cs.length - i; } else { clen = tlen; } String[] cstr = new String[clen]; System.arraycopy(c, i, cstr, 0, clen); re[t] = cstr; } return re; }
------解决方案--------------------
public List<String[]> group(String [] str,int m){ List<String> _student = new ArrayList<String>(); for(int i=0;i<str.length;i++){ _student.add(str[i]); } int n = _student.size(); List <String[]> student = new ArrayList<String[]>(); for(int i=0;i<m;i++){ int size = n%m==0?n/m:(i==(m-1)?((n/m+1)*m-n):n/(m-1)); String [] s = new String[size]; int _size = _student.size(); for(int j=0;j<size;j++){ int index = (int)(Math.random()*_size); s[j] = _student.get(index); _student.remove(index); _size--; } student.add(s); } return student; }