求教一个算法 算法java 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把这100个集合对象装到一个集合里!循环它,用random拿到随机数做为随机角标!取值!剩下一个数你自己想吧! 不知道,理解对不对import java.security.SecureRandom;import java.util.ArrayList;import java.util.Collections;import java.util.LinkedList;import java.util.List;import java.util.Random;public class RandomText { public static void main(String[] args) { String[] dictionaries = new String[] { "abcdefghijklmnopqrstuvwxyz", // 小写 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", // 大写 "0123456789", // 数字 "!@#$%^" // 符号 }; System.out.println(randomText(dictionaries, 5)); } /** * 返回随机字符串。 * * @param dictionaries * 不同的字典组合,每种都要至少出现一次 * @param length * 最后随机字符串长度 * @return 随机字符串 * @throws IllegalArgumentException * 如果字典数量或者长度限制不足以满足题意的话 */ public static String randomText(String[] dictionaries, int length) { if (length < dictionaries.length) { throw new IllegalArgumentException("length < dictionaries.length"); } // 被选中的字符列表,事后还要打乱顺序 List<Character> selected = new ArrayList<Character>(length); // 所有种类组合在一起的字典列表(扣除一开始已经被选中的,见下面代码解释) List<Character> rest = new LinkedList<Character>(); Random random = new SecureRandom(); for (String dictionary : dictionaries) { List<Character> list = new LinkedList<Character>(); for (char c : dictionary.toCharArray()) { list.add(c); } // 由于每种必然出现一次,先抽取一下 selected.add(list.remove(random.nextInt(list.size()))); // 剩余的这次未被选中的,组合在一起 rest.addAll(list); } // 再给未被选中的一些机会 while (selected.size() < length) { selected.add(rest.remove(random.nextInt(rest.size()))); } // 至此,所有被选中的,都已在selected里面,剩下只要打乱顺序即可 Collections.shuffle(selected, random); StringBuilder buff = new StringBuilder(length); for (char c : selected) { buff.append(c); } return buff.toString(); } 这位大哥一点了还不休息,注意身体呀。你这个代码和我一开始想的一样,我其实想问的就是打乱的那句Collections.shuffle如何在前面取这5个字符的时候一起实现,而不是在取完之后再实现打乱。 遍历元素最少的集合,每个集合先随机取一个位置的字符,再随机取一个集合的随机位置的一个字符,当字符数=101的时候就return /** * 返回随机字符串。 * * @param dictionaries * 不同的字典组合,每种都要至少出现一次 * @param length * 最后随机字符串长度 * @return 随机字符串 * @throws IllegalArgumentException * 如果字典数量或者长度限制不足以满足题意的话 */ public static String randomText2(String[] dictionaries, int length) { if (length < dictionaries.length) { throw new IllegalArgumentException("length < dictionaries.length"); } Random random = new SecureRandom(); // 每种类型出现的次数 int[] count = new int[dictionaries.length]; // 所有字典 List<List<Character>> allDictionaries = new ArrayList<List<Character>>( dictionaries.length); for (int i = 0; i < dictionaries.length; i++) { // 每种至少出现一次 count[i] = 1; List<Character> list = new LinkedList<Character>(); for (char c : dictionaries[i].toCharArray()) { list.add(c); } allDictionaries.add(list); } // 剩余次数随机分配 for (int i = length - dictionaries.length; i-- > 0;) { count[random.nextInt(dictionaries.length)]++; } char[] chars = new char[length]; // 每一位的字符分配 for (int i = 0; i < length; i++) { // 种类 int type = random.nextInt(dictionaries.length); // 一直找直到找到有还没剩余次数的为止 while (count[type] == 0) { type++; type %= dictionaries.length; } count[type]--; List<Character> dictionary = allDictionaries.get(type); chars[i] = dictionary.remove(random.nextInt(dictionary.size())); } return new String(chars); } BUG修正 /** * 返回随机字符串。 * * @param dictionaries * 不同的字典组合,每种都要至少出现一次 * @param length * 最后随机字符串长度 * @return 随机字符串 * @throws IllegalArgumentException * 如果字典数量或者长度限制不足以满足题意的话 */ public static String randomText2(String[] dictionaries, int length) { if (length < dictionaries.length) { throw new IllegalArgumentException("length < dictionaries.length"); } Random random = new SecureRandom(); // 每种类型出现的次数 int[] count = new int[dictionaries.length]; // 所有字典 List<List<Character>> allDictionaries = new ArrayList<List<Character>>( dictionaries.length); // 所有字符总数 int totalCount = 0; for (int i = 0; i < dictionaries.length; i++) { // 每种至少出现一次 count[i] = 1; List<Character> list = new LinkedList<Character>(); for (char c : dictionaries[i].toCharArray()) { list.add(c); } allDictionaries.add(list); totalCount += list.size(); } if (totalCount < length) { throw new IllegalArgumentException("totalCount < length"); } // 剩余次数随机分配 for (int i = length - dictionaries.length; i-- > 0;) { count[random.nextInt(dictionaries.length)]++; } char[] chars = new char[length]; // 每一位的字符分配 for (int i = 0; i < length; i++) { // 种类 int type = random.nextInt(dictionaries.length); // 一直找直到找到空位为止 while (count[type] == 0 || count[type] == dictionaries[i].length()) { type++; type %= dictionaries.length; } count[type]--; List<Character> dictionary = allDictionaries.get(type); chars[i] = dictionary.remove(random.nextInt(dictionary.size())); } return new String(chars); } 修改、删除功能并发处理 SOS Struts2传参 J2EE,不知道少引用了哪个jar包,有包的提示 最简单的提问?初入江湖,不懂江湖规矩,请多指教 JBOSS3 下发布 axis web service 的问题!急!!! 线程问题~~~~~高手们看看啊 spring报错 在linux9下建一个java开发环境,用什么工具好,请大家给点建议 这个错误是什么意思,我在试struts 用jb7开发Entity Bean时遇到的关系问题! 如果一个项目,一开始直接访问的是servlet页面,那么自动进入doPost()方法? java 程序员转 php 、 andriod 你会选哪一个
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;public class RandomText { public static void main(String[] args) {
String[] dictionaries = new String[] {
"abcdefghijklmnopqrstuvwxyz", // 小写
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", // 大写
"0123456789", // 数字
"!@#$%^" // 符号
};
System.out.println(randomText(dictionaries, 5));
} /**
* 返回随机字符串。
*
* @param dictionaries
* 不同的字典组合,每种都要至少出现一次
* @param length
* 最后随机字符串长度
* @return 随机字符串
* @throws IllegalArgumentException
* 如果字典数量或者长度限制不足以满足题意的话
*/
public static String randomText(String[] dictionaries, int length) {
if (length < dictionaries.length) {
throw new IllegalArgumentException("length < dictionaries.length");
} // 被选中的字符列表,事后还要打乱顺序
List<Character> selected = new ArrayList<Character>(length);
// 所有种类组合在一起的字典列表(扣除一开始已经被选中的,见下面代码解释)
List<Character> rest = new LinkedList<Character>(); Random random = new SecureRandom();
for (String dictionary : dictionaries) {
List<Character> list = new LinkedList<Character>();
for (char c : dictionary.toCharArray()) {
list.add(c);
}
// 由于每种必然出现一次,先抽取一下
selected.add(list.remove(random.nextInt(list.size())));
// 剩余的这次未被选中的,组合在一起
rest.addAll(list);
} // 再给未被选中的一些机会
while (selected.size() < length) {
selected.add(rest.remove(random.nextInt(rest.size())));
} // 至此,所有被选中的,都已在selected里面,剩下只要打乱顺序即可
Collections.shuffle(selected, random);
StringBuilder buff = new StringBuilder(length);
for (char c : selected) {
buff.append(c);
}
return buff.toString();
}
你这个代码和我一开始想的一样,我其实想问的就是打乱的那句Collections.shuffle如何在前面取这5个字符的时候一起实现,而不是在取完之后再实现打乱。
* 返回随机字符串。
*
* @param dictionaries
* 不同的字典组合,每种都要至少出现一次
* @param length
* 最后随机字符串长度
* @return 随机字符串
* @throws IllegalArgumentException
* 如果字典数量或者长度限制不足以满足题意的话
*/
public static String randomText2(String[] dictionaries, int length) {
if (length < dictionaries.length) {
throw new IllegalArgumentException("length < dictionaries.length");
} Random random = new SecureRandom(); // 每种类型出现的次数
int[] count = new int[dictionaries.length];
// 所有字典
List<List<Character>> allDictionaries = new ArrayList<List<Character>>(
dictionaries.length);
for (int i = 0; i < dictionaries.length; i++) {
// 每种至少出现一次
count[i] = 1;
List<Character> list = new LinkedList<Character>();
for (char c : dictionaries[i].toCharArray()) {
list.add(c);
}
allDictionaries.add(list);
} // 剩余次数随机分配
for (int i = length - dictionaries.length; i-- > 0;) {
count[random.nextInt(dictionaries.length)]++;
} char[] chars = new char[length]; // 每一位的字符分配
for (int i = 0; i < length; i++) {
// 种类
int type = random.nextInt(dictionaries.length);
// 一直找直到找到有还没剩余次数的为止
while (count[type] == 0) {
type++;
type %= dictionaries.length;
}
count[type]--;
List<Character> dictionary = allDictionaries.get(type);
chars[i] = dictionary.remove(random.nextInt(dictionary.size()));
} return new String(chars);
}
/**
* 返回随机字符串。
*
* @param dictionaries
* 不同的字典组合,每种都要至少出现一次
* @param length
* 最后随机字符串长度
* @return 随机字符串
* @throws IllegalArgumentException
* 如果字典数量或者长度限制不足以满足题意的话
*/
public static String randomText2(String[] dictionaries, int length) {
if (length < dictionaries.length) {
throw new IllegalArgumentException("length < dictionaries.length");
} Random random = new SecureRandom(); // 每种类型出现的次数
int[] count = new int[dictionaries.length];
// 所有字典
List<List<Character>> allDictionaries = new ArrayList<List<Character>>(
dictionaries.length);
// 所有字符总数
int totalCount = 0;
for (int i = 0; i < dictionaries.length; i++) {
// 每种至少出现一次
count[i] = 1;
List<Character> list = new LinkedList<Character>();
for (char c : dictionaries[i].toCharArray()) {
list.add(c);
}
allDictionaries.add(list);
totalCount += list.size();
}
if (totalCount < length) {
throw new IllegalArgumentException("totalCount < length");
} // 剩余次数随机分配
for (int i = length - dictionaries.length; i-- > 0;) {
count[random.nextInt(dictionaries.length)]++;
} char[] chars = new char[length]; // 每一位的字符分配
for (int i = 0; i < length; i++) {
// 种类
int type = random.nextInt(dictionaries.length);
// 一直找直到找到空位为止
while (count[type] == 0 || count[type] == dictionaries[i].length()) {
type++;
type %= dictionaries.length;
}
count[type]--;
List<Character> dictionary = allDictionaries.get(type);
chars[i] = dictionary.remove(random.nextInt(dictionary.size()));
} return new String(chars);
}