C では、大文字のない乱数と文字を含む char 配列があります。数字をそのままにして、大文字を含むすべての可能な組み合わせを生成する方法を理解する必要がありますが、どこから始めればよいかさえわかりません。(例: abc123、Abc123、aBc123、abC123、ABc123、AbC123、aBC123、ABC123)
2 に答える
2
実際、2^n の可能性があります。n は、char 配列にあるアルファベット文字の量を意味します。
あなたの問題を解決するには、recursionについて調べることをお勧めします。やりたいことを達成する最も簡単な方法だと思います。通常とは少し違う考え方をするだけです。
編集:ここにいくつかの実装があります
void enumerate(char *str, int n)
{
printf("%s\n", str); // Print one solution
while (++n < strlen(str)) // Loop while you don't reach the end
if (str[n] > 96 && str[n] < 123) // Check if str[n] is alphabetic
{
char *tmp = calloc(strlen(str) + 1, sizeof(char));
strcpy(tmp, str); // Create a copy of the initial string
tmp[n] -= 32; // Put tmp[n] = str[n] in uppercase
enumerate(tmp, n); // Call recursion with new string and current position
}
}
そして、あなたはそれをこのように呼びます
enumerate("abc123", -1);
結果は
abc123
Abc123
ABc123
ABC123
AbC123
aBc123
aBC123
abC123
于 2015-05-22T20:59:33.200 に答える
1
とりあえず数字は無視。
今x文字を持っているとしましょう。x=3 (仮定) の場合、2^3 - 1 は 7 であるため、0 から 7 までの数値を考慮します (状態として 0 を考慮する必要があるため、-1)。
次に、すべての数字を反復処理し、ビットが 1 のときは常に文字を大文字にする必要があります。
例:
- abc - 000 (0)
- abC - 001 (1)
- abc - 010 (2)
- aBC-011 (3)
- Abc - 100 (4)
理論的な説明を理解できなかった人のために、ここにそのコードを示します。
void enumerate (String input) {
int count = 0;
//count the number of non-digit characters
for(int i=0; i<input.length(); i++)
if(!Character.isDigit(input.charAt(i))) {
count++;
}
count =(int) Math.pow(2,count);
//printing all the combinations.
int i=0, digit=0;
while(count--> 0) {
String output = "";
for(int j=input.length()-1; j>=0; j--) {
char c = input.charAt(j);
if(Character.isDigit(c))
output = (char)c + output;
else {
output = (char) (((i&1) == 1)? c-32 : c) + output;
i = i>>1;
}
}
System.out.println(output);
i = ++digit;
}
}
于 2015-05-22T19:51:37.893 に答える