Alex and Karyn were at it again. The elementary school sisters were playing their favorite game to decide who gets to play on the computer next.
The rules of the game are quite simple. Given p people (p > 0), one of the p people is chosen to pick a number n (n > p) representing the number of pieces of bubble gum desired. Once this value is chosen, the people are iterated through, one at a time, starting at 1, from “left” to “right”, starting with the person who chose the number. Iterating is done in a circular fashion, meaning that once the person on the far right is reached, the next person in the iteration will be the person on the far left. Upon reaching n, the person at that location is the winner.
Given a list of names, followed by the name of the person choosing the number of pieces of bubble gum, followed by the number that person chose, determine who wins the game.
Input
The first value in the input file will be an integer t (0 < t < 1000) representing the number of test cases in the input file. Following this, on a case by case basis, will be a list of the names of the people (p), on a single line. Names will be no larger than 20 characters in length and all names are unique. There will be no more than 20 names. Each name is followed by a space, save for the last name, which is followed by a newline. On the next line is the name of the person choosing the number of pieces of bubble gum, followed by a newline. The test case is concluded with the number of pieces of gum n (p < n < 1000), which is also followed by a newline.
Output
For each test case, report the name of the person that won the game, followed by a newline.
Sample Input
3
Alex Karyn Maude
Karyn
5
Alex Karyn Maude
Alex
6
Alex Karyn Zach Becca Maude
Zach
8
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str,str1[21];
int n,t,l=0,s;
scanf("%d",&t);
for(int i=0; i<t; i++)
{
l=0;
getchar();
getline(cin,str);
int len=str.length();
s=0;
for(int j=0; j<=len; j++)
{
if(str[j]==' '||str[j]=='\0')
{
str1[l++]=str.substr(s,j-s);
s=j+1;
}
}
// for(int j=0;j<l;j++)
// {
// cout<<str1[j]<<",";
// }
int x;
cin>>str>>n;
for(int j=0;j<l;j++)
{
if(str1[j]==str)
{
x=j;
break;
}
}
n=n+x;
n=n%l-1;
if(n==-1)
cout<<str1[l-1]<<endl;
else
cout<<str1[n]<<endl;
}
return 0;
}
题意:给出t个测试样例,若干个字符串,然后再输入一个字符 串 str 和数字 n , 从给出的那个str开始数,一直数到 n 结束,数到 n 的就输出
Sample Output
Maude
Maude
Maude