字符串排列
发布网友
发布时间:2022-04-23 23:08
我来回答
共4个回答
热心网友
时间:2023-10-13 03:39
/**
* @author Stephen Wu
*
*/
public class Du {
public static void main(String[] args) {
displayPermutation("abc");
System.out.println("------------------------------------\n\n");
displayPermutation("abcd");
}
public static void displayPermutation(String s) {
displayPermutation(" ", s);
}
public static void displayPermutation(String s1, String s2) {
if (s2.trim().equals("")) {
System.out.println(s1.trim());
} else {
StringBuilder sb = null;
for (int i = 0; i < s2.length(); i++) {
sb = new StringBuilder(s2);
String first = s1 + sb.substring(i, i + 1);
displayPermutation(first, sb.deleteCharAt(i).toString());
}
}
}
}
---------------------Testing
abc
acb
bac
bca
cab
cba
------------------------------------
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba
热心网友
时间:2023-10-13 03:39
能实现全排列,还能实现其它排列,比如4选3,7选5等等。
import java.util.ArrayList;
import java.util.List;
public class TT {
public static void main(String[] args) {
FullSort full = new FullSort();
full.displayPermutation("abc");
for(String s: full.result){
System.out.println(s);
}
}
static class FullSort {
List<String> result = new ArrayList<String>();
public void displayPermutation(String datas) {
displayPermutation(datas,"",datas.length());
}
public void displayPermutation(String datas, String target) {
displayPermutation(datas,"",datas.length());
}
/**
* 递归算法:将数据分为两部分,递归将数据从左侧移右侧实现全排列
*
* @param datas
* @param target
*/
public void displayPermutation(String datas, String target, int picked) {
if (target.length() == picked) {
result.add(target);
return;
}
for (int i = 0; i < datas.length(); i++) {
String newTarget = target + datas.charAt(i);
String newDatas = datas.substring(0, i)
+ datas.substring(i + 1);
displayPermutation(newDatas, newTarget, picked);
}
}
}
}
热心网友
时间:2023-10-13 03:40
//////////////////////////////
// 动态链表字符串排序
//
//功能介绍:输入10个字符串,按ASCII排序输出。
// 再输入第11个字符串,11个字符串排序输出。
// 第12、13...依此类推
/////////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define N 21 //字符串长度
struct node
{
char str[N];
struct node *next;
};
void print( struct node *p ); //函数定义
void sort( struct node *p );
void swap( struct node *s1, struct node *s2 );
void main( )
{
struct node *head=NULL, *p, *p1;
char a[N];
int num = 0; //字符串个数标记
printf( "输入10个字符串,回车换行:\n" );
while( num<10 )
{
scanf( "%s",&a );
p = ( struct node *) malloc( sizeof( struct node ) );
strcpy( p->str,a );
if( head == NULL )
{
head = p;
p1 = p;
}
else
{
p1->next = p;
p1 = p;
}
num = num + 1;
}
p->next = NULL;
sort( head ); //字符串比较排序
print( head ); //输出打印字符串
while( 1 ) //死循环
{
printf( "输入字符串: " );
scanf( "%s",&a );
p = ( struct node *) malloc( sizeof( struct node ) );
strcpy( p->str,a );
p1->next = p;
p1 = p;
p->next = NULL;
sort( head );
print( head );
}
}
void sort( struct node *p ) //字符串比较排序
{
struct node *p1, *p2;
p1 = p;
while( p1 != NULL )
{
p2 = p1->next;
while( p2 != NULL )
{
if( strcmp( (p1->str),(p2->str) ) > 0 )
{
swap( p1,p2 ); //字符串交换
}
p2 = p2->next;
}
p1 = p1->next;
}
}
void swap( struct node *s1, struct node *s2 ) //字符串交换
{
struct node temp1,temp2;
temp1 = *s1;
temp1.next = s2->next;
temp2 = *s2;
temp2.next = s1->next;
*s1 = temp2;
*s2 = temp1;
}
void print( struct node *p ) //字符串输出
{
printf( "\n" );
while( p !=NULL )
{
puts( p->str );
p = p->next;
}
printf( "\n" );
}
热心网友
时间:2023-10-13 03:40
//////////////////////////////
// 动态链表字符串排序
//
//功能介绍:输入10个字符串,按ASCII排序输出。
// 再输入第11个字符串,11个字符串排序输出。
// 第12、13...依此类推
/////////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define N 21 //字符串长度
struct node
{
char str[N];
struct node *next;
};
void print( struct node *p ); //函数定义
void sort( struct node *p );
void swap( struct node *s1, struct node *s2 );
void main( )
{
struct node *head=NULL, *p, *p1;
char a[N];
int num = 0; //字符串个数标记
printf( "输入10个字符串,回车换行:\n" );
while( num<10 )
{
scanf( "%s",&a );
p = ( struct node *) malloc( sizeof( struct node ) );
strcpy( p->str,a );
if( head == NULL )
{
head = p;
p1 = p;
}
else
{
p1->next = p;
p1 = p;
}
num = num + 1;
}
p->next = NULL;
sort( head ); //字符串比较排序
print( head ); //输出打印字符串
while( 1 ) //死循环
{
printf( "输入字符串: " );
scanf( "%s",&a );
p = ( struct node *) malloc( sizeof( struct node ) );
strcpy( p->str,a );
p1->next = p;
p1 = p;
p->next = NULL;
sort( head );
print( head );
}
}
void sort( struct node *p ) //字符串比较排序
{
struct node *p1, *p2;
p1 = p;
while( p1 != NULL )
{
p2 = p1->next;
while( p2 != NULL )
{
if( strcmp( (p1->str),(p2->str) ) > 0 )
{
swap( p1,p2 ); //字符串交换
}
p2 = p2->next;
}
p1 = p1->next;
}
}
void swap( struct node *s1, struct node *s2 ) //字符串交换
{
struct node temp1,temp2;
temp1 = *s1;
temp1.next = s2->next;
temp2 = *s2;
temp2.next = s1->next;
*s1 = temp2;
*s2 = temp1;
}
void print( struct node *p ) //字符串输出
{
printf( "\n" );
while( p !=NULL )
{
puts( p->str );
p = p->next;
}
printf( "\n" );
}
另外,站长团上有产品团购,便宜有保证