我为一个程序编写了这段代码,以创建字母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);
}
}
}谢谢!
发布于 2022-06-03 04:43:14
您可以维护一个计数器变量,它在内部循环每次执行时都会递增:
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发布于 2022-06-03 05:37:47
这里有一个替代的实现。
代码点的IntStream
在处理单个字符时,我建议您养成使用码点整数的习惯,而不是使用遗留类型char。作为一个16位值,char在物理上无法表示大多数字符.
我们可以从a到z (97到122)从IntStream生成代码点的范围。Character.toString( codePoint )方法从代码点整数生成一个单字符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组合= aaa、aab、aac、aad、aae、aaf、…zzw,zzx,zzz,zzz
使用Stream#flatMap的一元线
我们甚至可以使用来自Holger的评论的令人印象深刻的代码,将该代码缩减为一行代码。
关键部分是对Stream#flatMap的调用,用于从一个值生成多个值。引用Javadoc:
flatMap()操作的效果是将一对多的转换应用于流的元素,然后将生成的元素扁平化为新的流。
顺便说一下,concat是一个static方法。考虑到流上的其他流利-style方法,这似乎有点奇怪。如果好奇,请看这个问题。
因此,我们首先将代码流转换为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、BAb、BAa、、en38 20#、aaa、aab、aba、abb、#en27、aaA、aaB、#en31、32、abA、abB、BAA、BAB、en33、34#en38、Bbb、# BbB #35#、# bBb ##、##en38#、#en38、#40#combinations.toString##40#BBA,BBB
发布于 2022-06-03 05:30:14
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=17576https://stackoverflow.com/questions/72484787
复制相似问题