【Java/算法/组合】将20把相同的椅子分别放入编号为1,2,3,4的四个会议室内,要求每个会议室中放入的椅子数不小于它的编号数,问有多少种不同的放法?
【数学解法】
先将0张椅子放入1号房间,1张椅子放入2号房间,2张椅子放入3号房间,3张椅子放入4号房间,这以后再放椅子就不存在违背题设的情况了。
剩下十四张椅子分四份,相当于3个隔板去插十四张椅子中的13个空,那么方案是C13_3=13*12*11/321=13*11*2=286
【代码解法】
这个程序实际只需要找到和为14的四个自然数,完成此任务只需四重循环即可,不需要Combination组合器的参与。
得到这四个数后,加上0,1,2,3输出即可。
代码:
public class RoomChair { public static void main(String[] args){ int idx=0; for(int i=1;i<=13;i++){ for(int j=1;j<=13;j++){ for(int k=1;k<=13;k++){ for(int m=1;m<=13;m++) { if (i + j + k+m == 14) { System.out.println(String.format("%03d", ++idx) + ".Room1:" + (i+0) + " Room2:" + (j+1) + " Room3:"+ (k+2)+" Room4:" + (m+3)); } } } } } } }
【输出】
001.Room1:1 Room2:2 Room3:3 Room4:14 002.Room1:1 Room2:2 Room3:4 Room4:13 003.Room1:1 Room2:2 Room3:5 Room4:12 004.Room1:1 Room2:2 Room3:6 Room4:11 005.Room1:1 Room2:2 Room3:7 Room4:10 006.Room1:1 Room2:2 Room3:8 Room4:9 007.Room1:1 Room2:2 Room3:9 Room4:8 008.Room1:1 Room2:2 Room3:10 Room4:7 009.Room1:1 Room2:2 Room3:11 Room4:6 010.Room1:1 Room2:2 Room3:12 Room4:5 011.Room1:1 Room2:2 Room3:13 Room4:4 012.Room1:1 Room2:3 Room3:3 Room4:13 013.Room1:1 Room2:3 Room3:4 Room4:12 014.Room1:1 Room2:3 Room3:5 Room4:11 015.Room1:1 Room2:3 Room3:6 Room4:10 016.Room1:1 Room2:3 Room3:7 Room4:9 017.Room1:1 Room2:3 Room3:8 Room4:8 018.Room1:1 Room2:3 Room3:9 Room4:7 019.Room1:1 Room2:3 Room3:10 Room4:6 020.Room1:1 Room2:3 Room3:11 Room4:5 021.Room1:1 Room2:3 Room3:12 Room4:4 022.Room1:1 Room2:4 Room3:3 Room4:12 023.Room1:1 Room2:4 Room3:4 Room4:11 024.Room1:1 Room2:4 Room3:5 Room4:10 025.Room1:1 Room2:4 Room3:6 Room4:9 026.Room1:1 Room2:4 Room3:7 Room4:8 027.Room1:1 Room2:4 Room3:8 Room4:7 028.Room1:1 Room2:4 Room3:9 Room4:6 029.Room1:1 Room2:4 Room3:10 Room4:5 030.Room1:1 Room2:4 Room3:11 Room4:4 031.Room1:1 Room2:5 Room3:3 Room4:11 032.Room1:1 Room2:5 Room3:4 Room4:10 033.Room1:1 Room2:5 Room3:5 Room4:9 034.Room1:1 Room2:5 Room3:6 Room4:8 035.Room1:1 Room2:5 Room3:7 Room4:7 036.Room1:1 Room2:5 Room3:8 Room4:6 037.Room1:1 Room2:5 Room3:9 Room4:5 038.Room1:1 Room2:5 Room3:10 Room4:4 039.Room1:1 Room2:6 Room3:3 Room4:10 040.Room1:1 Room2:6 Room3:4 Room4:9 041.Room1:1 Room2:6 Room3:5 Room4:8 042.Room1:1 Room2:6 Room3:6 Room4:7 043.Room1:1 Room2:6 Room3:7 Room4:6 044.Room1:1 Room2:6 Room3:8 Room4:5 045.Room1:1 Room2:6 Room3:9 Room4:4 046.Room1:1 Room2:7 Room3:3 Room4:9 047.Room1:1 Room2:7 Room3:4 Room4:8 048.Room1:1 Room2:7 Room3:5 Room4:7 049.Room1:1 Room2:7 Room3:6 Room4:6 050.Room1:1 Room2:7 Room3:7 Room4:5 051.Room1:1 Room2:7 Room3:8 Room4:4 052.Room1:1 Room2:8 Room3:3 Room4:8 053.Room1:1 Room2:8 Room3:4 Room4:7 054.Room1:1 Room2:8 Room3:5 Room4:6 055.Room1:1 Room2:8 Room3:6 Room4:5 056.Room1:1 Room2:8 Room3:7 Room4:4 057.Room1:1 Room2:9 Room3:3 Room4:7 058.Room1:1 Room2:9 Room3:4 Room4:6 059.Room1:1 Room2:9 Room3:5 Room4:5 060.Room1:1 Room2:9 Room3:6 Room4:4 061.Room1:1 Room2:10 Room3:3 Room4:6 062.Room1:1 Room2:10 Room3:4 Room4:5 063.Room1:1 Room2:10 Room3:5 Room4:4 064.Room1:1 Room2:11 Room3:3 Room4:5 065.Room1:1 Room2:11 Room3:4 Room4:4 066.Room1:1 Room2:12 Room3:3 Room4:4 067.Room1:2 Room2:2 Room3:3 Room4:13 068.Room1:2 Room2:2 Room3:4 Room4:12 069.Room1:2 Room2:2 Room3:5 Room4:11 070.Room1:2 Room2:2 Room3:6 Room4:10 071.Room1:2 Room2:2 Room3:7 Room4:9 072.Room1:2 Room2:2 Room3:8 Room4:8 073.Room1:2 Room2:2 Room3:9 Room4:7 074.Room1:2 Room2:2 Room3:10 Room4:6 075.Room1:2 Room2:2 Room3:11 Room4:5 076.Room1:2 Room2:2 Room3:12 Room4:4 077.Room1:2 Room2:3 Room3:3 Room4:12 078.Room1:2 Room2:3 Room3:4 Room4:11 079.Room1:2 Room2:3 Room3:5 Room4:10 080.Room1:2 Room2:3 Room3:6 Room4:9 081.Room1:2 Room2:3 Room3:7 Room4:8 082.Room1:2 Room2:3 Room3:8 Room4:7 083.Room1:2 Room2:3 Room3:9 Room4:6 084.Room1:2 Room2:3 Room3:10 Room4:5 085.Room1:2 Room2:3 Room3:11 Room4:4 086.Room1:2 Room2:4 Room3:3 Room4:11 087.Room1:2 Room2:4 Room3:4 Room4:10 088.Room1:2 Room2:4 Room3:5 Room4:9 089.Room1:2 Room2:4 Room3:6 Room4:8 090.Room1:2 Room2:4 Room3:7 Room4:7 091.Room1:2 Room2:4 Room3:8 Room4:6 092.Room1:2 Room2:4 Room3:9 Room4:5 093.Room1:2 Room2:4 Room3:10 Room4:4 094.Room1:2 Room2:5 Room3:3 Room4:10 095.Room1:2 Room2:5 Room3:4 Room4:9 096.Room1:2 Room2:5 Room3:5 Room4:8 097.Room1:2 Room2:5 Room3:6 Room4:7 098.Room1:2 Room2:5 Room3:7 Room4:6 099.Room1:2 Room2:5 Room3:8 Room4:5 100.Room1:2 Room2:5 Room3:9 Room4:4 101.Room1:2 Room2:6 Room3:3 Room4:9 102.Room1:2 Room2:6 Room3:4 Room4:8 103.Room1:2 Room2:6 Room3:5 Room4:7 104.Room1:2 Room2:6 Room3:6 Room4:6 105.Room1:2 Room2:6 Room3:7 Room4:5 106.Room1:2 Room2:6 Room3:8 Room4:4 107.Room1:2 Room2:7 Room3:3 Room4:8 108.Room1:2 Room2:7 Room3:4 Room4:7 109.Room1:2 Room2:7 Room3:5 Room4:6 110.Room1:2 Room2:7 Room3:6 Room4:5 111.Room1:2 Room2:7 Room3:7 Room4:4 112.Room1:2 Room2:8 Room3:3 Room4:7 113.Room1:2 Room2:8 Room3:4 Room4:6 114.Room1:2 Room2:8 Room3:5 Room4:5 115.Room1:2 Room2:8 Room3:6 Room4:4 116.Room1:2 Room2:9 Room3:3 Room4:6 117.Room1:2 Room2:9 Room3:4 Room4:5 118.Room1:2 Room2:9 Room3:5 Room4:4 119.Room1:2 Room2:10 Room3:3 Room4:5 120.Room1:2 Room2:10 Room3:4 Room4:4 121.Room1:2 Room2:11 Room3:3 Room4:4 122.Room1:3 Room2:2 Room3:3 Room4:12 123.Room1:3 Room2:2 Room3:4 Room4:11 124.Room1:3 Room2:2 Room3:5 Room4:10 125.Room1:3 Room2:2 Room3:6 Room4:9 126.Room1:3 Room2:2 Room3:7 Room4:8 127.Room1:3 Room2:2 Room3:8 Room4:7 128.Room1:3 Room2:2 Room3:9 Room4:6 129.Room1:3 Room2:2 Room3:10 Room4:5 130.Room1:3 Room2:2 Room3:11 Room4:4 131.Room1:3 Room2:3 Room3:3 Room4:11 132.Room1:3 Room2:3 Room3:4 Room4:10 133.Room1:3 Room2:3 Room3:5 Room4:9 134.Room1:3 Room2:3 Room3:6 Room4:8 135.Room1:3 Room2:3 Room3:7 Room4:7 136.Room1:3 Room2:3 Room3:8 Room4:6 137.Room1:3 Room2:3 Room3:9 Room4:5 138.Room1:3 Room2:3 Room3:10 Room4:4 139.Room1:3 Room2:4 Room3:3 Room4:10 140.Room1:3 Room2:4 Room3:4 Room4:9 141.Room1:3 Room2:4 Room3:5 Room4:8 142.Room1:3 Room2:4 Room3:6 Room4:7 143.Room1:3 Room2:4 Room3:7 Room4:6 144.Room1:3 Room2:4 Room3:8 Room4:5 145.Room1:3 Room2:4 Room3:9 Room4:4 146.Room1:3 Room2:5 Room3:3 Room4:9 147.Room1:3 Room2:5 Room3:4 Room4:8 148.Room1:3 Room2:5 Room3:5 Room4:7 149.Room1:3 Room2:5 Room3:6 Room4:6 150.Room1:3 Room2:5 Room3:7 Room4:5 151.Room1:3 Room2:5 Room3:8 Room4:4 152.Room1:3 Room2:6 Room3:3 Room4:8 153.Room1:3 Room2:6 Room3:4 Room4:7 154.Room1:3 Room2:6 Room3:5 Room4:6 155.Room1:3 Room2:6 Room3:6 Room4:5 156.Room1:3 Room2:6 Room3:7 Room4:4 157.Room1:3 Room2:7 Room3:3 Room4:7 158.Room1:3 Room2:7 Room3:4 Room4:6 159.Room1:3 Room2:7 Room3:5 Room4:5 160.Room1:3 Room2:7 Room3:6 Room4:4 161.Room1:3 Room2:8 Room3:3 Room4:6 162.Room1:3 Room2:8 Room3:4 Room4:5 163.Room1:3 Room2:8 Room3:5 Room4:4 164.Room1:3 Room2:9 Room3:3 Room4:5 165.Room1:3 Room2:9 Room3:4 Room4:4 166.Room1:3 Room2:10 Room3:3 Room4:4 167.Room1:4 Room2:2 Room3:3 Room4:11 168.Room1:4 Room2:2 Room3:4 Room4:10 169.Room1:4 Room2:2 Room3:5 Room4:9 170.Room1:4 Room2:2 Room3:6 Room4:8 171.Room1:4 Room2:2 Room3:7 Room4:7 172.Room1:4 Room2:2 Room3:8 Room4:6 173.Room1:4 Room2:2 Room3:9 Room4:5 174.Room1:4 Room2:2 Room3:10 Room4:4 175.Room1:4 Room2:3 Room3:3 Room4:10 176.Room1:4 Room2:3 Room3:4 Room4:9 177.Room1:4 Room2:3 Room3:5 Room4:8 178.Room1:4 Room2:3 Room3:6 Room4:7 179.Room1:4 Room2:3 Room3:7 Room4:6 180.Room1:4 Room2:3 Room3:8 Room4:5 181.Room1:4 Room2:3 Room3:9 Room4:4 182.Room1:4 Room2:4 Room3:3 Room4:9 183.Room1:4 Room2:4 Room3:4 Room4:8 184.Room1:4 Room2:4 Room3:5 Room4:7 185.Room1:4 Room2:4 Room3:6 Room4:6 186.Room1:4 Room2:4 Room3:7 Room4:5 187.Room1:4 Room2:4 Room3:8 Room4:4 188.Room1:4 Room2:5 Room3:3 Room4:8 189.Room1:4 Room2:5 Room3:4 Room4:7 190.Room1:4 Room2:5 Room3:5 Room4:6 191.Room1:4 Room2:5 Room3:6 Room4:5 192.Room1:4 Room2:5 Room3:7 Room4:4 193.Room1:4 Room2:6 Room3:3 Room4:7 194.Room1:4 Room2:6 Room3:4 Room4:6 195.Room1:4 Room2:6 Room3:5 Room4:5 196.Room1:4 Room2:6 Room3:6 Room4:4 197.Room1:4 Room2:7 Room3:3 Room4:6 198.Room1:4 Room2:7 Room3:4 Room4:5 199.Room1:4 Room2:7 Room3:5 Room4:4 200.Room1:4 Room2:8 Room3:3 Room4:5 201.Room1:4 Room2:8 Room3:4 Room4:4 202.Room1:4 Room2:9 Room3:3 Room4:4 203.Room1:5 Room2:2 Room3:3 Room4:10 204.Room1:5 Room2:2 Room3:4 Room4:9 205.Room1:5 Room2:2 Room3:5 Room4:8 206.Room1:5 Room2:2 Room3:6 Room4:7 207.Room1:5 Room2:2 Room3:7 Room4:6 208.Room1:5 Room2:2 Room3:8 Room4:5 209.Room1:5 Room2:2 Room3:9 Room4:4 210.Room1:5 Room2:3 Room3:3 Room4:9 211.Room1:5 Room2:3 Room3:4 Room4:8 212.Room1:5 Room2:3 Room3:5 Room4:7 213.Room1:5 Room2:3 Room3:6 Room4:6 214.Room1:5 Room2:3 Room3:7 Room4:5 215.Room1:5 Room2:3 Room3:8 Room4:4 216.Room1:5 Room2:4 Room3:3 Room4:8 217.Room1:5 Room2:4 Room3:4 Room4:7 218.Room1:5 Room2:4 Room3:5 Room4:6 219.Room1:5 Room2:4 Room3:6 Room4:5 220.Room1:5 Room2:4 Room3:7 Room4:4 221.Room1:5 Room2:5 Room3:3 Room4:7 222.Room1:5 Room2:5 Room3:4 Room4:6 223.Room1:5 Room2:5 Room3:5 Room4:5 224.Room1:5 Room2:5 Room3:6 Room4:4 225.Room1:5 Room2:6 Room3:3 Room4:6 226.Room1:5 Room2:6 Room3:4 Room4:5 227.Room1:5 Room2:6 Room3:5 Room4:4 228.Room1:5 Room2:7 Room3:3 Room4:5 229.Room1:5 Room2:7 Room3:4 Room4:4 230.Room1:5 Room2:8 Room3:3 Room4:4 231.Room1:6 Room2:2 Room3:3 Room4:9 232.Room1:6 Room2:2 Room3:4 Room4:8 233.Room1:6 Room2:2 Room3:5 Room4:7 234.Room1:6 Room2:2 Room3:6 Room4:6 235.Room1:6 Room2:2 Room3:7 Room4:5 236.Room1:6 Room2:2 Room3:8 Room4:4 237.Room1:6 Room2:3 Room3:3 Room4:8 238.Room1:6 Room2:3 Room3:4 Room4:7 239.Room1:6 Room2:3 Room3:5 Room4:6 240.Room1:6 Room2:3 Room3:6 Room4:5 241.Room1:6 Room2:3 Room3:7 Room4:4 242.Room1:6 Room2:4 Room3:3 Room4:7 243.Room1:6 Room2:4 Room3:4 Room4:6 244.Room1:6 Room2:4 Room3:5 Room4:5 245.Room1:6 Room2:4 Room3:6 Room4:4 246.Room1:6 Room2:5 Room3:3 Room4:6 247.Room1:6 Room2:5 Room3:4 Room4:5 248.Room1:6 Room2:5 Room3:5 Room4:4 249.Room1:6 Room2:6 Room3:3 Room4:5 250.Room1:6 Room2:6 Room3:4 Room4:4 251.Room1:6 Room2:7 Room3:3 Room4:4 252.Room1:7 Room2:2 Room3:3 Room4:8 253.Room1:7 Room2:2 Room3:4 Room4:7 254.Room1:7 Room2:2 Room3:5 Room4:6 255.Room1:7 Room2:2 Room3:6 Room4:5 256.Room1:7 Room2:2 Room3:7 Room4:4 257.Room1:7 Room2:3 Room3:3 Room4:7 258.Room1:7 Room2:3 Room3:4 Room4:6 259.Room1:7 Room2:3 Room3:5 Room4:5 260.Room1:7 Room2:3 Room3:6 Room4:4 261.Room1:7 Room2:4 Room3:3 Room4:6 262.Room1:7 Room2:4 Room3:4 Room4:5 263.Room1:7 Room2:4 Room3:5 Room4:4 264.Room1:7 Room2:5 Room3:3 Room4:5 265.Room1:7 Room2:5 Room3:4 Room4:4 266.Room1:7 Room2:6 Room3:3 Room4:4 267.Room1:8 Room2:2 Room3:3 Room4:7 268.Room1:8 Room2:2 Room3:4 Room4:6 269.Room1:8 Room2:2 Room3:5 Room4:5 270.Room1:8 Room2:2 Room3:6 Room4:4 271.Room1:8 Room2:3 Room3:3 Room4:6 272.Room1:8 Room2:3 Room3:4 Room4:5 273.Room1:8 Room2:3 Room3:5 Room4:4 274.Room1:8 Room2:4 Room3:3 Room4:5 275.Room1:8 Room2:4 Room3:4 Room4:4 276.Room1:8 Room2:5 Room3:3 Room4:4 277.Room1:9 Room2:2 Room3:3 Room4:6 278.Room1:9 Room2:2 Room3:4 Room4:5 279.Room1:9 Room2:2 Room3:5 Room4:4 280.Room1:9 Room2:3 Room3:3 Room4:5 281.Room1:9 Room2:3 Room3:4 Room4:4 282.Room1:9 Room2:4 Room3:3 Room4:4 283.Room1:10 Room2:2 Room3:3 Room4:5 284.Room1:10 Room2:2 Room3:4 Room4:4 285.Room1:10 Room2:3 Room3:3 Room4:4 286.Room1:11 Room2:2 Room3:3 Room4:4
END