Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2456
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2456
Cards
By OBI - Olimpíada Brasileira de Informática 2014 Brazil
Timelimit: 1
Beatriz loves to play cards with her friends. To train memory and logical thinking, she invented a small hobby with cards. She removes the top five cards from the top of a well-shuffled deck, and places them in sequence from left to right, the table with the face down.
Then she looks, for a moment, each of the sequence of cards (and soon puts back on the table, face down). Using only your memory, Beatrice should now say whether the sequence of cards is increasingly ordered, decreasingly, or is not orderly.
After so much play, she's getting tired, and do not trust their own judgment to find out whether she has succeeded or made a mistake. So she asked you to make a program that, given a sequence of five cards, determine whether a given sequence is ordered increasingly, decreasingly, or is not orderly.
Input
The input consists of a single line containing five cards of the sequence. Card values are represented by integers between 1 and 13. The five cards have different values.
Output
Your program should produce a single line containing a single uppercase character 'C' if the given sequence is increasingly ordered, 'D' if ordered decreasingly, or 'N' otherwise.
Input Sample | Output Sample |
1 2 3 5 6 |
C |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdbool.h>
bool verificaOrdCresc(short *, unsigned short);
bool verificaOrdDecre(short *, unsigned short);
int main (void)
{
short cartas[5], i;
for (i = 0; i < 5; i++)
scanf("%hd", &cartas[i]);
if (verificaOrdCresc(cartas, 5))
printf("C\n");
else if (verificaOrdDecre(cartas, 5))
printf("D\n");
else
printf("N\n");
}
bool verificaOrdCresc(short *vetor, unsigned short tam)
{
short i;
bool ordenado = true;
for (i = 1; i < tam; i++)
if (vetor[i] < vetor[i - 1] && ordenado == true)
ordenado = false;
if (ordenado)
return true;
else
return false;
}
bool verificaOrdDecre(short *vetor, unsigned short tam)
{
short i;
bool ordenado = true;
for (i = 1; i < tam; i++)
if (vetor[i] > vetor[i - 1] && ordenado == true)
ordenado = false;
if (ordenado)
return true;
else
return false;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
int main() {
int a, b, c, d, e;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (a >= b && b >= c && c >= d && d >= e) {
printf("D\n");
} else if (a < = b && b <= c && c <= d && d <= e) {
printf("C\n");
} else {
printf("N\n");
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const input = readFileSync("/dev/stdin", "utf8")
.split(" ", 5)
.map((value) => Number.parseInt(value, 10))
/**
* @param {string | any[]} arr
* @param {number} position
*/
const at = (arr, position) => arr[(arr.length + position) % arr.length]
/** @param {number[]} arr */
function isCrescent(arr) {
for (let index = 0; index < arr.length - 1; index++)
if (at(arr, index) < at(arr, index + 1) === false)
return false
return true
}
/** @param {number[]} arr */
function isDecrescent(arr) {
for (let index = 0; index < arr.length - 1; index++)
if (at(arr, index) > at(arr, index + 1) === false)
return false
return true
}
function main() {
if (isCrescent(input)) console.log("C")
else if (isDecrescent(input)) console.log("D")
else console.log("N")
}
main()
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
e = [int(x) for x in input().split()]
c = sorted(e)
d = c[::-1]
print('C' if e==c else 'D' if e==d else 'N')
Copy The Code &
Try With Live Editor
Input
Output