有26个字母a~z,找出所有字母组合,a、b、c、ab、abc、a~z 都是一个组合(顺序无关)

1.*这个问题有点像小学课本上握手问题,每个人都握到最后那个人,这样也就好理解点了
2.*我们可以通过循环加上字符串拼接每次加一个字母,然后存储到数组中,好比:a,a+b,a+b+c,a+b+c+d等等,到了b就清空继续这个操作
*3.*这样一看我们可以采用双重for循环一个负责首字母一个负责后续的拼接

public class ABC {
  public static void main(String[] args) {
    ArrayList list = new ArrayList();
    for (char i = 'a'; i <= 'z'; i++) {
      //清空
      String st = "";
      for (char j = i; j <= 'z'; j++) {
        //拼接
        st += j;
        //存储
        list.add(st);
      }
    }
    //遍历出来所有的组合
    for (String string : list) {
      System.out.println(string);
    }
  }
}

 

D:\develop\JDK1.8\jdk1.8.0_281\bin\java.exe "-javaagent:D:\develop\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=6168:D:\develop\IntelliJ IDEA 2020.3.2\bin" -Dfile.encoding=UTF-8 -classpath D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\charsets.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\deploy.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\access-bridge-64.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\cldrdata.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\dnsns.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\jaccess.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\jfxrt.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\localedata.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\nashorn.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\sunec.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\sunjce_provider.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\sunmscapi.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\sunpkcs11.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\ext\zipfs.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\javaws.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\jce.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\jfr.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\jfxswt.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\jsse.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\management-agent.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\plugin.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\resources.jar;D:\develop\JDK1.8\jdk1.8.0_281\jre\lib\rt.jar;D:\JavaCode\JavaSE_Code\out\production\Idea_test;D:\JavaCode\JavaSE_Code\Idea_test\libs\mysql-connector-java-5.1.37-bin.jar com.cy.mianshi.ABC
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
abcdefghijk
abcdefghijkl
abcdefghijklm
abcdefghijklmn
abcdefghijklmno
abcdefghijklmnop
abcdefghijklmnopq
abcdefghijklmnopqr
abcdefghijklmnopqrs
abcdefghijklmnopqrst
abcdefghijklmnopqrstu
abcdefghijklmnopqrstuv
abcdefghijklmnopqrstuvw
abcdefghijklmnopqrstuvwx
abcdefghijklmnopqrstuvwxy
abcdefghijklmnopqrstuvwxyz
b
bc
bcd
bcde
bcdef
bcdefg
bcdefgh
bcdefghi
bcdefghij
bcdefghijk
bcdefghijkl
bcdefghijklm
bcdefghijklmn
bcdefghijklmno
bcdefghijklmnop
bcdefghijklmnopq
bcdefghijklmnopqr
bcdefghijklmnopqrs
bcdefghijklmnopqrst
bcdefghijklmnopqrstu
bcdefghijklmnopqrstuv
bcdefghijklmnopqrstuvw
bcdefghijklmnopqrstuvwx
bcdefghijklmnopqrstuvwxy
bcdefghijklmnopqrstuvwxyz
c
cd
cde
cdef
cdefg
cdefgh
cdefghi
cdefghij
cdefghijk
cdefghijkl
cdefghijklm
cdefghijklmn
cdefghijklmno
cdefghijklmnop
cdefghijklmnopq
cdefghijklmnopqr
cdefghijklmnopqrs
cdefghijklmnopqrst
cdefghijklmnopqrstu
cdefghijklmnopqrstuv
cdefghijklmnopqrstuvw
cdefghijklmnopqrstuvwx
cdefghijklmnopqrstuvwxy
cdefghijklmnopqrstuvwxyz
d
de
def
defg
defgh
defghi
defghij
defghijk
defghijkl
defghijklm
defghijklmn
defghijklmno
defghijklmnop
defghijklmnopq
defghijklmnopqr
defghijklmnopqrs
defghijklmnopqrst
defghijklmnopqrstu
defghijklmnopqrstuv
defghijklmnopqrstuvw
defghijklmnopqrstuvwx
defghijklmnopqrstuvwxy
defghijklmnopqrstuvwxyz
e
ef
efg
efgh
efghi
efghij
efghijk
efghijkl
efghijklm
efghijklmn
efghijklmno
efghijklmnop
efghijklmnopq
efghijklmnopqr
efghijklmnopqrs
efghijklmnopqrst
efghijklmnopqrstu
efghijklmnopqrstuv
efghijklmnopqrstuvw
efghijklmnopqrstuvwx
efghijklmnopqrstuvwxy
efghijklmnopqrstuvwxyz
f
fg
fgh
fghi
fghij
fghijk
fghijkl
fghijklm
fghijklmn
fghijklmno
fghijklmnop
fghijklmnopq
fghijklmnopqr
fghijklmnopqrs
fghijklmnopqrst
fghijklmnopqrstu
fghijklmnopqrstuv
fghijklmnopqrstuvw
fghijklmnopqrstuvwx
fghijklmnopqrstuvwxy
fghijklmnopqrstuvwxyz
g
gh
ghi
ghij
ghijk
ghijkl
ghijklm
ghijklmn
ghijklmno
ghijklmnop
ghijklmnopq
ghijklmnopqr
ghijklmnopqrs
ghijklmnopqrst
ghijklmnopqrstu
ghijklmnopqrstuv
ghijklmnopqrstuvw
ghijklmnopqrstuvwx
ghijklmnopqrstuvwxy
ghijklmnopqrstuvwxyz
h
hi
hij
hijk
hijkl
hijklm
hijklmn
hijklmno
hijklmnop
hijklmnopq
hijklmnopqr
hijklmnopqrs
hijklmnopqrst
hijklmnopqrstu
hijklmnopqrstuv
hijklmnopqrstuvw
hijklmnopqrstuvwx
hijklmnopqrstuvwxy
hijklmnopqrstuvwxyz
i
ij
ijk
ijkl
ijklm
ijklmn
ijklmno
ijklmnop
ijklmnopq
ijklmnopqr
ijklmnopqrs
ijklmnopqrst
ijklmnopqrstu
ijklmnopqrstuv
ijklmnopqrstuvw
ijklmnopqrstuvwx
ijklmnopqrstuvwxy
ijklmnopqrstuvwxyz
j
jk
jkl
jklm
jklmn
jklmno
jklmnop
jklmnopq
jklmnopqr
jklmnopqrs
jklmnopqrst
jklmnopqrstu
jklmnopqrstuv
jklmnopqrstuvw
jklmnopqrstuvwx
jklmnopqrstuvwxy
jklmnopqrstuvwxyz
k
kl
klm
klmn
klmno
klmnop
klmnopq
klmnopqr
klmnopqrs
klmnopqrst
klmnopqrstu
klmnopqrstuv
klmnopqrstuvw
klmnopqrstuvwx
klmnopqrstuvwxy
klmnopqrstuvwxyz
l
lm
lmn
lmno
lmnop
lmnopq
lmnopqr
lmnopqrs
lmnopqrst
lmnopqrstu
lmnopqrstuv
lmnopqrstuvw
lmnopqrstuvwx
lmnopqrstuvwxy
lmnopqrstuvwxyz
m
mn
mno
mnop
mnopq
mnopqr
mnopqrs
mnopqrst
mnopqrstu
mnopqrstuv
mnopqrstuvw
mnopqrstuvwx
mnopqrstuvwxy
mnopqrstuvwxyz
n
no
nop
nopq
nopqr
nopqrs
nopqrst
nopqrstu
nopqrstuv
nopqrstuvw
nopqrstuvwx
nopqrstuvwxy
nopqrstuvwxyz
o
op
opq
opqr
opqrs
opqrst
opqrstu
opqrstuv
opqrstuvw
opqrstuvwx
opqrstuvwxy
opqrstuvwxyz
p
pq
pqr
pqrs
pqrst
pqrstu
pqrstuv
pqrstuvw
pqrstuvwx
pqrstuvwxy
pqrstuvwxyz
q
qr
qrs
qrst
qrstu
qrstuv
qrstuvw
qrstuvwx
qrstuvwxy
qrstuvwxyz
r
rs
rst
rstu
rstuv
rstuvw
rstuvwx
rstuvwxy
rstuvwxyz
s
st
stu
stuv
stuvw
stuvwx
stuvwxy
stuvwxyz
t
tu
tuv
tuvw
tuvwx
tuvwxy
tuvwxyz
u
uv
uvw
uvwx
uvwxy
uvwxyz
v
vw
vwx
vwxy
vwxyz
w
wx
wxy
wxyz
x
xy
xyz
y
yz
z

Process finished with exit code 0

你可能感兴趣的:(jaVa练习题,java)