知識社群登入
位置: AutoCAD開放式教學 > 討論區 > 討論
市民與 罪犯
1樓
#include <cstdio>
#include <vector>

int f[31];
const int k = 9;
const int n = 30;

std::vector<char> Batman(31, 'T');

void show() {
    for(int i = 1; i <= 30; ++i) {
        putchar(Batman[i]);
    }
    puts("");
}

int main() {
    int pivot = 0;
    for(int i = 1; i <= 15; ++i) {
        for(int j = 0; j < k; ++j) {
            do {
                ++pivot;
                if(pivot > 30) {
                    pivot = 1;
                }
            } while(f[pivot] == 1);
        }
        f[pivot] = 1;
        Batman[pivot] = 'C';
    }
    printf("answer: ");
    show();

    /**
    Simulation Josephus Problem

    **/
    pivot = 0;
    for(int i = 1; i <= 15; ++i) {
        for(int j = 0; j < k; ++j) {
            do {
                ++pivot;
                if(pivot > 30) {
                    pivot = 1;
                }
            } while(Batman[pivot] == ' ');
        }
        printf("round %2d catch %c at %2d, ", i, Batman[pivot], pivot);
        Batman[pivot] = ' ';
        show();
    }
    return 0;
}
2樓
answer: TTTTCCCCCTTCTTTCTCCTTCCCTCCTTC
round  1 catch C at  9, TTTTCCCC TTCTTTCTCCTTCCCTCCTTC
round  2 catch C at 18, TTTTCCCC TTCTTTCT CTTCCCTCCTTC
round  3 catch C at 27, TTTTCCCC TTCTTTCT CTTCCCTC TTC
round  4 catch C at  6, TTTTC CC TTCTTTCT CTTCCCTC TTC
round  5 catch C at 16, TTTTC CC TTCTTT T CTTCCCTC TTC
round  6 catch C at 26, TTTTC CC TTCTTT T CTTCCCT  TTC
round  7 catch C at  7, TTTTC  C TTCTTT T CTTCCCT  TTC
round  8 catch C at 19, TTTTC  C TTCTTT T  TTCCCT  TTC
round  9 catch C at 30, TTTTC  C TTCTTT T  TTCCCT  TT 
round 10 catch C at 12, TTTTC  C TT TTT T  TTCCCT  TT 
round 11 catch C at 24, TTTTC  C TT TTT T  TTCC T  TT 
round 12 catch C at  8, TTTTC    TT TTT T  TTCC T  TT 
round 13 catch C at 22, TTTTC    TT TTT T  TT C T  TT 
round 14 catch C at  5, TTTT     TT TTT T  TT C T  TT 
round 15 catch C at 23, TTTT     TT TTT T  TT   T  TT 

3樓
4樓
city.cpp

附件
1. city.cpp (2 KB)