Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1547
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1547
Guess What
By Neilor Tonin, URI Brazil
Timelimit: 1
Professor Genesio received several shirts from OBI (Organization of Bacharels and Intellectuals) to distribute to his students of Computer Science. To distribute these shirts he organized the students of each class in small groups (at maximum of 10) and defined that would make the draw of a shirt to each group. Since Genesio don't want to spend much time with this task, he asked you to help him with a program that would determine who was the student winner according to the following rule: The first of each group to hit the number chosen by Genesio obviously wins the shirt but if no one hit this number, wins the shirt the first to get closest to this number.
It makes no difference who the group the teacher chooses to try to start guessing. This will always be the student number 1, and so on.
Input
The first line of input contains an integer N that determines the amount of test cases, or shirts that will be raffled. Each test case consists of two lines. The first line contains two integers QT (4 ≤ QT ≤ 10) and S (1 ≤ S ≤ 100) separated by a space, which indicate the amount of students in the group and the secret number to be guessed. The second row contains each QT values, separated by a space.
Output
For each test case, your program must print a integer number that indicates the position of the winner of the shirt, according to the rules above described.
Sample Input | Sample Output |
3 |
5 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n, i, j, qt, s, d, m, ind;
scanf("%d", &n);
for (i = 0; i < n; ++i) {
scanf("%d %d", &qt, &s);
d = ind = 999;
for (j = 0; j < qt; ++j) {
scanf("%d", &m);
if (d > abs(s - m)) {
d = abs(s - m);
ind = j;
}
}
printf("%d\n", ind + 1);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
7 35
8 26 30 43 36 17 7
4 16
12 16 3 16
10 48
3 7 27 9 50 49 16 47 63 1
Output
2
6
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int main(void) {
int n, i, j, qt, s, d, m, ind;
cin >> n;
for (i = 0; i < n; ++i) {
cin >> qt >> s;
d = ind = 999;
for (j = 0; j < qt; ++j) {
cin >> m;
if (d > abs(s - m)) {
d = abs(s - m);
ind = j;
}
}
cout << ind + 1 << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
7 35
8 26 30 43 36 17 7
4 16
12 16 3 16
10 48
3 7 27 9 50 49 16 47 63 1
Output
2
6
#3 Code Example with Python Programming
Code -
Python Programming
n = int(input())
for i in range(n):
e = [int(x) for x in str(input()).split()]
qt = e[0]
s = e[1]
e = [int(x) for x in str(input()).split()]
d = ind = 999
for j in range(qt):
if d > abs(s - e[j]):
d = abs(s - e[j])
ind = j
print(ind + 1)
Copy The Code &
Try With Live Editor
Input
7 35
8 26 30 43 36 17 7
4 16
12 16 3 16
10 48
3 7 27 9 50 49 16 47 63 1
Output
2
6