这是金山的一道笔试题,求高手指导。。。。
200个苹果放到10个相同的篮子,每个篮子至少有一个苹果,一共有多少放法?
------解决方案--------------------------------------------------------
http://topic.csdn.net/u/20110409/00/1ba9ca82-dd9f-433e-94bf-13f249384cfe?77924
------解决方案--------------------------------------------------------
for example
没做优化处理
- Java code
import java.util.*;public class Test { public static void main(String[] args) throws Throwable { int n = 200; int[] blank = new int[10]; Arrays.fill(blank, 1); blank[blank.length-1] += (200-blank.length); int sum = 0; long cnt = 1; while (true) { sum = 0; for (int i=0; i<blank.length; i++) { sum += blank[i]; } if (sum == 200) { System.out.printf("method-%d: %s\r\n", cnt++, Arrays.toString(blank)); } blank[blank.length-1]++; for (int i=blank.length-1; i>0; i--) { if (blank[i] > 200-blank.length+1) { blank[i] = 1; blank[i-1]++; } else { break; } } if (blank[0] == 200-blank.length+1) { System.out.printf("method-%d: %s\r\n", cnt++, Arrays.toString(blank)); break; } } }}