虽然给鱼不如教钓鱼,不过思路还真不好解释,所以附上source code
想法很简单 把所有可能都跑过就行了
用recursive是因为比较好处理多重条件(这次取1个或者取2个)
private void splitNumber(String remain, String handled) {
if (remain.length() == 0)
System.out.println(handled.substring(0, handled.length() -2));
} else if (remain.length() == 1) {
System.out.println(handled + remain);
} else { // s.lenght() >= 2
splitNumber(remain.substring(1), handled + remain.substring(0, 1) + ", ");
splitNumber(remain.substring(2), handled + remain.substring(0, 2) + ", ");
}
}
使用function:
splitNumber("1234567", "");
结果:
1, 2, 3, 4, 5, 6, 7
1, 2, 3, 4, 5, 67
1, 2, 3, 4, 56, 7
1, 2, 3, 45, 6, 7
1, 2, 3, 45, 67
1, 2, 34, 5, 6, 7
1, 2, 34, 5, 67
1, 2, 34, 56, 7
1, 23, 4, 5, 6, 7
1, 23, 4, 5, 67
1, 23, 4, 56, 7
1, 23, 45, 6, 7
1, 23, 45, 67
12, 3, 4, 5, 6, 7
12, 3, 4, 5, 67
12, 3, 4, 56, 7
12, 3, 45, 6, 7
12, 3, 45, 67
12, 34, 5, 6, 7
12, 34, 5, 67
12, 34, 56, 7
※ 引述《OoShiunoO (机机勋)》之铭言:
: 请问各位大大
: 如果我有一个数字, ex: 213112
: 我要怎么把它拆成各种组合呢?(题目是限定最大只能拆成10位数)
: 2,1,3,1,1,2
: 21,3,1,1,2
: 2,13,1,1,2
: .
: .
: .
: .
: 21,31,12
: 烦请各位大大给个提示,谢谢