问题描述:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 52628 | Accepted: 20053 |
Description
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
Output
Sample Input
3 4 0
Sample Output
5 30
中文意思:
题意:给出一个k,代表有K个好人与K个坏人,其中前K个是好人,后K个是坏人,根据约瑟夫环游戏的原理,在坏人都出局而好人没有一个出局的情况下,m最小是多大
思路:由于k的数值小,直接暴力枚举打表
c++代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int min, countx = 0, a[14], *T = new int[100], P[14] = { 0 };//T表示输入的各个数字
memset(a,0,sizeof(int)*14);//memset函数用于集体赋值,a表示数组的起始地址,0表示赋的值,sizeof(int)表示int的尺寸*数组的大小
for (int i = 1; i < 14; i++){
memset(a, 0, sizeof(int)* 14);
min = 1;
for (int j = 1; j <= i; j++){
a[j] = (a[j - 1] + min - 1) % (2 * i - j + 1);//a[j-1]+min-1表示上一局过后的起始位置开始数min个数到现在的位置,
//2*i-j+1表示到现在这一轮总共的人数,因为有可能超出所以要取余数
if (a[j] < i){
j = 0;
min++;
}
}
P[i] = min;
}
while (cin >> T[countx] && T[countx] != 0)countx++;
countx = 0;
while (T[countx++] != 0)
cout <<P[T[countx - 1]] << endl;
delete []T;
return 0;
}