java实现:键盘输入从0~9中任意5个数,排列组合出所有不重复的组合,打印出来
必有追加大分!!!比如1.2.3.4.5共有120个组合12345,12354,12435,12453,12534,12543;13245,13254,13425,134...
必有追加大分!!!
比如1.2.3.4.5 共有120个组合
12345,12354,12435,12453,12534,12543;
13245,13254,13425,13452,13524,13542;
14235,14253,14325,14352,14523,14532;
15234,15243,15324,15342,15423,15432;
21345,21354,21435,21453,21534,21543;
23145,23154,23415,23451,23514,23541;
24135,24153,24315,24351,24513,24531;
25134,25143,25314,25341,25413,25431;
31245,31254,31425,31452,31524,31542;
32145,32154,32415,32451,32514,32541;
34125,34152,34215,34251,34512,34521;
35124,35142,35214,35241,35412,35421;
41235,41253,41325,41352,41523,41532;
42135,42153,42315,42351,42513,42531;
43125,43152,43215,43251,43512,43521;
45123,45132,45213,45231,45312,45321;
51234,51243,51324,51342,51423,51432;
52134,52143,52314,52341,52413,52431;
53124,53142,53214,53241,53412,53421;
54123,54132,54213,54231,54312,54321;
我需要的是让不懂的人,通过键盘输入,得到所有的组合的内容
其他高级语言实现也可以,软件实现也可以,总之要的是结果 展开
比如1.2.3.4.5 共有120个组合
12345,12354,12435,12453,12534,12543;
13245,13254,13425,13452,13524,13542;
14235,14253,14325,14352,14523,14532;
15234,15243,15324,15342,15423,15432;
21345,21354,21435,21453,21534,21543;
23145,23154,23415,23451,23514,23541;
24135,24153,24315,24351,24513,24531;
25134,25143,25314,25341,25413,25431;
31245,31254,31425,31452,31524,31542;
32145,32154,32415,32451,32514,32541;
34125,34152,34215,34251,34512,34521;
35124,35142,35214,35241,35412,35421;
41235,41253,41325,41352,41523,41532;
42135,42153,42315,42351,42513,42531;
43125,43152,43215,43251,43512,43521;
45123,45132,45213,45231,45312,45321;
51234,51243,51324,51342,51423,51432;
52134,52143,52314,52341,52413,52431;
53124,53142,53214,53241,53412,53421;
54123,54132,54213,54231,54312,54321;
我需要的是让不懂的人,通过键盘输入,得到所有的组合的内容
其他高级语言实现也可以,软件实现也可以,总之要的是结果 展开
5个回答
展开全部
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
final int length = 5;
int[] nums = new int[length];
Scanner scanner = new Scanner(System.in);
for(int i = 0; i < nums.length; i++){
System.out.print("Please input " + (i+1) + " number(0~9): ");
int value = scanner.nextInt();
if(value < 0 || value > 9){
System.out.println("Invalid input! number should between 0 and 9!");
i--;
continue;
}
nums[i] = value;
}
int count = 0;
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if(i != j){
for(int k = 0; k < nums.length; k++){
if(k != i && k != j){
for(int m = 0; m < nums.length; m++){
if(m != i && m != k && m!=j){
for(int n = 0; n < nums.length; n++){
if(n != i && n != j && n != k && n != m){
System.out.println(nums[i] + "" + nums[j] + "" + nums[k] + "" + nums[m] + "" + nums[n]);
count++;
}
}
}
}
}
}
}
}
}
System.out.println("Total " + count + " group numbers!");
}
}
-----------------------
Please input 1 number(0~9): 2
Please input 2 number(0~9): 1
Please input 3 number(0~9): 52
Invalid input! number should between 0 and 9!
Please input 3 number(0~9): 3
Please input 4 number(0~9): 5
Please input 5 number(0~9): 4
21354
21345
21534
....
45321
45312
Total 120 group numbers!
public class Test {
public static void main(String[] args) {
final int length = 5;
int[] nums = new int[length];
Scanner scanner = new Scanner(System.in);
for(int i = 0; i < nums.length; i++){
System.out.print("Please input " + (i+1) + " number(0~9): ");
int value = scanner.nextInt();
if(value < 0 || value > 9){
System.out.println("Invalid input! number should between 0 and 9!");
i--;
continue;
}
nums[i] = value;
}
int count = 0;
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if(i != j){
for(int k = 0; k < nums.length; k++){
if(k != i && k != j){
for(int m = 0; m < nums.length; m++){
if(m != i && m != k && m!=j){
for(int n = 0; n < nums.length; n++){
if(n != i && n != j && n != k && n != m){
System.out.println(nums[i] + "" + nums[j] + "" + nums[k] + "" + nums[m] + "" + nums[n]);
count++;
}
}
}
}
}
}
}
}
}
System.out.println("Total " + count + " group numbers!");
}
}
-----------------------
Please input 1 number(0~9): 2
Please input 2 number(0~9): 1
Please input 3 number(0~9): 52
Invalid input! number should between 0 and 9!
Please input 3 number(0~9): 3
Please input 4 number(0~9): 5
Please input 5 number(0~9): 4
21354
21345
21534
....
45321
45312
Total 120 group numbers!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Zuhe {
public static void main(String[] args) {
String a = args[0];//这里是你运行时的参数
char[] c = s.toCharArray();
new Zuhe().zuhe(c,c.length,0);
System.out.println("可能的组合数:"+kk);
}
static int kk=0;
private void zuhe(char[] array, int n, int k) {
if (n == k) {
String str = new String(array);
if(str.indexOf("53")<0&&str.indexOf("35")<0){//3,5不能连续出现
System.out.println(str);
++kk;
}
} else {
for (int i = k; i < n; i++) {
swap(array, k, i);
zuhe(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
执行:java Zuhe 12345
这样就OK了
public static void main(String[] args) {
String a = args[0];//这里是你运行时的参数
char[] c = s.toCharArray();
new Zuhe().zuhe(c,c.length,0);
System.out.println("可能的组合数:"+kk);
}
static int kk=0;
private void zuhe(char[] array, int n, int k) {
if (n == k) {
String str = new String(array);
if(str.indexOf("53")<0&&str.indexOf("35")<0){//3,5不能连续出现
System.out.println(str);
++kk;
}
} else {
for (int i = k; i < n; i++) {
swap(array, k, i);
zuhe(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
执行:java Zuhe 12345
这样就OK了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
告诉你公式 9*8*7*6/4/3/2*5*4*3*2
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
总计10万注而不是120注...
追问
不好意思,没说清楚,每个数字只能出现一次。
追答
你干什么...演数是学术.价值连城...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要图形界面吗?
追问
多谢你的回答,还打包成可执行文件,超级感谢!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询