求教一个算法题 从26个字母中取出三个字母并排列,打印出所有情况 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 排列就是有顺序的了 对吧 abc 和 cba 不能算一个 有时间帮你写一个 我理解的应该是不能重复的3个字母按顺序排列吧。下午比较无聊, 按照上午的思路写了个。package suanfa;import java.util.Arrays;public class Sort { public static void main(String[] args){ new Sort().ergodic(3); } /** * 将方法重载下, 只需要传入需要的长度 * @param length */ private void ergodic(int length) { ergodic('a', length, new char[0]); } /** * @param first 第一个字符 * @param left 剩余需要的串长 * @param str 已经拼接的字符 */ private void ergodic(char first, int left, char[] str) { char next = (char)(first+1); //下一个 //已经是最后一个了,将其输出 if(left == 1){ //直接输出 char[] newStr = Arrays.copyOf(str, str.length+1); newStr[str.length] = first; System.out.println(newStr); if(first == 'z')//最后一个 结束了 return; //还没到最后 继续递归 this.ergodic(next, left, str); } else{ if(first == 'z')//最后一个 结束了 return; //用第一个字符 char[] newStr = Arrays.copyOf(str, str.length+1); newStr[str.length] = first; this.ergodic(next, left - 1, newStr); //递归 //不用第一个字符 this.ergodic(next, left , str); } }} 嘿嘿 我下午也写了一个import java.util.ArrayList;/** * * * permutation and combination * * @author Administrator */public class Pac { Pac(String str) { this.str = str; } String str;//源字符串。 String state;//c or a 状态 /* * 组合数 */ public void permutation(int c) { state="a"; alc = new ArrayList<String>(); ala = new ArrayList<String>(); c(str, c); for (String s : alc) { a(s, c); } for (String s : ala) { System.out.println(s); } System.out.println("共计" + ala.size() + "个 组合"); } ArrayList<String> ala = new ArrayList<String>();//返回用的al void a(String str, int c) { filla(str, c, ""); } void filla(String str, int c, String pre) { if (c <= 0) { System.out.println("选择的个数太少必须大于0当前是" + c); return; } if (c == 1) { ala.add(pre + str.charAt(0)); return; } for (int i = 0; i < str.length(); i++) { filla(str.substring(0, i) + str.substring(i + 1, str.length()), c - 1, pre + str.charAt(i));//正常 } } /* * 排列数 */ public void combination(int c) { state="c"; alc = new ArrayList<String>(); c(str, c); for (String s : alc) { System.out.println(s); } System.out.println("共计" + alc.size() + "个 排列"); } /* * 内部组合 */ private ArrayList<String> c(String str, int c) { if (c > str.length()) { System.out.println("数组内部出错选择大于数组长度"); return null; } fillc(str, c, ""); return null; } ArrayList<String> alc = new ArrayList<String>();//返回用的al void fillc(String str, int c, String pre) { if (c <= 0) { System.out.println("选择的个数太少必须大于0当前是" + c); return; } if (c == 1) { for (char s : str.toCharArray()) { alc.add(pre + s); } return; } fillc(str.substring(1, str.length()), c - 1, pre + str.charAt(0));//正常 //重填 if (c <= str.length() - 1) { fillc(str.substring(1, str.length()), c, pre); } } public static void main(String[] args) { Pac p = new Pac("123456789"); p.combination(3); p.permutation(3); }} Java里面有排列组合的算法吧。自己写的话,看起来代码都不是很Generic。 这个是个组合问题,你可以看下我的博客http://xieyan30.iteye.com/admin/blogs/1814117看看能不能帮助到你 rs<K,V,N>中如何获取k、v、n的值? 求写几个SQL语句,在线等 TOMCAT配置数据源有段话不明白---求教 在SWT中使用TabFolder,并添加了几个TabItem,怎么在程序中使某个TabItem被选中 JAVA中如何移动文件`? ?不知道怎么绘事,我在DESIGN中画的东西,没有代码生成? 一个小程序,一个小问题(数值如何压入栈中) 这样的select语句写得出吗? 非静态初始化模块和构造器的区别? Applet下的类都必须在同一个包中吗? public static void main(String\u005B\u005D args)这是什么 map遍历查找缺少值的问题
下午比较无聊, 按照上午的思路写了个。package suanfa;import java.util.Arrays;public class Sort {
public static void main(String[] args){
new Sort().ergodic(3);
}
/**
* 将方法重载下, 只需要传入需要的长度
* @param length
*/
private void ergodic(int length) {
ergodic('a', length, new char[0]);
}
/**
* @param first 第一个字符
* @param left 剩余需要的串长
* @param str 已经拼接的字符
*/
private void ergodic(char first, int left, char[] str) {
char next = (char)(first+1); //下一个
//已经是最后一个了,将其输出
if(left == 1){
//直接输出
char[] newStr = Arrays.copyOf(str, str.length+1);
newStr[str.length] = first;
System.out.println(newStr);
if(first == 'z')//最后一个 结束了
return;
//还没到最后 继续递归
this.ergodic(next, left, str);
}
else{
if(first == 'z')//最后一个 结束了
return;
//用第一个字符
char[] newStr = Arrays.copyOf(str, str.length+1);
newStr[str.length] = first;
this.ergodic(next, left - 1, newStr); //递归
//不用第一个字符
this.ergodic(next, left , str);
}
}
}
*
*
* permutation and combination
*
* @author Administrator
*/
public class Pac { Pac(String str) {
this.str = str;
}
String str;//源字符串。
String state;//c or a 状态
/*
* 组合数
*/
public void permutation(int c) {
state="a";
alc = new ArrayList<String>();
ala = new ArrayList<String>();
c(str, c);
for (String s : alc) {
a(s, c);
}
for (String s : ala) {
System.out.println(s);
}
System.out.println("共计" + ala.size() + "个 组合");
}
ArrayList<String> ala = new ArrayList<String>();//返回用的al void a(String str, int c) {
filla(str, c, "");
} void filla(String str, int c, String pre) {
if (c <= 0) {
System.out.println("选择的个数太少必须大于0当前是" + c);
return;
}
if (c == 1) {
ala.add(pre + str.charAt(0));
return;
}
for (int i = 0; i < str.length(); i++) {
filla(str.substring(0, i) + str.substring(i + 1, str.length()), c - 1, pre + str.charAt(i));//正常
} } /*
* 排列数
*/
public void combination(int c) {
state="c";
alc = new ArrayList<String>();
c(str, c);
for (String s : alc) {
System.out.println(s);
}
System.out.println("共计" + alc.size() + "个 排列");
}
/*
* 内部组合
*/ private ArrayList<String> c(String str, int c) {
if (c > str.length()) {
System.out.println("数组内部出错选择大于数组长度");
return null;
}
fillc(str, c, "");
return null;
}
ArrayList<String> alc = new ArrayList<String>();//返回用的al void fillc(String str, int c, String pre) {
if (c <= 0) {
System.out.println("选择的个数太少必须大于0当前是" + c);
return;
}
if (c == 1) {
for (char s : str.toCharArray()) {
alc.add(pre + s);
}
return;
}
fillc(str.substring(1, str.length()), c - 1, pre + str.charAt(0));//正常
//重填
if (c <= str.length() - 1) {
fillc(str.substring(1, str.length()), c, pre);
}
} public static void main(String[] args) {
Pac p = new Pac("123456789");
p.combination(3);
p.permutation(3); }
}
http://xieyan30.iteye.com/admin/blogs/1814117
看看能不能帮助到你