浏览 5814 次
锁定老帖子 主题:[笔试题]求乘法结合律的个数
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-24
三个数的乘法:a*b*c,共有两种结合方式:(a*b)*c,a*(b*c) 四个数的乘法:a*b*c*d,共有五种结合方式:((a*b)*c)*c, (a*(b*c))*d, a*((b*c)*d), a*(b*(c*d)), (a*b)*(c*d) 让你写一个函数,参数是乘数的个数,返回值是用乘法结合律后可能的结合方式总数。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-06-25
怎么没人回答呢?大家开动脑筋想一想啊,我是没想出方法来
|
|
返回顶楼 | |
发表时间:2008-06-27
思路:迭代n = k的时候的结果集,
依次将每个字符串中的 x 替换成 (x * x) 比如 (x * x) * x ,经过替换,可以得到以下三个结果: ((x * x) * x) * x, (x * (x * x)) * x, (x * x) * (x * x) 最后,去掉重复的结果即可 迭代过程如下图所示 源代码如下: package org.ibatis.helper.test; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Associativity { public static void main(String[] args) { int n = 6; Set set = extractAssociativity(n); } private static Set replaceResult4Display(Set set) { Set result = new HashSet(); for (Iterator i = set.iterator() ; i.hasNext() ; ) { String str = (String)i.next(); int strLength = str.length(); StringBuffer sb = new StringBuffer(); int count = 0; for (int j = 0 ; j < strLength ; j++) { char c = str.charAt(j); sb.append(c == 'X' ? (char)('a' + count++) : c); } result.add(sb.toString()); } return result; } public static Set extractAssociativity(int n) { if (n < 2 ){ return Collections.EMPTY_SET; } Set result = new HashSet(); result.add("X * X"); if (n == 2 ){ return result; } for (int i = 3 ; i <= n ; i++) { result = subExtractAssociativity(result); System.out.println("i : " + i + "\tcount : " + result.size()); System.out.println("-------------------------------------------"); Set set = replaceResult4Display(result); for (Iterator iter = set.iterator() ; iter.hasNext() ; ) { System.out.println(iter.next()); } System.out.println("\n******************************************\n"); } return result; } private static Set subExtractAssociativity(Set set) { Set result = new HashSet(); for (Iterator iter = set.iterator() ; iter.hasNext() ; ) { String str = (String)iter.next(); int strLength = str.length(); for (int i = 0 ; i < strLength ; i++) { char c = str.charAt(i); if (c == 'X' ){ StringBuffer sb = new StringBuffer(); for (int j = 0 ; j < strLength ; j++) { if (j == i ){ sb.append("(X * X)"); } else sb.append(str.charAt(j)); } result.add(sb.toString()); } } } return result; } } 结果如下: 引用 i : 3 count : 2 ------------------------------------------- a * (b * c) (a * b) * c ****************************************** i : 4 count : 5 ------------------------------------------- ((a * b) * c) * d a * (b * (c * d)) a * ((b * c) * d) (a * (b * c)) * d (a * b) * (c * d) ****************************************** i : 5 count : 14 ------------------------------------------- ((a * (b * c)) * d) * e ((a * b) * c) * (d * e) a * (b * (c * (d * e))) a * ((b * c) * (d * e)) (a * (b * c)) * (d * e) ((a * b) * (c * d)) * e a * (b * ((c * d) * e)) (a * b) * ((c * d) * e) a * ((b * (c * d)) * e) (((a * b) * c) * d) * e (a * b) * (c * (d * e)) (a * (b * (c * d))) * e (a * ((b * c) * d)) * e a * (((b * c) * d) * e) ****************************************** i : 6 count : 42 ------------------------------------------- a * ((b * ((c * d) * e)) * f) (a * (b * c)) * (d * (e * f)) ((a * (b * (c * d))) * e) * f (((a * b) * (c * d)) * e) * f a * (((b * c) * d) * (e * f)) (a * (b * (c * (d * e)))) * f (((a * b) * c) * (d * e)) * f (a * b) * (c * ((d * e) * f)) a * ((b * (c * (d * e))) * f) (a * b) * ((c * (d * e)) * f) ((a * b) * c) * (d * (e * f)) ((a * (b * c)) * d) * (e * f) ((a * b) * (c * d)) * (e * f) (a * (((b * c) * d) * e)) * f a * ((b * c) * (d * (e * f))) (a * ((b * (c * d)) * e)) * f (((a * b) * c) * d) * (e * f) (a * (b * (c * d))) * (e * f) ((a * b) * ((c * d) * e)) * f a * (b * ((c * d) * (e * f))) (a * b) * (c * (d * (e * f))) ((a * (b * c)) * (d * e)) * f a * (b * ((c * (d * e)) * f)) a * ((b * (c * d)) * (e * f)) a * (b * (((c * d) * e) * f)) a * ((b * c) * ((d * e) * f)) ((((a * b) * c) * d) * e) * f (a * ((b * c) * d)) * (e * f) a * (((b * c) * (d * e)) * f) a * (((b * (c * d)) * e) * f) a * ((((b * c) * d) * e) * f) ((a * b) * c) * ((d * e) * f) (a * ((b * c) * (d * e))) * f (a * (b * ((c * d) * e))) * f ((a * b) * (c * (d * e))) * f a * (b * (c * ((d * e) * f))) a * (b * (c * (d * (e * f)))) (a * b) * (((c * d) * e) * f) (((a * (b * c)) * d) * e) * f ((a * ((b * c) * d)) * e) * f (a * (b * c)) * ((d * e) * f) (a * b) * ((c * d) * (e * f)) ****************************************** 引用 i : 3 count : 2 i : 4 count : 5 i : 5 count : 14 i : 6 count : 42 i : 7 count : 132 i : 8 count : 429 i : 9 count : 1430 i : 10 count : 4862 i : 11 count : 16796 i : 12 count : 58786 i : 13 count : 208012 |
|
返回顶楼 | |
发表时间:2008-06-27
没这么复杂。设k个数乘法结合方式有S(k),S(k+1)只比S(k)多有限种方式。可以推导出来
|
|
返回顶楼 | |
发表时间:2008-07-30
f(1)=1,f(2)=1
f(n)=∑(f(k)*f(n-k)) | k=1,n-1 n>2 --------------------------------------- public class Test2 { public long f(int n) { assert(n>0); if(n<=2) return 1; long count=0; for(int i=1;i<n;++i) { count=count+c[i]*c[n-i]; } return count; } long[] c; public static void main(String[] args) { long start=System.currentTimeMillis(); Test2 test = new Test2(); int n=36; test.c=new long[n+1]; for(int i=1;i<=n;++i) { test.c[i]=test.f(i); System.out.println("f("+i+")="+test.c[i]); } System.out.println("use "+(System.currentTimeMillis()-start)+"毫秒"); } } ------------------------------------------ f(1)=1 f(2)=1 f(3)=2 f(4)=5 f(5)=14 f(6)=42 f(7)=132 f(8)=429 f(9)=1430 f(10)=4862 f(11)=16796 f(12)=58786 f(13)=208012 f(14)=742900 f(15)=2674440 f(16)=9694845 f(17)=35357670 f(18)=129644790 f(19)=477638700 f(20)=1767263190 f(21)=6564120420 f(22)=24466267020 f(23)=91482563640 f(24)=343059613650 f(25)=1289904147324 f(26)=4861946401452 f(27)=18367353072152 f(28)=69533550916004 f(29)=263747951750360 f(30)=1002242216651368 f(31)=3814986502092304 f(32)=14544636039226909 f(33)=55534064877048198 f(34)=212336130412243110 f(35)=812944042149730764 f(36)=3116285494907301262 use 16毫秒 |
|
返回顶楼 | |
发表时间:2008-07-30
典型的DP
|
|
返回顶楼 | |
发表时间:2008-07-30
很赶的写了一个, 但有重复的问题, 去掉即可. 拉肚子中~```
import java.util.ArrayList; import java.util.List; public class ParseGroupTest { public static void main(String[] args) { ParseGroupTest test = new ParseGroupTest(); test.parseGroup("a", "b", "c", "d", "e"); } /** * 存储结果的地方. 注: 代码不严谨, 没有处理相同的情况. 里面可能toString()会有相同的对象. */ private List<Group> result = new ArrayList<Group>(); public void parseGroup(String... arrays) { if (arrays == null || arrays.length < 2) { return; } List<Group> groups = new ArrayList<Group>(); for (int i = 0; i < arrays.length; i ++) { Group group = new SingleGroup(arrays[i]); groups.add(group); } this.parseGroup(groups); for (int i = 0; i < result.size(); i ++) { System.out.println(result.get(i)); } } public void parseGroup(List<Group> groups) { if (groups == null || groups.size() == 0) { return; } if (groups.size() == 1) { result.add(groups.get(0)); return; } if (groups.size() == 2) { result.add(new GroupGroup(groups.get(0), groups.get(1))); return; } for (int i = 0; i < groups.size() - 1; i ++) { List<Group> newGroup = new ArrayList<Group>(); for (int j = 0; j < i; j ++) { newGroup.add(groups.get(j)); } newGroup.add(new GroupGroup(groups.get(i), groups.get(i + 1))); for (int k = i+ 2; k < groups.size(); k ++) { newGroup.add(groups.get(k)); } this.parseGroup(newGroup); } } /** * 基类 */ class Group { } /** * 单个String */ class SingleGroup extends Group { String body; public SingleGroup(String body) { this.body = body; } public String toString() { return body; } } /** * Group 与Group的组合 */ class GroupGroup extends Group { Group leftGroup; Group rightGroup; public GroupGroup(Group leftGroup, Group rightGroup) { this.leftGroup = leftGroup; this.rightGroup = rightGroup; } @Override public String toString() { return "(" + leftGroup.toString() + "*" + rightGroup.toString() + ")"; } } } a,b,c 的输出: ((a*b)*c) (a*(b*c)) "a", "b", "c", "d"的输出: (((a*b)*c)*d) ((a*b)*(c*d)) ((a*(b*c))*d) (a*((b*c)*d)) ((a*b)*(c*d)) -> 重复的 (a*(b*(c*d))) |
|
返回顶楼 | |
发表时间:2009-01-14
arksea 写道 f(1)=1,f(2)=1
f(n)=∑(f(k)*f(n-k)) | k=1,n-1 n>2 --------------------------------------- public class Test2 { public long f(int n) { assert(n>0); if(n<=2) return 1; long count=0; for(int i=1;i<n;++i) { count=count+c[i]*c[n-i]; } return count; } long[] c; public static void main(String[] args) { long start=System.currentTimeMillis(); Test2 test = new Test2(); int n=36; test.c=new long[n+1]; for(int i=1;i<=n;++i) { test.c[i]=test.f(i); System.out.println("f("+i+")="+test.c[i]); } System.out.println("use "+(System.currentTimeMillis()-start)+"毫秒"); } } ------------------------------------------ f(1)=1 f(2)=1 f(3)=2 f(4)=5 f(5)=14 f(6)=42 f(7)=132 f(8)=429 f(9)=1430 f(10)=4862 f(11)=16796 f(12)=58786 f(13)=208012 f(14)=742900 f(15)=2674440 f(16)=9694845 f(17)=35357670 f(18)=129644790 f(19)=477638700 f(20)=1767263190 f(21)=6564120420 f(22)=24466267020 f(23)=91482563640 f(24)=343059613650 f(25)=1289904147324 f(26)=4861946401452 f(27)=18367353072152 f(28)=69533550916004 f(29)=263747951750360 f(30)=1002242216651368 f(31)=3814986502092304 f(32)=14544636039226909 f(33)=55534064877048198 f(34)=212336130412243110 f(35)=812944042149730764 f(36)=3116285494907301262 use 16毫秒 以前肯定搞过算法,膜拜一下 |
|
返回顶楼 | |
发表时间:2009-01-16
arksea 写道 f(1)=1,f(2)=1
f(n)=∑(f(k)*f(n-k)) | k=1,n-1 n>2 --------------------------------------- public class Test2 { public long f(int n) { assert(n>0); if(n<=2) return 1; long count=0; for(int i=1;i<n;++i) { count=count+c[i]*c[n-i]; } return count; } long[] c; public static void main(String[] args) { long start=System.currentTimeMillis(); Test2 test = new Test2(); int n=36; test.c=new long[n+1]; for(int i=1;i<=n;++i) { test.c[i]=test.f(i); System.out.println("f("+i+")="+test.c[i]); } System.out.println("use "+(System.currentTimeMillis()-start)+"毫秒"); } } ------------------------------------------ f(1)=1 f(2)=1 f(3)=2 f(4)=5 f(5)=14 f(6)=42 f(7)=132 f(8)=429 f(9)=1430 f(10)=4862 f(11)=16796 f(12)=58786 f(13)=208012 f(14)=742900 f(15)=2674440 f(16)=9694845 f(17)=35357670 f(18)=129644790 f(19)=477638700 f(20)=1767263190 f(21)=6564120420 f(22)=24466267020 f(23)=91482563640 f(24)=343059613650 f(25)=1289904147324 f(26)=4861946401452 f(27)=18367353072152 f(28)=69533550916004 f(29)=263747951750360 f(30)=1002242216651368 f(31)=3814986502092304 f(32)=14544636039226909 f(33)=55534064877048198 f(34)=212336130412243110 f(35)=812944042149730764 f(36)=3116285494907301262 use 16毫秒 跑题。。。 |
|
返回顶楼 | |