最新评论
顶天的蚂蚁 2012-03-17 11:10
使用Or操作数(|)就可以选择一个以上的路径。例如:
/catalog/cd/title | catalog/cd/artist
这个漏掉了一个 '/',嘿嘿
/catalog/cd/title | /catalog/cd/artist
水手~ 2009-12-16 13:16
呵呵,个人觉得此题用图遍历并不合适,一方面多个数组,特别是TreeSet的使用,内存消耗比较大;另一方面,"重复的2"和"4不能在第三位"这些条件仍然需要完全遍历后才能够排除。我觉得该题更适合使用递归加剪枝的方式。这样既不需要状态数组,又可以及时地剪去那些不符合要求的分支,代码如下:
public class PermutationAlgo {
private int count = 0;
public void calculate(){
String eleStr = "122345";
depthSearch(eleStr, "");
System.out.println("符合条件的总结果数为:"+count+"条");
}
/**
* @param eleStr - 待分配字符组成的串
* @param rstStr - 已分配字符组成的串
*/
public void depthSearch(String eleStr, String rstStr) {
if (eleStr.length() == 0) {
count++;
System.out.println(rstStr);
return;
}
for (int i = 0; i < eleStr.length(); i++) {
String currEle = eleStr.substring(i, i + 1); //取出当前位的值
if (rstStr.length() == 2 && "4".equals(currEle)) continue; //剪掉第三位为4的分支
if (rstStr.endsWith("3") && "5".equals(currEle)) continue; //剪掉"35"相连的分支
if (rstStr.endsWith("5") && "3".equals(currEle)) continue; //剪掉"53"相连的分支
if (eleStr.substring(0, i).indexOf(currEle) != -1) continue; //剪掉同一位上字符重复的分支(此题即剪掉重复的2)
depthSearch(eleStr.substring(0, i) + eleStr.substring(i + 1), rstStr + currEle); //用剩余的合法串继续递归
}
}
public static void main(String[] args) {
new PermutationAlgo().calculate();
}
}
llllllllllllllll 2009-07-24 19:56
博主能告诉我简单实现ftp服务器和客户端功能的VC++程序么,谢谢了,有急用,你可以发到我邮箱里
发个 2009-05-08 08:58
public static void main(String args [])
{
for(int i = 122345; i <= 543221; i++)
{
if(!a(i))
System.out.println(i);
}
}
public static boolean a(int n)
{
String a = "" + n;
int sum = 0;
for(int i = 0; i < a.length(); i++)
{
if(a.charAt(i) == '2')
sum++;
}
if(a.charAt(2) == '4')
return true;
if(a.indexOf('6') != -1 || a.indexOf('7') != -1 ||
a.indexOf('8') != -1 || a.indexOf('9') != -1 || a.indexOf('0') != -1)
return true;
if(a.indexOf("35") != -1 || a.indexOf("53") != -1)
return true;
if(a.indexOf('1') != a.lastIndexOf('1') ||a.indexOf('3') != a.lastIndexOf('3') ||
a.indexOf('4') != a.lastIndexOf('4') || a.indexOf('5') != a.lastIndexOf('5'))
return true;
if(sum >= 3 || sum ==1)
return true;
return false;
}
{
for(int i = 122345; i <= 543221; i++)
{
if(!a(i))
System.out.println(i);
}
}
public static boolean a(int n)
{
String a = "" + n;
int sum = 0;
for(int i = 0; i < a.length(); i++)
{
if(a.charAt(i) == '2')
sum++;
}
if(a.charAt(2) == '4')
return true;
if(a.indexOf('6') != -1 || a.indexOf('7') != -1 ||
a.indexOf('8') != -1 || a.indexOf('9') != -1 || a.indexOf('0') != -1)
return true;
if(a.indexOf("35") != -1 || a.indexOf("53") != -1)
return true;
if(a.indexOf('1') != a.lastIndexOf('1') ||a.indexOf('3') != a.lastIndexOf('3') ||
a.indexOf('4') != a.lastIndexOf('4') || a.indexOf('5') != a.lastIndexOf('5'))
return true;
if(sum >= 3 || sum ==1)
return true;
return false;
}
mbc 2009-03-22 21:46
--引用--------------------------------------------------
mbc: 胡老师,我生成的不能应用,可不可以给我一个你生成的动态链接库。谢谢
--------------------------------------------------------
mbc: 胡老师,我生成的不能应用,可不可以给我一个你生成的动态链接库。谢谢
--------------------------------------------------------
宋志朋 2009-03-19 17:22
package suanfa;
import java.util.TreeSet;
public class ShuPaiXu {
private static TreeSet ts1=new TreeSet();
public static boolean re(String a){
if(a.indexOf("0")!=-1)
return true;
else if(a.indexOf("7")!=-1)
return true;
else if(a.indexOf("8")!=-1)
return true;
else if(a.indexOf("9")!=-1)
return true;
else if(a.indexOf("4")==2)
return true;
else if(a.indexOf("35")!=-1)
return true;
else if(a.indexOf("53")!=-1)
return true;
else {
for(int i=1;i<7;i++){
if(a.indexOf(i+"")!=a.lastIndexOf(i+""))
return true;
}
return false;
}
}
public static void main(String[] args){
for(int i=123456;i<=654321;i++){
if(!re(i+"")){
ts1.add((i+"").replace('6', '2'));
}
}
for(Object o:ts1){
System.out.println(o);
}
}
}
import java.util.TreeSet;
public class ShuPaiXu {
private static TreeSet ts1=new TreeSet();
public static boolean re(String a){
if(a.indexOf("0")!=-1)
return true;
else if(a.indexOf("7")!=-1)
return true;
else if(a.indexOf("8")!=-1)
return true;
else if(a.indexOf("9")!=-1)
return true;
else if(a.indexOf("4")==2)
return true;
else if(a.indexOf("35")!=-1)
return true;
else if(a.indexOf("53")!=-1)
return true;
else {
for(int i=1;i<7;i++){
if(a.indexOf(i+"")!=a.lastIndexOf(i+""))
return true;
}
return false;
}
}
public static void main(String[] args){
for(int i=123456;i<=654321;i++){
if(!re(i+"")){
ts1.add((i+"").replace('6', '2'));
}
}
for(Object o:ts1){
System.out.println(o);
}
}
}
NicolasZ 2009-03-17 16:55
--引用--------------------------------------------------
cat11: 你好,我是新手啊,按照你这的介绍一步一步配置好了,但是运行的时候老是报错,说“该用户与可信 SQL Server 连接无关联。”什么意思呢?用户名和密码应该怎样设置呢?谢谢
--------------------------------------------------------
如果用户名密码配置正确的话估计你SQL Server没有设成混合登陆模式
cat11: 你好,我是新手啊,按照你这的介绍一步一步配置好了,但是运行的时候老是报错,说“该用户与可信 SQL Server 连接无关联。”什么意思呢?用户名和密码应该怎样设置呢?谢谢
--------------------------------------------------------
如果用户名密码配置正确的话估计你SQL Server没有设成混合登陆模式
hdxiong 2009-03-13 10:10
private static String[] mustExistNumber = new String[] { "1", "2", "2",
"3", "4", "5" };
private static boolean isValidNumber(String str) {
// 检查是否包含12345这五个数,不包含返回false
for (String number : mustExistNumber) {
if (str.indexOf(number) < 0)
return false;
}
// 检查是否有两个2,只有一个返回false
if (str.lastIndexOf("2") == str.indexOf("2")) {
return false;
}
// 检查4在不在第三位,是返回false
if (str.charAt(2) == '4') {
return false;
}
// 检查是否存在35在一起,有返回false
if (str.indexOf("35") >= 0 || str.indexOf("53") >= 0) {
return false;
}
return true;
}
public static void main(String[] args) {
// TODO code application logic here
for (int i = 122345; i < 543221; i++) {
if (isValidNumber(String.valueOf(i))) {
System.out.println(i);
}
}
}
"3", "4", "5" };
private static boolean isValidNumber(String str) {
// 检查是否包含12345这五个数,不包含返回false
for (String number : mustExistNumber) {
if (str.indexOf(number) < 0)
return false;
}
// 检查是否有两个2,只有一个返回false
if (str.lastIndexOf("2") == str.indexOf("2")) {
return false;
}
// 检查4在不在第三位,是返回false
if (str.charAt(2) == '4') {
return false;
}
// 检查是否存在35在一起,有返回false
if (str.indexOf("35") >= 0 || str.indexOf("53") >= 0) {
return false;
}
return true;
}
public static void main(String[] args) {
// TODO code application logic here
for (int i = 122345; i < 543221; i++) {
if (isValidNumber(String.valueOf(i))) {
System.out.println(i);
}
}
}
shenyan 2009-03-10 08:41
else if((str.indexOf(number, temp+1)>temp)&&str.charAt(temp) != '2')
这句话是什么意思?
这句话是什么意思?
超古 2009-03-06 22:53
//上面的有些试过了,没有解决像1、3、4、5重复的问题,我改了一下应该能行
public class Main {
private static String[] forbidenNumber = new String[]{"0","6","7","8","9"};
private static String[] mustExistNumber = new String[]{"1","2","2","3","4","5"};
private static boolean isValidNumber(String str){
//检查是否有非法数字,有返回false,否则继续
for(String number:forbidenNumber){
if(str.indexOf(number) >= 0){
return false;
}
}
//检查是否存在要的数字,如果不存在返回false,否则继续
for(String number:mustExistNumber){
int temp = str.indexOf(number);
if(temp<0){
return false;
}
else if((str.indexOf(number, temp+1)>temp)&&str.charAt(temp) != '2'){
return false;
}
}
//检查4在不在第三位,是返回false
if(str.charAt(2) == '4'){
return false;
}
//检查是否存在35在一起,有返回false
if(str.indexOf("35") >= 0||str.indexOf("53") >= 0){
return false;
}
return true;
}
/**
* @param args the command line arguments
*/
// public static boolean Test(int i){
// String str = ""+i;
// if(str.charAt(2) == '4'){
// return true;
// }
// if(str.indexOf("35") != -1||str.indexOf("53") != -1){
// return true;
// }
// return false;
// }
public static void main(String[] args) {
// TODO code application logic here
for(int i = 122345;i<543221;i++){
if(isValidNumber(String.valueOf(i))){
System.out.println(i);
}
}
}
}
public class Main {
private static String[] forbidenNumber = new String[]{"0","6","7","8","9"};
private static String[] mustExistNumber = new String[]{"1","2","2","3","4","5"};
private static boolean isValidNumber(String str){
//检查是否有非法数字,有返回false,否则继续
for(String number:forbidenNumber){
if(str.indexOf(number) >= 0){
return false;
}
}
//检查是否存在要的数字,如果不存在返回false,否则继续
for(String number:mustExistNumber){
int temp = str.indexOf(number);
if(temp<0){
return false;
}
else if((str.indexOf(number, temp+1)>temp)&&str.charAt(temp) != '2'){
return false;
}
}
//检查4在不在第三位,是返回false
if(str.charAt(2) == '4'){
return false;
}
//检查是否存在35在一起,有返回false
if(str.indexOf("35") >= 0||str.indexOf("53") >= 0){
return false;
}
return true;
}
/**
* @param args the command line arguments
*/
// public static boolean Test(int i){
// String str = ""+i;
// if(str.charAt(2) == '4'){
// return true;
// }
// if(str.indexOf("35") != -1||str.indexOf("53") != -1){
// return true;
// }
// return false;
// }
public static void main(String[] args) {
// TODO code application logic here
for(int i = 122345;i<543221;i++){
if(isValidNumber(String.valueOf(i))){
System.out.println(i);
}
}
}
}
nevergofishing 2009-02-11 20:07
居然写错了。
这个才是对的。
private static String[] forbiddenNumber = new String[] { "0", "6", "7", "8", "9" };
private static String[] mustExistNumber = new String[] { "1", "2", "3", "4", "5" };
private static boolean isValidNumber(String s) {
// first check if 0, 6, 7, 8, 9 does not exsit.
for (String number : forbiddenNumber) {
if (s.indexOf(number) >= 0) {
return false;
}
}
// check if 1, 2, 3, 4, 5 exist.
for (String number : mustExistNumber) {
if (s.indexOf(number) < 0) {
return false;
}
}
// check if the 3 number is 4.
if (s.charAt(2) == '4') {
return false;
}
// check if 35 or 53 exists.
if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
return false;
}
return true;
}
public static void main(String[] arg) {
for (int i = 112345; i <= 554321; i++) {
if (isValidNumber(String.valueOf(i))) {
System.out.println(i);
}
}
}
这个才是对的。
private static String[] forbiddenNumber = new String[] { "0", "6", "7", "8", "9" };
private static String[] mustExistNumber = new String[] { "1", "2", "3", "4", "5" };
private static boolean isValidNumber(String s) {
// first check if 0, 6, 7, 8, 9 does not exsit.
for (String number : forbiddenNumber) {
if (s.indexOf(number) >= 0) {
return false;
}
}
// check if 1, 2, 3, 4, 5 exist.
for (String number : mustExistNumber) {
if (s.indexOf(number) < 0) {
return false;
}
}
// check if the 3 number is 4.
if (s.charAt(2) == '4') {
return false;
}
// check if 35 or 53 exists.
if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
return false;
}
return true;
}
public static void main(String[] arg) {
for (int i = 112345; i <= 554321; i++) {
if (isValidNumber(String.valueOf(i))) {
System.out.println(i);
}
}
}
nevergofishing 2009-02-11 20:04
才6位数字, brute force 就够了。
private static String[] forbiddenNumber = new String[] { "0", "6", "7", "8", "9" };
private static String[] mustExistNumber = new String[] { "1", "2", "3", "4", "5" };
private static boolean isValidNumber(String s) {
// first check if 0, 6, 7, 8, 9 does not exsit.
for (String number : forbiddenNumber) {
if (s.indexOf(number) >= 0) {
return false;
}
}
// check if 1, 2, 3, 4, 5 exist.
for (String number : mustExistNumber) {
if (s.indexOf(number) < 0) {
return false;
}
}
// check if the 3 number is 4.
if (s.charAt(2) == '4') {
return false;
}
// check if 35 or 53 exists.
if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
return false;
}
return true;
}
public static void main(String[] arg) {
for (int i = 122345; i <= 543221; i++) {
if (isValidNumber(String.valueOf(i))) {
System.out.println(i);
}
}
}
private static String[] forbiddenNumber = new String[] { "0", "6", "7", "8", "9" };
private static String[] mustExistNumber = new String[] { "1", "2", "3", "4", "5" };
private static boolean isValidNumber(String s) {
// first check if 0, 6, 7, 8, 9 does not exsit.
for (String number : forbiddenNumber) {
if (s.indexOf(number) >= 0) {
return false;
}
}
// check if 1, 2, 3, 4, 5 exist.
for (String number : mustExistNumber) {
if (s.indexOf(number) < 0) {
return false;
}
}
// check if the 3 number is 4.
if (s.charAt(2) == '4') {
return false;
}
// check if 35 or 53 exists.
if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0) {
return false;
}
return true;
}
public static void main(String[] arg) {
for (int i = 122345; i <= 543221; i++) {
if (isValidNumber(String.valueOf(i))) {
System.out.println(i);
}
}
}
马辉 2008-12-02 13:54
import java.util.*;
public class test{
public static void main(String[] args){
test t=new test();
}
public test(){
search(1);
System.out.println(count);
}
private void search(int depth){
boolean flag=false;
int currennum;
if(depth==7){
System.out.println();
for(int num:newnum){
System.out.print(num+" ");
}
count=count+1;
return;
}
if(searched[1] ==0 && searched[2]==0){
flag=true;
}
for(int i=0;i<6;i++){
if(searched[i]!=0)continue;
if(i==2 && flag)continue;
currennum=num[i];
if(depth==3 && currennum==4)continue;
if(depth!=1 && (currennum==5 || currennum==3)){
if(newnum[depth-2]==5 || newnum[depth-2]==3)continue;
}
newnum[depth-1]=currennum;
searched[i]=1;
search(depth+1);
searched[i]=0;
}
}
private int[] num={1,2,2,3,4,5};
private int[] newnum={0,0,0,0,0,0};
private int[] searched={0,0,0,0,0,0};
private int count=0;
}
public class test{
public static void main(String[] args){
test t=new test();
}
public test(){
search(1);
System.out.println(count);
}
private void search(int depth){
boolean flag=false;
int currennum;
if(depth==7){
System.out.println();
for(int num:newnum){
System.out.print(num+" ");
}
count=count+1;
return;
}
if(searched[1] ==0 && searched[2]==0){
flag=true;
}
for(int i=0;i<6;i++){
if(searched[i]!=0)continue;
if(i==2 && flag)continue;
currennum=num[i];
if(depth==3 && currennum==4)continue;
if(depth!=1 && (currennum==5 || currennum==3)){
if(newnum[depth-2]==5 || newnum[depth-2]==3)continue;
}
newnum[depth-1]=currennum;
searched[i]=1;
search(depth+1);
searched[i]=0;
}
}
private int[] num={1,2,2,3,4,5};
private int[] newnum={0,0,0,0,0,0};
private int[] searched={0,0,0,0,0,0};
private int count=0;
}
