我想随机生成12位十六进制。约束是不超过2个数字作为整体重复,并且不连续重复。如何写出高效的代码?
xx11xxxxxxxx is not valid because the same digit (e.g., 1) appears consecutively.
12121xxxxxxx is not valid as well because the digit 1 appears thrice.
我想从B,C,D的列表中随机挑选一个字母,并确保它们不重复。我试过了,但它重复了字母
public class Test {
static Random r = new Random();
static char pickRandom(char... letters) {
return letters[r.nextInt(letters.length)];
}
public static void main(String args[]) {
for (int i = 0; i < 10; i++) {
问题是要找到长度为N的可重复二进制字符串的数目。如果二进制字符串的任何子字符串可以重复其自身以形成原始二进制字符串,则二进制字符串是可重复的。
Example
"1010" is a repeatable string as it can be obtained from "10" by repeating 2 number of times
"1001" is not a repeatable string as it cannot be obtained from any sub string of "1001" by r
public class PossibleCombination {
public static void comb(String s) {
int N = s.length();
char[] a = new char[N];
for (int i = 0; i < N; i++)
a[i] = s.charAt(i);
comb(a, N);
}
private static void comb(char[] a, int n) {
if (n == 1) {
我有一个“随机”字符组成的字符串。我根据字符在字符串中的位置为每个字符分配了一个数值,然后设置一个循环,以便在任意随机选择的位置输出字符。到目前为止,我的代码如下: public class Random9_4 {
public static void main(String[] args) {
final String chords = "ADE";
final int N = chords.length();
java.util.Random rand = new java.util.Random();
for(int i = 0;
strKeyword将根据循环重复。如何将结果保存为新字符串。例如,如果工作"hello“重复了两次,我现在如何创建"hellohello”作为一个全新的字符串。
for (int l = 0; l < newKeywordLength; l++) {
System.out.print(strKeyword);
}