//這個程序是以前用高分求來的,現(xiàn)在稍作修改,呵呵
站在用戶的角度思考問題,與客戶深入溝通,找到靈武網(wǎng)站設(shè)計與靈武網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計制作、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋靈武地區(qū)。
public class Zuhe {
public static void main(String[] args) {
String s = "122345";//這里是要用到的所有數(shù)組成的一個字符串,其它字符同樣適用
char[] c = s.toCharArray();
new Zuhe().zuhe(c,c.length,0);
System.out.println("可能的組合數(shù):"+kk);
}
static int kk=0;
private void zuhe(char[] array, int n, int k) {
if (n == k) {
if(array[2]!='4'){//第三個位置不能出現(xiàn)4
String str = new String(array);
if(str.indexOf("53")0str.indexOf("35")0){//3,5不能連續(xù)出現(xiàn)
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;
}
}
========結(jié)果=========
122345
122543
123245
123254
123425
123452
125432
125423
125243
125234
122345
122543
123245
123254
123425
123452
125432
125423
125243
125234
132245
132254
132425
132452
132542
132524
132245
132254
132425
132452
132542
132524
142325
142523
143225
143252
143225
143252
142325
142523
145232
145223
145223
145232
152342
152324
152432
152423
152243
152234
152342
152324
152432
152423
152243
152234
212345
212543
213245
213254
213425
213452
215432
215423
215243
215234
221345
221543
223145
223154
223415
223451
225431
225413
225143
225134
232145
232154
232415
232451
232541
232514
231245
231254
231425
231452
231542
231524
242315
242513
243215
243251
243125
243152
241325
241523
245132
245123
245213
245231
252341
252314
252431
252413
252143
252134
251342
251324
251432
251423
251243
251234
221345
221543
223145
223154
223415
223451
225431
225413
225143
225134
212345
212543
213245
213254
213425
213452
215432
215423
215243
215234
231245
231254
231425
231452
231542
231524
232145
232154
232415
232451
232541
232514
241325
241523
243125
243152
243215
243251
242315
242513
245231
245213
245123
245132
251342
251324
251432
251423
251243
251234
252341
252314
252431
252413
252143
252134
322145
322154
322415
322451
322541
322514
321245
321254
321425
321452
321542
321524
325142
325124
325412
325421
325241
325214
322145
322154
322415
322451
322541
322514
321245
321254
321425
321452
321542
321524
325142
325124
325412
325421
325241
325214
312245
312254
312425
312452
312542
312524
312245
312254
312425
312452
312542
312524
315242
315224
315422
315422
315242
315224
342125
342152
342215
342251
342521
342512
341225
341252
341225
341252
341522
341522
342125
342152
342215
342251
342521
342512
345122
345122
345212
345221
345221
345212
422315
422513
423215
423251
423125
423152
421325
421523
425132
425123
425213
425231
422315
422513
423215
423251
423125
423152
421325
421523
425132
425123
425213
425231
432215
432251
432125
432152
432512
432521
432215
432251
432125
432152
432512
432521
431225
431252
431225
431252
431522
431522
412325
412523
413225
413252
413225
413252
412325
412523
415232
415223
415223
415232
452312
452321
452132
452123
452213
452231
451322
451322
451232
451223
451223
451232
452312
452321
452132
452123
452213
452231
522341
522314
522431
522413
522143
522134
523241
523214
523421
523412
523142
523124
521342
521324
521432
521423
521243
521234
522341
522314
522431
522413
522143
522134
523241
523214
523421
523412
523142
523124
521342
521324
521432
521423
521243
521234
542321
542312
542231
542213
542123
542132
543221
543212
543221
543212
543122
543122
542321
542312
542231
542213
542123
542132
541322
541322
541232
541223
541223
541232
512342
512324
512432
512423
512243
512234
513242
513224
513422
513422
513242
513224
512342
512324
512432
512423
512243
512234
可能的組合數(shù):396
import java.util.ArrayList;
import java.util.List;
public class AllSort {
/**
* 從數(shù)組中選出所有組合
* @param source
* @param arrayList
* @param num
*/
public static void select(char[] source, ListString arrayList, int num) {
int lg = source.length;
char[] temp = new char[num];
System.arraycopy(source, 0, temp, 0, num);
// 首次填充數(shù)組
arrayList.add(new String(temp));
// 逐個(位置)替換方式實(shí)現(xiàn)不重復(fù)全選
for (int i = num; i lg; i++) {
for (int j = 0; j num; j++) {
char tempChar = temp[j];
temp[j] = source[i];
arrayList.add(new String(temp));
temp[j] = tempChar;
}
}
}
/**
* 全排序
* @param source
* @param start
* @param end
*/
public static void perm(char[] source, int start, int end) {
if (start == end) {// 遞歸出口,最簡單的情況,只取出1個
for (int i = 0; i = end; i++) {
System.out.print(source[i]);
}
System.out.println();
} else {// 多個全排列
for (int i = start; i = end; i++) {
char temp = source;// 逐位交換
source = source[i];
source[i] = temp;
perm(source, start + 1, end);// 遞歸全排列
// 還原,為下個替換做準(zhǔn)備
temp = source;
source = source[i];
source[i] = temp;
}
}
}
public static void main(String[] args) {
//char[] source = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Aa-Zz...'};
char[] source = { '0', '1', '2', '3', '4','5','6','7','8','9' };
ArrayListString list = new ArrayListString();
for (int i = 1; i = source.length; i++) {
list.clear();
//選出
select(source, list, i);
System.out.println("===========取出"+i+"個==============");
//排列
for (String str : list) {
char[] temp = str.toCharArray();
perm(temp, 0, temp.length - 1);
}
}
}
}
之前在遠(yuǎn)標(biāo)java時接觸過!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 創(chuàng)建Scanner對象,用于獲取用戶輸入
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入任意字符序列:");
// 獲取用戶輸入的字符序列
String str = scanner.nextLine();
// 循環(huán)遍歷字符序列中的每個字符
for (int i = 0; i str.length(); i++) {
// 獲取字符序列中的第i個字符
char c1 = str.charAt(i);
// 循環(huán)遍歷字符序列中的每個字符
for (int j = 0; j str.length(); j++) {
// 獲取字符序列中的第j個字符
char c2 = str.charAt(j);
// 如果第i個字符不等于第j個字符,則輸出它們的排列
if (i != j) {
System.out.println(c1 + "" + c2);
}
}
}
}
}
完成了一種實(shí)現(xiàn),發(fā)給你參考下。
不過感覺應(yīng)該還有更好的辦法,有時間我會繼續(xù)研究下.
import?java.util.ArrayList;
import?java.util.Arrays;
import?java.util.HashSet;
import?java.util.List;
import?java.util.Set;
public?class?TestQiuhe?{
//集合a{1,2,3,5,7,10},輸出不多于4個元素(不重復(fù))的加和為22的組合。
public?static?void?main(String[]?args)?{
int[]?nums={1,2,3,5,7,10};
int?l?=?nums.length;
Listint[]?results=new?ArrayListint[]();
for(int?i1=0;i1l;i1++){
for(int?i2=0;i2l;i2++){
if(nums[i1]==22){
results.add(new?int[]{nums[i1]});
}
if(i2!=i1){
if(nums[i1]+nums[i2]==22){
results.add(new?int[]{nums[i1],nums[i2]});
}
for(int?i3=0;i3l;i3++){
if(i3!=i1??i3!=i2){
if(nums[i1]+nums[i2]+nums[i3]==22){
results.add(new?int[]{nums[i1],nums[i2],nums[i3]});
}
for(int?i4=0;i4l;i4++){
if(i4!=i1??i4!=?i2??i4!=i3){
if(nums[i1]+nums[i2]+nums[i3]+nums[i4]==22){
results.add(new?int[]{nums[i1],nums[i2],nums[i3],nums[i4]});
}
}
}
}
}
}
}
}
//去重
SetString?reSet=new?HashSet();
for(int[]?r:results){
Arrays.sort(r);
reSet.add(Arrays.toString(r));
}
System.out.println("一共得到結(jié)果集:"+reSet.size());
System.out.println(reSet);
}
}
運(yùn)行結(jié)果:
一共得到結(jié)果集:2
[[5, 7, 10], [2, 3, 7, 10]]
char[] a={'1','2','3','4'};
String b= "";
String c= "";
for (int i = 0; i a.length-1; i++) {
b="["+a[i]+","+a[i+1]+"]";
c+=b;
}
System.out.println(c);
import?java.util.Arrays;
import?java.util.LinkedList;
public?class?RecursionNArray
{
private?static?LinkedListchar[]?recursionSub?(?LinkedListchar[]?list,?int?count,?char[][]?array,?int?ind,
int?start,?int...?indexs?)
{
start++;
if?(start??count?-?1)
{
return?null;
}
if?(start?==?0)
{
indexs?=?new?int[array.length];
}
for?(?indexs?=?0;?indexs??array.length;?indexs++?)
{
recursionSub?(list,?count,?array,?0,?start,?indexs);
if?(start?==?count?-?1)
{
char[]?temp?=?new?char[count];
for?(?int?i?=?count?-?1;?i?=?0;?i--?)
{
temp[start?-?i]?=?array[start?-?i][indexs[start?-?i]];
}
list.add?(temp);
}
}
return?list;
}
public?static?void?main?(?String[]?args?)
{
char[]?a?=?{?'中','國','有','個','人'?};
char[]?b?=?{?'中','國','有','個','人'?};
char[]?c?=?{?'中','國','有','個','人'?};
char[]?d?=?{?'中','國','有','個','人'?};
char[][]?temp?=?{?a,?b,?c,?d?};
LinkedListchar[]?list?=?new?LinkedListchar[]?();
recursionSub?(list,?temp.length,?temp,?0,?-1);
for?(?int?i?=?0;?i??list.size?();?i++?)
{
System.out.println?(Arrays.toString?(list.get?(i)).replaceAll?("[\\[\\]\\,\\s]",?""));
}
}
}
網(wǎng)站欄目:java代碼實(shí)現(xiàn)組合排列 編程實(shí)現(xiàn)排列組合
新聞來源:http://muchs.cn/article26/hjdgcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、關(guān)鍵詞優(yōu)化、商城網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)