码迷,mamicode.com
首页 > Windows程序 > 详细

Who Will Win?

时间:2017-10-25 21:39:28      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:nes   col   ace   however   res   which   ops   iostream   arc   

Gautam and Subhash are two brothers. They are similar to each other in all respects except one. They have different ways of searching. When Gautam has to search for an item from a lot, he goes through all the items one by one and stops when he finds the item. However Subhash has an entirely different and interesting way of searching. However the search works only for items which are sorted. He goes to the middle of the lot, if he finds the desired item, he stops, if not, he checks whether the middle item is smaller or larger than the required item. If larger, he repeats the same process for the first half of the lot, otherwise second half. One day, the two brothers sit in a contest in which they have to find a name out of a sorted dictionary. Whoever finds out the name first will win the contest. The audience is very eager to know who will win and hence they want you to predict.

Input:

The first line of input takes the number of test cases, T.

The next T lines take the total number of names N, followed by M the position of the name to be searched, as well as the amount of time taken for each observation by Gautam G and Subhash S.

Output:

Print 1 in case Gautam will win, 2 in case Subhash will win, 0 in case of a draw in a separate line.

Constraints:

1<=T<=100

1<=N<=1000

1<=M<=N

1<=G<=100

1<=S<=100

Example:

Input:

3
10 8 10 25
10 8 10 30
10 8 10 12

Output:

2
2
2
下面是我的代码实现,C++
#include <iostream>
using namespace std;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int num,index,g,s,gtime,stime;

        cin>>num;
        cin>>index;
        cin>>g;
        cin>>s;

        int l=1,r=num,mid,sci=0;
        gtime=index*g;

        while(l<=r)
        {
            mid=(l+r)>>1;
            if(mid==index)
            {
                sci++;
                break;
            }
            else if(mid<index)
            {
                sci++;
                l=mid+1;
            }
            else
            {
                sci++;
                r=mid-1;
            }
        }
        stime=sci*s;
        if(stime>gtime)
            cout<<1<<endl;
        else
            cout<<2<<endl;

    }
    return 0;
}

如有问题,请留言

Who Will Win?

标签:nes   col   ace   however   res   which   ops   iostream   arc   

原文地址:http://www.cnblogs.com/wongyi/p/7731939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!