Java aaa到zzz的字母组合程序
Java Letter combination program for aaa to zzz
我已经为一个程序编写了这段代码来创建字母 aaa 到 zzz 的组合。共有 17,576 种组合。例如首先是aaa然后是aab等等。
我希望我的代码在输出过程中计算不同的组合,例如 1.aaa 2.aab 3.aac 等等。
这是我的代码:
for(char c1 = 'a'; c1 <= 'z'; c1++){
for(char c2 = 'a'; c2 <= 'z'; c2++){
for(char c3 = 'a'; c3 <= 'z'; c3++){
System.out.println("" + c1 + c2 + c3);
}
}
}
谢谢!
好吧,你可以维护一个计数器变量,它在每次执行内部循环时递增:
int counter = 0;
List<String> combinations = new ArrayList<>();
for (char c1 = 'a'; c1 <= 'z'; c1++) {
for (char c2 = 'a'; c2 <= 'z'; c2++) {
for (char c3 = 'a'; c3 <= 'z'; c3++) {
String combo = "" + c1 + c2 + c3;
System.out.println(combo);
combinations.add(combo);
++counter;
}
}
}
System.out.println("total number of combinations is " + counter); // 17576
int n = 26; //no. of alphabet which is fix number
int p = 3; //no. of alphabet we require in one combination like aaa,abc,bbb....
System.out.print(Math.pow(n,p));
有关公式的解释,请阅读以下描述,这将有所帮助
**Easy mathematic formula**
***(fix number)^(no. of element in each combination)***
It is just like no. of total combination of when we toss a coin , when we throw two dice in probability.
here each combination contains 3 alphabets so , let's take each alphabet from different pool(p1 , p2 , p3).
p1 p2 p3
__ ___ __
a a a
b b b
c c c
d d d
. . .
. . .
. . .
so here first take 1st alphabet 'a' from p1(pool1) and then take each alphabet from p2(pool2) so our result be like **aa,ab,ac,ad.....** . here we can see that when we take only 'a' then it creates 26 different combination. likewise each character of pool1(p1) has its own 26 different combination (like 'b' has 'ba','bb','bc'... also c has'ca','cb','cc' and so on).
so total combination we can make from first two pool is 26*26.
now let see how our new pool (new combinated pool) look likes...
new pool** **p3**
_____________ ________
aa a
ab b
ac c
ad d
. .
. .
. z
. ====
zz 26
=====
26*26
now let's do the same process between **new pool** and **p3** , so our final result is (new pool characters)*(p3's characters)=[26**26]*26=17576
这是另一种实现方式。
IntStream
个代码点
我建议在处理单个字符时养成使用 code points 整数而不是旧类型 char
的习惯。作为 16 位值,char
在物理上无法表示大多数字符。
我们可以从 IntStream
生成从 a
到 z
(97 到 122)的代码点范围。 Character.toString( codePoint )
方法从我们的代码点整数生成一个 single-character String
对象。
List < String > characters =
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) ) // ( 97 inclusive, 122 inclusive )
.mapToObj( Character :: toString )
.toList();
characters.toString() = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
将三个组合字符中的每个 String
收集到 List
。
List < String > combinations = new ArrayList <>( characters.size() ^ 3 );
然后使用 for-each 语法将该源列表循环三次,嵌套,一次用于所需输出的每个位置。
for ( String firstCharacter : characters )
{
for ( String secondCharacter : characters )
{
for ( String thirdCharacter : characters )
{
combinations.add( firstCharacter + secondCharacter + thirdCharacter );
}
}
}
调用 List#size
即可获得所需的计数。虽然从数学上我们知道计数应该是 ( 26 ^ 3 ) = 17,576.
System.out.println( combinations.size() + " combinations = " + combinations );
当运行.
17576 combinations = [aaa, aab, aac, aad, aae, aaf, … zzw, zzx, zzy, zzz]
One-liner 通过使用 Stream#flatMap
我们甚至可以使用 .
中令人印象深刻的代码将代码减少到 single-line
关键部分是对Stream#flatMap
的调用,用于从一个值生成多个值。引用 Javadoc:
The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.
顺便说一下,concat
是一个 static
方法。考虑到另一个 fluent-style methods on streams. If curious, see this Question.
,这似乎有点奇怪
因此,我们首先将代码点流转换为 Strings
流,每个代码点都包含一个字符。对于我们的第一个字符中的每一个,我们为第二个字符创建了一堆转换为 String
对象的代码点。我们为第二个字符中的每一个再次调用 flatMap
,每个字符都会生成另一个代码点流,转换为我们第三个位置的 String
对象。从那里,第一个、第二个和第三个字符被组合成一个结果字符串,我们将其收集到我们的最终结果中,一个不可修改的 List< String >
.
我们得到与上面相同的 17,576 种组合。
List < String > combinations =
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) )
.mapToObj( Character :: toString )
.flatMap(
first ->
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) )
.mapToObj( Character :: toString )
.flatMap( second ->
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) )
.mapToObj( third -> first + second + Character.toString( third ) )
)
)
.toList();
多组输入
上面的代码假设我们有一个单一范围的字符来混合和匹配。请注意,我们可以组合多个范围。只需在传递一对流时调用 Stream.concat
。
在这个例子中,我们混合并匹配小写 ab
和大写 AB
。对于三个位置总共使用四个字符,我们期望 4 ^ 3 = 64 种组合。
List < String > combinations =
IntStream
.concat(
IntStream.rangeClosed( "a".codePointAt( 0 ) , "b".codePointAt( 0 ) ) ,
IntStream.rangeClosed( "A".codePointAt( 0 ) , "B".codePointAt( 0 ) )
)
.mapToObj( Character :: toString )
.flatMap(
first ->
IntStream
.concat(
IntStream.rangeClosed( "a".codePointAt( 0 ) , "b".codePointAt( 0 ) ) ,
IntStream.rangeClosed( "A".codePointAt( 0 ) , "B".codePointAt( 0 ) )
)
.mapToObj( Character :: toString )
.flatMap( second ->
IntStream
.concat(
IntStream.rangeClosed( "a".codePointAt( 0 ) , "b".codePointAt( 0 ) ) ,
IntStream.rangeClosed( "A".codePointAt( 0 ) , "B".codePointAt( 0 ) )
)
.mapToObj( third -> first + second + Character.toString( third ) )
)
)
.toList();
64 combinations.toString() = [aaa, aab, aaA, aaB, aba, abb, abA, abB, aAa, aAb, aAA, aAB, aBa, aBb, aBA, aBB, baa, bab, baA, baB, bba, bbb, bbA, bbB, bAa, bAb, bAA, bAB, bBa, bBb, bBA, bBB, Aaa, Aab, AaA, AaB, Aba, Abb, AbA, AbB, AAa, AAb, AAA, AAB, ABa, ABb, ABA, ABB, Baa, Bab, BaA, BaB, Bba, Bbb, BbA, BbB, BAa, BAb, BAA, BAB, BBa, BBb, BBA, BBB]
我已经为一个程序编写了这段代码来创建字母 aaa 到 zzz 的组合。共有 17,576 种组合。例如首先是aaa然后是aab等等。
我希望我的代码在输出过程中计算不同的组合,例如 1.aaa 2.aab 3.aac 等等。
这是我的代码:
for(char c1 = 'a'; c1 <= 'z'; c1++){
for(char c2 = 'a'; c2 <= 'z'; c2++){
for(char c3 = 'a'; c3 <= 'z'; c3++){
System.out.println("" + c1 + c2 + c3);
}
}
}
谢谢!
好吧,你可以维护一个计数器变量,它在每次执行内部循环时递增:
int counter = 0;
List<String> combinations = new ArrayList<>();
for (char c1 = 'a'; c1 <= 'z'; c1++) {
for (char c2 = 'a'; c2 <= 'z'; c2++) {
for (char c3 = 'a'; c3 <= 'z'; c3++) {
String combo = "" + c1 + c2 + c3;
System.out.println(combo);
combinations.add(combo);
++counter;
}
}
}
System.out.println("total number of combinations is " + counter); // 17576
int n = 26; //no. of alphabet which is fix number
int p = 3; //no. of alphabet we require in one combination like aaa,abc,bbb....
System.out.print(Math.pow(n,p));
有关公式的解释,请阅读以下描述,这将有所帮助
**Easy mathematic formula**
***(fix number)^(no. of element in each combination)***
It is just like no. of total combination of when we toss a coin , when we throw two dice in probability.
here each combination contains 3 alphabets so , let's take each alphabet from different pool(p1 , p2 , p3).
p1 p2 p3
__ ___ __
a a a
b b b
c c c
d d d
. . .
. . .
. . .
so here first take 1st alphabet 'a' from p1(pool1) and then take each alphabet from p2(pool2) so our result be like **aa,ab,ac,ad.....** . here we can see that when we take only 'a' then it creates 26 different combination. likewise each character of pool1(p1) has its own 26 different combination (like 'b' has 'ba','bb','bc'... also c has'ca','cb','cc' and so on).
so total combination we can make from first two pool is 26*26.
now let see how our new pool (new combinated pool) look likes...
new pool** **p3**
_____________ ________
aa a
ab b
ac c
ad d
. .
. .
. z
. ====
zz 26
=====
26*26
now let's do the same process between **new pool** and **p3** , so our final result is (new pool characters)*(p3's characters)=[26**26]*26=17576
这是另一种实现方式。
IntStream
个代码点
我建议在处理单个字符时养成使用 code points 整数而不是旧类型 char
的习惯。作为 16 位值,char
在物理上无法表示大多数字符。
我们可以从 IntStream
生成从 a
到 z
(97 到 122)的代码点范围。 Character.toString( codePoint )
方法从我们的代码点整数生成一个 single-character String
对象。
List < String > characters =
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) ) // ( 97 inclusive, 122 inclusive )
.mapToObj( Character :: toString )
.toList();
characters.toString() = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
将三个组合字符中的每个 String
收集到 List
。
List < String > combinations = new ArrayList <>( characters.size() ^ 3 );
然后使用 for-each 语法将该源列表循环三次,嵌套,一次用于所需输出的每个位置。
for ( String firstCharacter : characters )
{
for ( String secondCharacter : characters )
{
for ( String thirdCharacter : characters )
{
combinations.add( firstCharacter + secondCharacter + thirdCharacter );
}
}
}
调用 List#size
即可获得所需的计数。虽然从数学上我们知道计数应该是 ( 26 ^ 3 ) = 17,576.
System.out.println( combinations.size() + " combinations = " + combinations );
当运行.
17576 combinations = [aaa, aab, aac, aad, aae, aaf, … zzw, zzx, zzy, zzz]
One-liner 通过使用 Stream#flatMap
我们甚至可以使用
关键部分是对Stream#flatMap
的调用,用于从一个值生成多个值。引用 Javadoc:
The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.
顺便说一下,concat
是一个 static
方法。考虑到另一个 fluent-style methods on streams. If curious, see this Question.
因此,我们首先将代码点流转换为 Strings
流,每个代码点都包含一个字符。对于我们的第一个字符中的每一个,我们为第二个字符创建了一堆转换为 String
对象的代码点。我们为第二个字符中的每一个再次调用 flatMap
,每个字符都会生成另一个代码点流,转换为我们第三个位置的 String
对象。从那里,第一个、第二个和第三个字符被组合成一个结果字符串,我们将其收集到我们的最终结果中,一个不可修改的 List< String >
.
我们得到与上面相同的 17,576 种组合。
List < String > combinations =
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) )
.mapToObj( Character :: toString )
.flatMap(
first ->
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) )
.mapToObj( Character :: toString )
.flatMap( second ->
IntStream
.rangeClosed( "a".codePointAt( 0 ) , "z".codePointAt( 0 ) )
.mapToObj( third -> first + second + Character.toString( third ) )
)
)
.toList();
多组输入
上面的代码假设我们有一个单一范围的字符来混合和匹配。请注意,我们可以组合多个范围。只需在传递一对流时调用 Stream.concat
。
在这个例子中,我们混合并匹配小写 ab
和大写 AB
。对于三个位置总共使用四个字符,我们期望 4 ^ 3 = 64 种组合。
List < String > combinations =
IntStream
.concat(
IntStream.rangeClosed( "a".codePointAt( 0 ) , "b".codePointAt( 0 ) ) ,
IntStream.rangeClosed( "A".codePointAt( 0 ) , "B".codePointAt( 0 ) )
)
.mapToObj( Character :: toString )
.flatMap(
first ->
IntStream
.concat(
IntStream.rangeClosed( "a".codePointAt( 0 ) , "b".codePointAt( 0 ) ) ,
IntStream.rangeClosed( "A".codePointAt( 0 ) , "B".codePointAt( 0 ) )
)
.mapToObj( Character :: toString )
.flatMap( second ->
IntStream
.concat(
IntStream.rangeClosed( "a".codePointAt( 0 ) , "b".codePointAt( 0 ) ) ,
IntStream.rangeClosed( "A".codePointAt( 0 ) , "B".codePointAt( 0 ) )
)
.mapToObj( third -> first + second + Character.toString( third ) )
)
)
.toList();
64 combinations.toString() = [aaa, aab, aaA, aaB, aba, abb, abA, abB, aAa, aAb, aAA, aAB, aBa, aBb, aBA, aBB, baa, bab, baA, baB, bba, bbb, bbA, bbB, bAa, bAb, bAA, bAB, bBa, bBb, bBA, bBB, Aaa, Aab, AaA, AaB, Aba, Abb, AbA, AbB, AAa, AAb, AAA, AAB, ABa, ABb, ABA, ABB, Baa, Bab, BaA, BaB, Bba, Bbb, BbA, BbB, BAa, BAb, BAA, BAB, BBa, BBb, BBA, BBB]