zoukankan      html  css  js  c++  java
  • 1012-Joseph 约瑟夫问题

    问题描述:

    Joseph
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 52628   Accepted: 20053

    Description

    The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved. 

    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

    The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

    Output

    The output file will consist of separate lines containing m corresponding to k in the input file.

    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;
    }

  • 相关阅读:
    Eclipse汉化后怎么改回英文版 (中文 改 英文)
    解决android中Layout文件下的xml文件配好后,R类中不能自动生成相应代码
    Android SDK离线安装
    Windows环境下Android Studio v1.0安装教程
    Eclipse调试Bug的七种常用技巧
    博客开通了
    Android常见的按钮监听器实现方式
    用setTimeout实现在DOM上(通常是菜单栏)鼠标停留一段时间才执行相应的操作
    Javascript模块模式学习分享
    Oracle数据库逻辑存储结构管理
  • 原文地址:https://www.cnblogs.com/1996313xjf/p/5784942.html
Copyright © 2011-2022 走看看