A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 爱吃柠檬   /  2016-8-24 23:01  /  1615 人查看  /  32 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

阿卜 发表于 2016-8-26 11:42
题目不是要求解多少种组合吗……难道打印出来了一个一个数?

嗯嗯,明白了,刚开始学有点蒙
回复 使用道具 举报
阿卜 发表于 2016-8-26 11:42
题目不是要求解多少种组合吗……难道打印出来了一个一个数?

嗯嗯,明白了,刚开始学有点蒙
回复 使用道具 举报
阿卜 发表于 2016-8-26 11:42
题目不是要求解多少种组合吗……难道打印出来了一个一个数?

嗯嗯,明白了,刚开始学有点蒙
回复 使用道具 举报
阿卜 发表于 2016-8-25 17:47
好像这个是最笨的做法……再想想

[mw_shl_code=java,true]public class Practice {

多谢
回复 使用道具 举报
gsa798780633 发表于 2016-8-26 19:42
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

多谢
回复 使用道具 举报
好好想想啊
回复 使用道具 举报
学习学习!
回复 使用道具 举报
看起来好号复杂的样子!
回复 使用道具 举报
排列组合?
回复 使用道具 举报
有点难啊
回复 使用道具 举报
支持一下
回复 使用道具 举报
[Java] 纯文本查看 复制代码
class Test {
	public static void main(String[] args){
		int cnt2 = fun01("", "1234");
		System.out.println("cnt: "+cnt2);
		int cnt3 = fun02("", "1234");
		System.out.println("cnt: "+cnt3);
	}
	public static int fun01(String res, String source){
		if ( res.length() >= source.length()) {		//保证长度			
			//System.out.println(res);
			return 1;
		}
		int cnt = 0;
		for ( char c : source.toCharArray() ) {
			if ( res.contains( c+"" ) ) {			//保证不重复
				continue;
			}
			cnt += fun01(res+c, source);
		}
		return cnt;
	}
	public static int fun02(String res, String source){
		if ( res.length() >= source.length()) {		//保证长度
			//System.out.println(res);
			return 1;
		}
		int cnt = 0;
		for ( char c : source.toCharArray() ) {
			if ( res.contains( c+"" ) ) {			//保证不重复
				continue;
			}
			if ( "".equals(res) && c == '4' ) {		//4不能开头
				continue;
			}
			if ( (res.endsWith("3") && c=='1') || (res.endsWith("1") && c=='3') ) { //1,3不在一起
				continue;
			}
			cnt += fun02(res+c, source);
		}
		return cnt;
	}
}
回复 使用道具 举报
好赞顶,支持。。。
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马