原题如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
我看了回贴都没有很好解决,主要是没有排除重复。
解决思路:强化题目,用1、2、2、3、4、5这六个数字排列“递增”序列。其他要求不变。
算法思路:显然是递归,初始序列122345,先从末两位(45)变化(45,54),然后末三位(345) ... 直到最后六位.怎样解决重复问题?很简单,由于是递增序列,每生成新序列可与前一生成序列比较,如<放弃当前序列。当然有更好效率,如预先预测。代码如下:
class test
{
// 当前固定部分
private String CurFixPart;
private String PreGenNum;
public static void main(String[] args)
{
test t=new test();
t.GenControll("122345");
}
// 调整字符串s位置pos字符到最前
private String shift(String s, int pos)
{
String newStr;
if (s.length()>pos+1)
newStr=s.substring(pos, pos+1)
+s.substring(0, pos)
+s.substring(pos+1);
else
newStr=s.substring(pos)
+s.substring(0, pos);
return newStr;
}
protected int Validate(String newNum)
{
String newGenNum=CurFixPart+newNum;
if (Integer.valueOf(newGenNum)<=Integer.valueOf(PreGenNum))
return 0;
if (newGenNum.substring(2,3).equals("4") ||
(newGenNum.indexOf("35")!=-1) || (newGenNum.indexOf("53")!=-1))
return 0;
PreGenNum=newGenNum;
System.out.println(newGenNum);
return 0;
}
public void GenControll(String Base)
{
PreGenNum="0";
CurFixPart="";
GenNext(Base, 0);
}
void GenNext(String varPart, int curPos)
{
if (varPart.length()==2)
{
Validate(varPart);
Validate(shift(varPart, 1));
return;
}
// Next Layer
String newGen=shift(varPart, curPos);
String SavedFixPart=CurFixPart;
CurFixPart=CurFixPart+newGen.substring(0,1);
GenNext(newGen.substring(1), 0);
CurFixPart=SavedFixPart;
// 同层递增
if (curPos==varPart.length()-1)
return;
GenNext(varPart, curPos+1);
}
}
序列122345测试通过。
有什么意见请大家多多提点。
我看了回贴都没有很好解决,主要是没有排除重复。
解决思路:强化题目,用1、2、2、3、4、5这六个数字排列“递增”序列。其他要求不变。
算法思路:显然是递归,初始序列122345,先从末两位(45)变化(45,54),然后末三位(345) ... 直到最后六位.怎样解决重复问题?很简单,由于是递增序列,每生成新序列可与前一生成序列比较,如<放弃当前序列。当然有更好效率,如预先预测。代码如下:
class test
{
// 当前固定部分
private String CurFixPart;
private String PreGenNum;
public static void main(String[] args)
{
test t=new test();
t.GenControll("122345");
}
// 调整字符串s位置pos字符到最前
private String shift(String s, int pos)
{
String newStr;
if (s.length()>pos+1)
newStr=s.substring(pos, pos+1)
+s.substring(0, pos)
+s.substring(pos+1);
else
newStr=s.substring(pos)
+s.substring(0, pos);
return newStr;
}
protected int Validate(String newNum)
{
String newGenNum=CurFixPart+newNum;
if (Integer.valueOf(newGenNum)<=Integer.valueOf(PreGenNum))
return 0;
if (newGenNum.substring(2,3).equals("4") ||
(newGenNum.indexOf("35")!=-1) || (newGenNum.indexOf("53")!=-1))
return 0;
PreGenNum=newGenNum;
System.out.println(newGenNum);
return 0;
}
public void GenControll(String Base)
{
PreGenNum="0";
CurFixPart="";
GenNext(Base, 0);
}
void GenNext(String varPart, int curPos)
{
if (varPart.length()==2)
{
Validate(varPart);
Validate(shift(varPart, 1));
return;
}
// Next Layer
String newGen=shift(varPart, curPos);
String SavedFixPart=CurFixPart;
CurFixPart=CurFixPart+newGen.substring(0,1);
GenNext(newGen.substring(1), 0);
CurFixPart=SavedFixPart;
// 同层递增
if (curPos==varPart.length()-1)
return;
GenNext(varPart, curPos+1);
}
}
序列122345测试通过。
有什么意见请大家多多提点。
解决方案 »
- 一个关于哈希表的问题。。。求解。。。
- 关于类关系问题,恳求答案
- 我想提高一下自己的算法能力,网上有没有比较好的网站啊?
- CSDN没有人会!java到底能不能处理windows注册表 除了HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs以外的
- 为何无法调用Set方法设置的值?
- 字符串问题(一回答马上结贴)
- 我安装了j2re-1_4_1_02-windows-i586-i.exe,怎么会找不到javac?
- 初学JAVA想写一个时钟。。不会。。
- 高分求助,
- 关于sun Java下载问题
- 一个面试题:String s = new String("xyz");创建了几个String Object?
- JAVA编程:打印昨天的当前时刻
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑:
1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。采用二维数组定义图结构,最后的代码是:import java.util.Iterator;
import java.util.TreeSet;public class TestQuestion {private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet set = new TreeSet();public static void main(String[] args) {
new TestQuestion().start();
}private void start() {// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
代码中请注意这几行:
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;只要这样定义图,根本不用在代码中写IF ELSE语句。
实际上基于图的算法好处在于,只要你能定义好满足题目要求的图结构,遍历的结果就是你要的结果,不用任何对遍历结果做任何处理。包括本题中的:4不能在第三位置,3,5不能相连,唯一
性要求,其实都可以在体现在构造的图形结构里,然后直接遍历图取得自己要的结果。而不用再次处理结果集。只是说这里实际上对其它要求要体现在图结构里有困难(理论上是可以的),但起码3,5不能相接是很好构造的,就是上面的代码段来解释的。关于图形数据结构建议先看看数据结构的书,主要是将如何利用二维数组描述图结构,再看看图的深度遍历实现原理。最后再应用到这个问题上来,自然就不难明白了。
图虽然概念清晰,但用在这题上并不是很好
1。运行效率,没办法避免重复的排序。
2。内存,用TreeSet存结果集,如果序列很长,内存会耗光。
其实有两方面可以优化一下:
1。Validate 可以学习Struts,利用抽象Validate集去掉If/Else语句。在此不多说。
2。在同层递增前增加“重复预测“减少不必要的重复排序,提高效率。
改进代码如下,其中“//********”处为修改地方
class test
{
// 当前固定部分
private String CurFixPart;
private String PreGenNum;
public static void main(String[] args)
{
test t=new test();
t.GenControll("1111111111");
}
// 调整字符串s位置pos字符到最前
private String shift(String s, int pos)
{
String newStr;
if (s.length()>pos+1)
newStr=s.substring(pos, pos+1)
+s.substring(0, pos)
+s.substring(pos+1);
else
newStr=s.substring(pos)
+s.substring(0, pos);
return newStr;
}
protected int Validate(String newNum)
{
String newGenNum=CurFixPart+newNum;
if (Integer.valueOf(newGenNum)<=Integer.valueOf(PreGenNum))
return 0;
if (newGenNum.substring(2,3).equals("4") ||
(newGenNum.indexOf("35")!=-1) || (newGenNum.indexOf("53")!=-1))
return 0;
PreGenNum=newGenNum;
System.out.println(newGenNum);
return 0;
}
public void GenControll(String Base)
{
PreGenNum="0";
CurFixPart="";
GenNext(Base, 0);
}
void GenNext(String varPart, int curPos)
{
if (varPart.length()==2)
{
Validate(varPart);
Validate(shift(varPart, 1));
return;
}
// Next Layer
String newGen=shift(varPart, curPos);
String SavedFixPart=CurFixPart;
CurFixPart=CurFixPart+newGen.substring(0,1);
GenNext(newGen.substring(1), 0);
CurFixPart=SavedFixPart;
//*************
// 同层递增
while (true)
{
if (curPos==varPart.length()-1)
break;
// 预测是否重复
curPos++;
if (Integer.valueOf(CurFixPart+shift(varPart, curPos))<=Integer.valueOf(PreGenNum))
continue;
GenNext(varPart, curPos);
break;
}
//*************
}
}
import java.util.*;
public class test {
public static void main(String[] arg) {
Scanner r=new Scanner(System.in);
String s=r.nextLine();
Pailie(s,"");
}
static void Pailie(String s, String p) {
if(s.length()<1) {
String t=p+s;
if(t.charAt(2)!='4' && t.contains("35")==false)
System.out.println(t);
}
else {
for(int i=0; i<s.length(); i++) {
Pailie(s.substring(1),p+s.substring(0,1));
s=s.substring(1)+s.substring(0,1);
}
}
}
}
abc(输入行)
abc
acb
bca
bac
cab
cba
以下是楼主题的运行结果:
12345(输入行)
12345
12534
12543
13245
13254
14523
14532
14253
14325
15234
15243
15342
15324
23145
23154
24513
24531
24153
24315
25134
25143
25341
25314
21345
21534
21543
34512
34521
34125
34152
34251
34215
31245
31254
31524
31542
32514
32541
32145
32154
45123
45132
45231
45213
45312
45321
41253
41325
41523
41532
42315
42513
42531
42153
43125
43152
43251
43215
51234
51243
51342
51324
52341
52314
52134
52143
53124
53142
53241
53214
54123
54132
54231
54213
54312
54321
http://bbs.kaoyan.com/thread-1697273-1-2.html
比求1-6强多了
下面的地址也有提到上面的有关内容
http://community.csdn.net/Expert/topic/5201/5201506.xml?temp=.5158502
JianZhiZG(健之) 你可以试一下1223,你的算法没有排除重复
import java.util.*;
public class test {
static LinkedList list=new LinkedList();
public static void main(String[] arg) {
Scanner r=new Scanner(System.in);
String s=r.nextLine();
Pailie(s,"");
for(int i=0; i<list.size(); i++)
for(int j=i+1; j<list.size(); j++)
if(((String)list.get(i)).compareTo((String)list.get(j))==0)
list.remove(j);
for(int i=0; i<list.size(); i++)
System.out.println(list.get(i));
}
static void Pailie(String s,String p) {
if(s.length()<1) {
String n=new String(p);
list.add(n);
}
else {
for(int i=0; i<s.length(); i++) {
Pailie(s.substring(1),p+s.substring(0,1));//递归调用
s=s.substring(1)+s.substring(0,1);//循环移位
}
}
}
}
这样,你的结果就得到了:
1223(输入行)
1223
1232
1322
2231
2213
2312
2321
2123
2132
3122
3221
3212
否则,则必须转化条件,让递归算法在程序中就能判断和剔出重复的情况。比如,对于只可能有两个字符重复的情况,所有重复的串的情况出现在两个紧邻的字符相同的情况下,我们则可以修改递归显示条件为当没有两个相邻字符相同时发生(同样,这条件可能不对,但只是说明问题),程序如下:
import java.util.*;
public class test {
static LinkedList list=new LinkedList();
public static void main(String[] arg) {
Scanner r=new Scanner(System.in);
String s=r.nextLine();
Pailie(s,"");
}
static void Pailie(String s,String p) {
if(s.length()<1) System.out.println(p);
else {
for(int i=0; i<s.length(); i++) {
if(s.length()<2 || (s.charAt(0)!=s.charAt(1)))
Pailie(s.substring(1),p+s.substring(0,1));//递归调用
s=s.substring(1)+s.substring(0,1);//循环移位
}
}
}
}
运行结果为:
1223(输入行)
1232
1223
2312
2321
2123
2132
2231
2213
3212
3221
我们算法原理一样,我只是增加
1。Validate 可以学习Struts,利用抽象Validate集去掉If/Else语句。
2。在同层递增前增加“重复预测“减少不必要的重复排序,提高效率。
所以代码变复杂,你试一下“11111111”,两算法时间空间差别很大,
不过这只是优化问题。
思路是这样的,对于任意一个串利用递归进行排列时,我们是循环串中的每个字符到第一个字符进行递归。如果串中字符出现重复的话,则重复的字符只可以利用递归算法一次,即只要与前面相同的字符循环到第一个字符时不调用递归就可以避免重复,为此,我们只需要按如下方式修改算法:
import java.util.*;
public class test {
static int count=0;
public static void main(String[] arg) {
Scanner r=new Scanner(System.in);
String s=r.nextLine();
Pailie(s,"");
System.out.println("Total:"+count);
}
static void Pailie(String s,String p) {
if(s.length()<1) {
System.out.println(p);//字符串长度小于1,换行
count++;
}
else {
int index[]=new int[s.length()];
for(int i=0; i<s.length(); i++)//该循环将所有字符的第一次出现的位置记录在数组index中
index[i]=s.indexOf(s.charAt(i));
for(int i=0; i<s.length(); i++) {
if(i==index[i])//只有当循环数与第一次记录数相等时才递归,保证相同字符中的第一个调用
Pailie(s.substring(1),p+s.substring(0,1));//递归,打印其它字符
s=s.substring(1)+s.substring(0,1);//循环移位
}
}
}
}
这样,由于相同的字符只递归调用了一次,则避免了重复串的排列。下面是几个典型的运算结果:
2222222(输入。当串中的所有字符相同时,应该递归调用1次)
2222222
Total:1122222(输入。当串中只有一个字符不同时,该字符应该循环所有不同位置)
122222
222221
222212
222122
221222
212222
Total:61223(输入。szlhj的例子)
1223
1232
1322
2231
2213
2312
2321
2123
2132
3122
3221
3212
Total:12122345(输入。本题的要求,最后结果为360个,其它的我就不列出来了,大家可以自己测试)
122345
122354
122453
122435
122534
……
543221
543212
Total:360
如
for(i=122345;i<=543221;i++)
if(第三位不能为四)
{
循环截取两位,如果不为'35'或'53'的都打印出来
}
如
for(i=122345;i<=543221;i++)
if(第三位不能为四)
{
循环截取1位,
if all in(1,2,2,3,4,5)
循环截取两位,
if <>'35' and <>'53'
print() }
{
int i;
for (i = 0; i < MAXN; ++i)
printf("%d", o[i]);
printf("\n");
}void solve(const int depth)
{
int i; for (i = 0; i < MAXN; ++i)
{
if (used[i] > 0)
{
--used[i]; if (
!(i + 1 == 4 && depth == 2) && // 4 at position 3
!(i + 1 == 3 && depth > 0 && o[depth - 1] == 5) && // 53
!(i + 1 == 5 && depth > 0 && o[depth - 1] == 3) // 35
)
{
o[depth] = i + 1;
if (MAXN - 1 == depth)
{
++count;
print();
}
else
solve(depth + 1);
}
else
{
i = i;
} ++used[i];
}
}
}int main()
{
int i; a[0] = 1;
a[1] = 2;
a[2] = 2;
a[3] = 3;
a[4] = 4;
a[5] = 5; for (i = 0; i < MAXN; ++i)
++used[a[i] - 1]; solve(0);
printf("Total count: %d\n", count); return 0;
}运行结果:122345
122543
123245
123254
123425
123452
125234
125243
125423
125432
132245
132254
132425
132452
132524
132542
142325
142523
143225
143252
145223
145232
152234
152243
152324
152342
152423
152432
212345
212543
213245
213254
213425
213452
215234
215243
215423
215432
221345
221543
223145
223154
223415
223451
225134
225143
225413
225431
231245
231254
231425
231452
231524
231542
232145
232154
232415
232451
232514
232541
241325
241523
242315
242513
243125
243152
243215
243251
245123
245132
245213
245231
251234
251243
251324
251342
251423
251432
252134
252143
252314
252341
252413
252431
312245
312254
312425
312452
312524
312542
315224
315242
315422
321245
321254
321425
321452
321524
321542
322145
322154
322415
322451
322514
322541
325124
325142
325214
325241
325412
325421
341225
341252
341522
342125
342152
342215
342251
342512
342521
345122
345212
345221
412325
412523
413225
413252
415223
415232
421325
421523
422315
422513
423125
423152
423215
423251
425123
425132
425213
425231
431225
431252
431522
432125
432152
432215
432251
432512
432521
451223
451232
451322
452123
452132
452213
452231
452312
452321
512234
512243
512324
512342
512423
512432
513224
513242
513422
521234
521243
521324
521342
521423
521432
522134
522143
522314
522341
522413
522431
523124
523142
523214
523241
523412
523421
541223
541232
541322
542123
542132
542213
542231
542312
542321
543122
543212
543221
Total count: 198
我写了一个,大家看看。public class Test{
String s = "22";
String sc[] = {"22"};
int ii = 0;
public void doAdd(String ss){
String sc1[] = new String[sc.length*(sc[0].length()+1)];;
for(int i=0;i<sc.length;i++){
String s = sc[i];
String sc2[] = getStrArray(s,ss);
for(int j=0;j<sc2.length;j++){
sc1[i*sc2.length+j] = sc2[j];
}
}
sc = sc1;
}
public String[] getStrArray(String s,String ss){
String newSc[] = new String[s.length()+1];
for(int i=0;i<=s.length();i++){
String s1 = s.substring(0,i);
String s2 = s.substring(i);
newSc[i] = s1+ss+s2;
}
return newSc;
}
public Test(){
for(int i=3;i<6;i++){
doAdd(i+"");
}
doAdd(1+"");
}
public static void main(String arg[]){
Test t = new Test();
int j = 0;
for(int i=0;i<t.sc.length;i++){
String s = t.sc[i];
if(!(s.indexOf("35")>-1||s.indexOf("53")>-1||s.indexOf("4")==2)){
System.out.println(" 打印 S :"+s);
j++;
}
}
System.out.println(" 符合条件的数共有:"+j+"个");
}
}
#include <vector>
#include <iostream>using namespace std;ostream& operator << (ostream& os, vector<int>& v)
{
for(vector<int>::iterator i = v.begin(); i != v.end(); ++i)
{
cout << *(i) << " ";
} return os;
}bool CanPrint(const vector<int>& position)
{
if(position[4] == 2)
{
return false;
}
else if(position[3] - position[5] == 1 || position[3] - position[5] == -1)
{
return false;
}
else if(position[1] > position[2])
{
return false;
}
else
{
return true;
}
}void Print(const vector<int>& position, const vector<int>& value)
{
static unsigned n = 0; if(CanPrint(position))
{
++n;
vector<int> out(position.size()); for(unsigned i = 0; i < out.size(); ++i)
{
out[position[i]] = value[i];
} cout << n << ":" << out << endl;
}
}vector<int> SwapItem(int i, int j, const vector<int>& position)
{
vector<int> result = position;
int temp = result[i];
result[i] = result[j];
result[j] = temp;
return result;
}void Calculate(unsigned depth, const vector<int>& position, const vector<int>& value)
{
Print(position, value);
for(unsigned i = depth; i < position.size() - 1; ++i)
{
for(unsigned j = i + 1; j < position.size(); ++j)
{
Calculate(i + 1, SwapItem(i, j, position), value);
}
}
}void main(void)
{
vector<int> position(6);
vector<int> value(6); position[0] = 0;
position[1] = 1;
position[2] = 2;
position[3] = 3;
position[4] = 4;
position[5] = 5; value[0] = 1;
value[1] = 2;
value[2] = 2;
value[3] = 3;
value[4] = 4;
value[5] = 5; Calculate(0, position, value);
}
1, 一个文本文件有多行,每行为一个URL。请编写代码,统计出URL中的文件名及出现次数。
a) 文件名不包括域名、路径和URL参数,例如http://www.rs.com/n.op/q/rs?id=1中的文件名是rs。
b) 部分URL可能没有文件名,例如http://www.abc.com/,这类统计为“空文件名”。
c) 出现在不同URL中的相同文件名视为同一文件名,例如http://www.ceshi.com/hi.php
和ftp://ftp.cdef.com/hi.php为同一文件名 文件内容示例如下:
http://www.test.com/abc/de/fg.php?id=1&url=http://www.test.com/index.html
http://www.ceshi.com/hi.jsp
ftp://ftp.ceshi.com/hi.jsp
http://www.hello.com/cw/hi.jsp?k=8
http://www.hi.com/jk/l.html?id=1&s=a.html
http://www.rs.com/n.op/q/rs?id=1
http://www.abc.com/
for(int i=0;i<6;i++)
for(int j=i+;i<6;j++){
// i, j 位置调换
1. String = changePosition(String, i,j);
//if 判断不成立的两种情况
2.截取第三位不为 "3"
3.判断不含"35", "53"
else
print(String);}
}
for(int i=0;i<6;i++)
for(int j=i+1;i<6;j++){
// i, j 位置调换
1. String = changePosition(String, i,j);
//if 判断不成立的两种情况
2.截取第三位不为 "3"
3.判断不含"35", "53"
print(String);}
}
但从看上
个人觉得 survivortt() 的思路很与众不同 有创意
他说:先排列1,2,3,4,5
再插入2,当插入点是2的邻位时,只插左边或只插右边在以上结果中排除要求的两个条件会比较方便吧 多两个IF而已
int i, j, m;
int* digit2;
int num2; if(num <= 0)
return; num2 = num - 1;
digit2 = new int[num2];
for(i = 0; i < num; i++) {
//judge condition, and print result
if(outNum == 2 && digit[i] == 4)
continue;
if(digit[i] == 5 && outNum > 0 && outDigit[outNum - 1] == 3)
continue;
if(digit[i] == 3 && outNum > 0 && outDigit[outNum - 1] == 5)
continue;
if(outDigit[outNum] == digit[i])
continue; outDigit[outNum] = digit[i];
if(num == 1) {
//out the resutl
for(j = 0; j < outNum + 1; j++)
printf("%d", outDigit[j]);
printf("\n");
total++;
}
else {
m = 0;
for(j = 0; j < num; j++) {
if(j != i)
digit2[m++] = digit[j];
} seq(digit2, num2, outDigit, outNum + 1, total);
}
}
delete[] digit2; outDigit[outNum] = 0;}void main()
{
int num;
int digit[1024] = {0};
int outDigit[1024] = {0};
int outNum = 0;
int total = 0;
int digitSeq = 0; //init digit sequence
scanf("%d", &digitSeq);
num = 0;
while(digitSeq) {
digit[num++] = digitSeq % 10;
digitSeq /= 10;
}
printf("\nthe result is:\n");
//calc
seq(digit, num, outDigit, outNum, total); printf("total=%d\n", total);
}
然后用类似冒泡排序,进行求出所以排序可能的值。
再排除:"4"不能在第三位。
最后用POS排除,"3"与"5"不能相连.
本人对算法不是很了解
不知道这样做效率怎样?
学习 all
String[] num = { "1", "2", "2", "3", "4", "5" };
int total = 0;
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
if (j != i) {
for (int h = 0; h < num.length; h++) {
if (h != i && h != j) {
for (int k = 0; k < num.length; k++) {
if (k != i && k != j && k != h) {
for (int m = 0; m < num.length; m++) {
if (m != i && m != j && m != h&& m != k) {
for (int n = 0; n < num.length; n++) {
if (n != i && n != j && n != h&& n != k && n != m) {
System.out.println(num[i]+ num[j] + num[h]+ num[k] + num[m]+ num[n]);
total++;
}
}
}
}
}
}
}
}
}
}
}
System.out.println("total number=" + total);
}........
543122
543212
543221
543212
543221
total number=720
think in java 各章后练习答案.....
http://blog.csdn.net/heimaoxiaozi/
public class Anagram { /**
* @param args
*/
public static void main(String[] args) {
String word="éé23eRf@";
anagram("",word);
}
public static void anagram(String prefix, String suffix) {
String newPrefix,newSuffix;
int numOfChars=suffix.length();
if(numOfChars==1) {
if(!(prefix+suffix).contains("éé")&&(prefix+suffix).indexOf('@')!=2)
{
System.out.println(prefix + suffix);
}
} else {
for(int i=1;i<=numOfChars;i++) {
newSuffix=suffix.substring(1,numOfChars);
newPrefix=prefix+suffix.charAt(0);
anagram(newPrefix, newSuffix);
suffix=newSuffix+suffix.charAt(0);
}
}
}
}
if(contains(0,6,7,8,9,35)) continue;
if(2的个数 != 2)continue;
if(4的位数 = 3) continue;
println(i);
}
if(contains(0,6,7,8,9,35)) continue;
if(contains(1,2,3,4,5)) continue;
if(2的个数 != 2)continue;
if(4的位数 = 3) continue;
println(i);
}
比如一个字符串: cat
经变换得到atc然后到tac(就是把后面字母提到前面).
同理cat--cta
tac---tca, atc--act.
然后操作特例情况,比如哪些字母不能在一起.......
建议: 不要看到数字就往数学方面想,(大的工程,用数学公式除外)
象这样小的问题,往往跟数学分开比较简单.
newPrefix=prefix+suffix.charAt(0);
anagram(newPrefix, newSuffix);
suffix=newSuffix+suffix.charAt(0);
这就是把字母交换位置操作,交换完了,然后在递归到下一步.
for(int i=122345;i<=543221;i++) {
if(contains(0,6,7,8,9,35)) continue;
if(contains(1,2,3,4,5)) continue;
if(2的个数 != 2)continue;
if(4的位数 = 3) continue;
println(i);
}
for(int i=122345;i<=543221;i++) {
if(contains(0,6,7,8,9,35)) continue;
if(contains(1,2,3,4,5)) continue;
if(2的个数 != 2)continue;
if(4的位数 = 3) continue;
println(i);
}
的算法有问题,有很多种情况没考虑到,132453,132553,154432...这样的数满足要求吗?
package com.lily.test;public class Test
{
private void arrange(int pos, int[] seed, int[] result, int preData)
{
if(pos >= result.length)
{
sum++;
print(result);
return;
} for(int i = 0; i < seed.length; i++)
{
int curValue = seed[i];
if(i >= 1 && curValue == seed[i - 1])continue;
if(pos == 2 && curValue == 4)continue;
if(preData * curValue == 15)continue;
result[pos] = curValue;
int[] newSeed = new int[seed.length - 1];
copyArray(newSeed, seed, i);
arrange(pos + 1, newSeed, result, curValue);
}
} private void copyArray(int[] dest, int[] source, int eliminatePos)
{
int n = 0;
for(int i = 0; i < source.length; i++)
{
if(i == eliminatePos)continue;
dest[n++] = source[i];
}
} private void print(int[] result)
{
for(int i = 0; i < result.length; i++)
{
System.out.print(result[i]);
if(i != result.length - 1)
{
System.out.print(',');
}
}
System.out.println();
} int sum; public static void main(String[] args)
{
int[] seed = new int[]
{1, 2, 2, 3, 4, 5}; // 如果没有排序,请先排序
int[] result = new int[6];
Test test = new Test();
test.arrange(0, seed, result, -1);
System.out.println("Total:" + test.sum);
}
}
比如 @|##|}]´[[
:)
数组的元素是一个string. 比如 122345, 522413 等等
2. 历遍整个数组用正规表达式去判断这个元素是不是符合规格
比如 122435
规则1 。 match=[//d][//d][//d][4]
规则2。 match=[35]|[53]
如果正规表达式匹配的结果数大于0, 说明这个元素不是我们要得
当匹配的结果等于0,则把这个元素加入一个新的集合中去
3. 新的集合就是我们要得结果集好处: 1不要动脑子想,思路清楚。 2对于数字,字符,都适合。 3。规则的修改快速,比如要求改为第4位不能为1, 2和3不能相邻等, 只学要改规则就可以了。
if(p.contains("35")==false && p.contains("53")==false && p.charAt(2)!=('4')){
System.out.println(p);//字符串长度小于1,换行
count++;
}这样你的结果正好198 该是正确答案了
public class Test {
public static void main(String[] args) {
for (int a = 1; a < 7; a++) {
for (int b = 1; b < 7; b++) {
if (b == a) continue;
if ((b == 3) && (a == 5)) continue;
if ((b == 5) && (a == 3)) continue;
for (int c = 1; c < 7; c++) {
if (c == a) continue;
if (b == c) continue;
if (c == 4) continue;
if ((b == 3) && (c == 5)) continue;
if ((b == 5) && (c == 3)) continue;
for (int d = 1; d < 7; d++) {
if (d == a) continue;
if (b == d) continue;
if (c == d) continue;
if ((d == 3) && (c == 5)) continue;
if ((d == 5) && (c == 3)) continue;
for (int e = 1; e < 7; e++) {
if (e == a) continue;
if (b == e) continue;
if (c == e) continue;
if (e == d) continue;
if ((d == 3) && (e == 5)) continue;
if ((d == 5) && (e == 3)) continue;
for (int f = 1; f < 7; f++) {
if (f == a) continue;
if (b == f) continue;
if (c == f) continue;
if (e == f) continue;
if (f == d) continue;
if ((d == 3) && (f == 5)) continue;
if ((d == 5) && (f == 3)) continue;
String r = "" + a + b + c + d + e + f;
r = r.replace('6', '2');
System.out.println(r);
}
}
}
}
}
}
}}
public class Test {
public static void main(String[] args) {
for (int a = 1; a < 7; a++) {
for (int b = 1; b < 7; b++) {
if (b == a) continue;
if ((b == 3) && (a == 5)) continue;
if ((b == 5) && (a == 3)) continue;
for (int c = 1; c < 7; c++) {
if (c == a) continue;
if (b == c) continue;
if (c == 4) continue;
if ((b == 3) && (c == 5)) continue;
if ((b == 5) && (c == 3)) continue;
for (int d = 1; d < 7; d++) {
if (d == a) continue;
if (b == d) continue;
if (c == d) continue;
if ((d == 3) && (c == 5)) continue;
if ((d == 5) && (c == 3)) continue;
for (int e = 1; e < 7; e++) {
if (e == a) continue;
if (b == e) continue;
if (c == e) continue;
if (e == d) continue;
if ((d == 3) && (e == 5)) continue;
if ((d == 5) && (e == 3)) continue;
for (int f = 1; f < 7; f++) {
if (f == a) continue;
if (b == f) continue;
if (c == f) continue;
if (e == f) continue;
if (f == d) continue;
if ((d == 3) && (f == 5)) continue;
if ((d == 5) && (f == 3)) continue;
String r = "" + a + b + c + d + e + f;
r = r.replace('6', '2');
System.out.println(r);
}
}
}
}
}
}
}
}