ccf报数游戏java_ccf 201712 02 (游戏)
鄙人不才,此中鄙陋甚多,望海涵!!!
這里我們來詳細講解一下圓桌問題的相關題目
第一道一定是hdu的4841了,罪惡的源頭
圓桌上圍坐著2n個人。其中n個人是好人,另外n個人是壞人。如果從第一個人開始數數,數到第m個人,則立即處死該人;然后從被處死的人之后開始數數,再將數到的第m個人處死……依此方法不斷處死圍坐在圓桌上的人。試問預先應如何安排這些好人與壞人的座位,能使得在處死n個人之后,圓桌上圍坐的剩余的n個人全是好人。
輸入:多組數據,每組數據輸入:好人和壞人的人數n(<=32767)、步長m(<=32767);
輸出:對于每一組數據,輸出2n個大寫字母,‘G’表示好人,‘B’表示壞人,50個字母為一行,不允許出現空白字符。相鄰數據間留有一空行。
輸入
2 3
2 4
輸出
GBBG
BGGB
C++代碼
#include
#include
using namespace std;
vector res;
int main()
{
int n,m;
while(cin>>n>>m)
{
res.clear();//用res來模擬圓桌
for(int i=0;i<2*n;i++) res.push_back(i);//先將這2n個人放入res中;
int p=0;
for(int i=0;i
{
p=(p+m-1)%res.size();//每次先定位該死的壞人的位置,注意res下標從0開始,而且
//涉及到取余問題,記得減一
res.erase(res.begin()+p);//將壞人抹殺
}
for(int i=0,cnt=0;i<2*n;i++)
{
if(i%50==0 && i) puts("");//每50行輸出一個換行
if(cnt
{//別忘了cnt
printf("G");
cnt++;
}
else printf("B");
}
cout<
}
return 0;
}
圓桌問題的簡單應用
題目
“Eeny meeny miny moe” is a well-known nursery rhyme inEnglish, used (among other things) by kids to “randomly”select members of a team. It exists in many variations, oneof which goes like this:
Eeny, meeny, miny, moe,
Catch a tiger by the toe.
If he hollers, let him go,
Eeny, meeny, miny, moe.
Similar verses exist in most languages, such as “Ulle dulle dof” in Finnish, “Akka bakka bonka rakka” in Norwegian, and “Ole dole doff” in Swedish.
Two teams are to be selected for a game and the rhyme is used to select one kid for a team at a time, alternating between the two teams, until all kids have been selected. The kids are standing in a circle. In each selection round we start counting the kids in clockwise order around the circle, skipping one kid for every word in the rhyme, until the last word. The kid matching the last word is chosen for the current team and then the next round starts. In all rounds but the first, the counting starts at the next remaining kid (in clockwise order) after the one that was selected in the previous round.
Given such a rhyme, and a group of kids, can you tell which kids will be in which team?
Illustration of the first three rounds of Sample Input 1. In rounds 1 and 3, Alvar and Rakel get selected for the first team, and in round 2, Lisa is selected for the second team. In round 4 (not shown), only Kalle remains and is selected for the second team.
Input
The first line of input contains the rhyme, consisting of a list of words separated by spaces. The second line of input contains an integer n (1 ≤ n ≤ 100), the number of kids. Then follow the names of the kids, one per line. The kids are given in clockwise order and the first kid listed is the one at which counting starts in the first round.
All words and names consist only of upper and lower case letters ‘A’-‘Z’ and ‘a’-‘z’. No input line is empty or longer than 100 characters (excluding the newline character at the end of the line).
Output
Output the two teams, starting with the one whose first member is chosen first. For each team, output the number of kids in the team, followed by the names of the kids in the team, in the same order as they were chosen for the team.
樣例1輸入
eeny meeny miny
4
Kalle
Lisa
Alvar Rakel
樣例1輸出
2
Alvar
Rakel
2
Lisa
Kalle
樣例2輸入
Every Other
3
a
b
c
樣例2輸出
2
b
c
1
a
C++code
#include
#include
using namespace std;
vector a;
vector b;
vector sum;
string s;
int main()
{
int cnt=0,n=0;
while(cin>>s)
{
if(s[0]>='a' && s[0]<='z' || s[0]>='A' && s[0]<='Z') cnt++;
else
{
for(int i=0;i
break;
}
}
for(int i=0;i
{
string item;
cin>>item;
sum.push_back(item);
}
int m=1;
while(sum.size())
{
int pos=(pos+cnt-1)%sum.size();
if(m&1)
{
a.push_back(sum[pos]);
sum.erase(sum.begin()+pos);
}
else
{
b.push_back(sum[pos]);
sum.erase(sum.begin()+pos);
}
m++;
}
cout<< a.size() <
for(auto x:a) cout<< x <
cout<< b.size() <
for(auto x:b) cout<< x <
return 0;
}
總算是回到了游戲這題,這題其實就是屬于圓桌問題的變種,關鍵就在于把報數的過程轉化為每隔幾個人就會被淘汰的問題,這就相當于n個人只有一個是好人,從第一個人開始每隔幾個人就會抹殺一個壞人,最終剩下一個好人勝出
關鍵:處理這個隔幾個人的問題
題目請到ccf官網自提
C++ Code
#include
#include
using namespace std;
vector res;//用res來模擬圓桌
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++) res.push_back(i);
for(int i=0,cnt=1,m=0;i
{
if(cnt%k==0 || cnt%10==k)
{
int p=(p+m)%res.size();
res.erase(res.begin()+p);
m=0,i++;//將m歸0,并且淘汰人數加1
}
else m++;//這里m就是距離上一次淘汰所間隔人數
cnt++;//cnt表示所報的數
}
cout<< res[0] <
return 0;
}
持續更新中。。。。
總結
以上是生活随笔為你收集整理的ccf报数游戏java_ccf 201712 02 (游戏)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS 13 教程:如何从 iPhone
- 下一篇: iPhone手机接口“小洞洞”里的灰怎么