Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1943
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1943
Top N
By Cristhian Bonilha, UTFPR Brazil
Timelimit: 1
The regional phase of the SBC Programming Contest happened recently, where more than 600 teams participated in more than 40 cities around Brazil. Your friend competed, and when asked about his position he told you: “I got placed in the top 10”.
You were happy for your friend, but you could not stop asking yourself about what was his real position. “Top 10” could mean any position between first and tenth placed, however if he had placed first he would have said “Top 1”, if he had placed second or third he would have said “Top 3”, and if he had placed fourth or fifth he would have said “Top 5”. Therefore, his real position was between sixth and tenth, because people tend to put themselves in the lowest category they belong.
You gathered all the categories people most use: 1, 3, 5, 10, 25, 50 and 100. Given a position K, write an algorithm that says the number of the lowest category this position belongs.
Input
Each test case has one integer K, representing a position (1 ≤ K ≤ 100).
Output
For each test case you should print one line with the sentence “Top N”, and replace N by the number of the lowest category the position K belongs.
25 |
Top 25 |
26 |
Top 50 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main (void)
{
unsigned short n;
scanf("%hu", &n);
if (n == 1)
printf("Top 1\n");
else if (n == 2 || n == 3)
printf("Top 3\n");
else if (n == 4 || n == 5)
printf("Top 5\n");
else if (n >= 6 && n < = 10)
printf("Top 10\n");
else if (n >= 11 && n <= 25)
printf("Top 25\n");
else if (n >= 26 && n < = 50)
printf("Top 50\n");
else if (n >= 50 && n <= 100)
printf("Top 100\n");
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
static final int[] TOPS = {1, 3, 5, 10, 25, 50, 100};
public static void main(String[] args) throws IOException {
int K = readInt();
int t = 0;
for (int top : TOPS) {
if (K < = top) {
t = top;
break;
}
}
out.println("Top " + t);
out.close();
}
private static int readInt() throws IOException {
return Integer.parseInt(in.readLine());
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const [position] = readFileSync("/dev/stdin", "utf8").split("\n")
const TOPS = [1, 3, 5, 10, 25, 50, 100]
function main() {
const topList = TOPS.find(top => top >= +position)
console.log("Top", topList)
}
main()
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
n = int(input())
print('Top', end=' ')
if n == 1: print(1)
elif n <= 3: print(3)
elif n <= 5: print(5)
elif n <= 10: print(10)
elif n <= 25: print(25)
elif n <= 50: print(50)
else: print(100)
Copy The Code &
Try With Live Editor
Input
Output