UVa10474——Where is the Marble?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/westbrook1998/article/details/80316180

Raju and Meena love to play with Marbles. They have got a lot of
marbles with numbers written on them. At the beginning, Raju would
place the marbles one after another in ascending order of the numbers
written on them. Then Meena would ask Raju to find the first marble
with a certain number. She would count 1…2…3. Raju gets one point
for correct answer, and Meena gets the point if Raju fails. After some
fixed number of trials the game ends and the player with maximum
points wins. Today it’s your chance to play as Raju. Being the smart
kid, you’d be taking the favor of a computer. But don’t underestimate
Meena, she had written a program to keep track how much time you’re
taking to give all the answers. So now you have to write a program,
which will help you in your role as Raju.
Input
There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins
with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next
N lines would contain the numbers written on the N marbles. These marble numbers will not come
in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers
are greater than 10000 and none of them are negative.
Input is terminated by a test case where N = 0 and Q = 0.
Output
For each test case output the serial number of the case.
For each of the queries, print one line of output. The format of this line will depend upon whether
or not the query number is written upon any of the marbles. The two different formats are described
below:
• ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered
1, 2, … , N.
• ‘x not found’, if the marble with number x is not present.
Look at the output for sample input for details.
Sample Input
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
Sample Output
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3

紫书上的练习题,英语终究是个硬伤,最后还是百度加看样例才看懂的,很水的一道题,主要是熟悉sort()lower_bound()的操作

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=10050;
int a[MAXN];
int n,q,t;
int main(void){
    int c=1;
    while(~scanf("%d%d",&n,&q)){
        if(n==0 && q==0){
            break;
        }
        memset(a,0,sizeof(n));
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        for(int i=0;i<q;i++){
            scanf("%d",&t);
            if(i==0){
                printf("CASE# %d:\n",c++);
            }
            int f=lower_bound(a,a+n,t)-a;
            if(f>=0 && f<n && a[f]==t){
                printf("%d found at %d\n",t,f+1);
            }
            else{
                printf("%d not found\n",t);
            }
        }
    }
    return 0;
}
阅读更多

Find the Marble

08-29

Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of the pots. After that, Bob cannot see the inside of the pots. Then Alice makes a sequence of swappings and Bob guesses which pot the marble is in. In each of the swapping, Alice chooses two different pots and swaps their positions.nnUnfortunately, Alice's actions are very fast, so Bob can only catch k of m swappings and regard these k swappings as all actions Alice has performed. Now given the initial pot the marble is in, and the sequence of swappings, you are asked to calculate which pot Bob most possibly guesses. You can assume that Bob missed any of the swappings with equal possibility.nnInputnnThere are several test cases in the input file. The first line of the input file contains an integer N (N ≈ 100), then N cases follow.nnThe first line of each test case contains 4 integers n, m, k and s(0 < s ≤ n ≤ 50, 0 ≤ k ≤ m ≤ 50), which are the number of pots, the number of swappings Alice makes, the number of swappings Bob catches and index of the initial pot the marble is in. Pots are indexed from 1 to n. Then m lines follow, each of which contains two integers ai and bi (1 ≤ ai, bi ≤ n), telling the two pots Alice swaps in the i-th swapping.nnOutoutnnFor each test case, output the pot that Bob most possibly guesses. If there is a tie, output the smallest one.nnSample Inputnn3n3 1 1 1n1 2n3 1 0 1n1 2n3 3 2 2n2 3n3 2n1 2nSample Outputnn2n1n3

Bridged Marble Rings

10-24

问题描述 :nn26 marbles―half yellow and half gray―are distributed between two circles of 13 marbles each. The marbles in each circle can be freely rotated clockwise or counterclockwise. The upper and lower circles are bridged by a smaller circle, which rotates―in the plane of the board―180 degrees, effectively exchanging the three bottommost marbles of the upper circle with the three uppermost marbles of the lower one. The goal is to get all gray marbles to the upper circle and all yellow marbles to the lower one while minimizing the number of times the bridging circle is rotated.nn输入:nnThe input is a series of lines, where each line describes an initial board configuration. Each line is a permutation of 13 y’s and 13 g’s. The first half of the line describes the clockwise configuration of the upper circle, and the rest of the line describes the clockwise configuration of the lower one. Of course, each y corresponds to a yellow marble, and each g corresponds to a gray one.nThe input file will include multiple test cases. Each test case consists of a single line containing some permutation of the string y13g13. All lines (including the last one) are terminated with a newline. The newline immediately follows the last letter on the line.nn输出:nnThe input is a series of lines, where each line describes an initial board configuration. Each line is a permutation of 13 y’s and 13 g’s. The first half of the line describes the clockwise configuration of the upper circle, and the rest of the line describes the clockwise configuration of the lower one. Of course, each y corresponds to a yellow marble, and each g corresponds to a gray one.nThe input file will include multiple test cases. Each test case consists of a single line containing some permutation of the string y13g13. All lines (including the last one) are terminated with a newline. The newline immediately follows the last letter on the line.nn样例输入:nngggggggggggggyyyyyyyyyyyyynyyyyyggggggggyyyygggggyyyyngyyygyggyyygyyggyyggggyyggnygygygygygygygygygygygygygn样例输出:nn0n2n5n6

没有更多推荐了,返回首页