Problem 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.
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.
题意:
输入一个k, 前k个 是好人,前k 个是好人, 后k个是坏人,找出一个m, 从自身开始数,第 m 个数 筛出去,一直这样循环,找出 符合m的条件(筛出去的都是坏人,好人只有在坏人都出去之后才可以被晒出去,找出符合这样条件最小的m)
#if 1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int f[30],a[30];
for(int i=1; i<14; i++)
{
int min=1;
int n=2*i;
memset(a,0,sizeof(a));
for(int j=1; j<=i; j++)
{
a[j]=(a[j-1]+min-1)%(n-j+1);//);//(a[j-1]+MIN-1)计算的是从现在这个人的位置开始数能到达的人的位置,
// (n-j+1)是出局人后一个人的位置,因为要由他开始数,得到的是下个出局的人的位置,注意这个位置是从0开始的
if(a[j]<i)
{
j=0;
min++; //好人出局了,j重置,min增加进行枚举
}
}
f[i]=min;
}
int x;
while(cin>>x && x)
cout<<f[x]<<endl;
return 0;
}
#endif
环的遍历:
/ 2.2 2 绕环问题, 给你一个n , m .
题意就是说 :输入一个n 这个n 个人, 还有m,从1开始 每隔m-1个人开始遍历,看看能不能全部遍历。
题意就是说 :输入一个n 这个n 个人, 还有m,从1开始 每隔m-1个人开始遍历,看看能不能全部遍历。
例如 3 2 就可以 。
潜在的 规律 要是 m and n 没有 除了1以外 的公约数 就可以了。
潜在的 规律 要是 m and n 没有 除了1以外 的公约数 就可以了。
#if 0
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
if(a<b)
{
a=a^b;
b=a^b;
a=a^b;
}
while(b)
{
int x=a%b;
a=b;
b=x;
gcd(a,b);
}
return a;
}
int main()
{
int n , m ,x;
cin>>x;
while(x--)
{
int t;
cin>>m>>n;
if(gcd(m,n)==1) cout << "NO" <<endl;
else cout <<"YES"<<endl;
}
}
#endif

本文探讨了一个基于约瑟夫问题的变形题目,要求在特定条件下找出最小的步长m,使得所有坏人在第一个好人被淘汰之前全部被淘汰。通过枚举和循环遍历的方法给出了具体的算法实现。
1488

被折叠的 条评论
为什么被折叠?



