灯的排列问题

标签: 未分类


设在一排上有N个格子(N≤20),若在格子中放置有不同颜色的灯,每种灯的个数记为N1,N2,……Nk(k表示不同颜色灯的个数)。
  放灯时要遵守下列规则:
    ①同一种颜色的灯不能分开;
    ②不同颜色的灯之间至少要有一个空位置。

每种颜色的灯看成一个整体,剩下 n-m 个空, |—|-|-|,有 n-m+1 条缝放 t 种灯

#include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    int n;
    cin >>n;
    char color;
    int cnt;
    
    int t = 0, total = 0;
    while (cin >>color && color != 'Q') {
        cin >>cnt;
        t++;
        total += cnt;
    }
    int tmp_t = t;
//    cout <<t<<" " <<total<<endl;
    int box = n - total + t - t + 1;
    int sum = 1;
    while (tmp_t--) {
        sum *= box--;
    }
    cout <<sum <<endl;
    
    return 0;
}
原文地址:https://www.cnblogs.com/i-8023-/p/12190835.html