[AOJ] Descending sort in various languages

I found a programming problem submission & scoring site provided by the University of Aizu called AIZU ONLINE JUDGE and it was fun, so I have problems in various languages. I decided to solve it.

Select 5 languages: C ++, python, php, sqlite3, common lisp. The language I usually use is C ++, and I wrote other languages while searching online.

This time, the problem is 0001 List of Top 3 Hills.

problem

List of Top 3 Hills

There are 10 pieces of data that represent the height of the mountain as an integer in meters. Create a program that reads the 10 data, outputs 3 of them in descending order, and finishes.

Input

Mountain height 1 (integer) Mountain height 2 (integer) . . Mountain height 10 (integer)

Constraints

0 ≤ mountain height ≤ 10000

Output

Highest mountain height The height of the second highest mountain The height of the third highest mountain

Sample Input

1819 2003 876 2840 1723 1673 3776 2848 1592 922

Output for the Sample Input

3776 2848 2840

For the time being, write something that returns the correct answer to the sample input. Sorting is included as standard in all languages, so I didn't have to write the algorithm myself.

Answer

C++


#include <iostream>
#include <set>
using namespace std;
 
int main() {
 
  typedef std::multiset<int> MySet;
  MySet heightSet;
 
  for(int i = 0; i < 10; ++i)
  {
    int val;
    cin >> val;
    //Automatically sorts when you insert an element into set
    heightSet.insert(val);
  }
 
  //Reverse to access in descending order_use iterator
  MySet::reverse_iterator rit = heightSet.rbegin();
  for(int i = 0; i < 3; ++i)
  {
    cout << *rit << endl;
    ++rit;
  }
  return 0;
}

If you use STL's multiset container, it will be sorted automatically when you insert an element, so you can skip it a little.

python


#Create an array
n = list()

for var in range(0, 10):
  #Read one line from standard input
  #Cast to int
  n.append( int(raw_input()) )
  
#Sort the array in descending order
n.sort(reverse = True)
  
for var in range(0, 3):
  print n[var]

I was impressed that I could write it short with python.

PHP


<?php
$hi = fopen('php://stdin', "r");
$ho = fopen('php://stdout', "w");

#Create an empty array
$arr = array();

#Set data from standard input
for($i = 0; $i < 10; $i++)
{
	fscanf($hi, "%d", $arr[]);
}

#Descending sort
rsort($arr);

#output
for($i = 0; $i < 3; $i++)
{
	fwrite($ho, sprintf("%d\n", $arr[$i]));
}

fclose($ho);
fclose($hi);

It feels like C language except for adding $ to variables.

sqlite3


--Creating a table
create table tbl(height integer);

--Read data from a text file
.import data.txt tbl

-- 'tbl'From'height'Sort in descending order by field and read 3 lines
select * from tbl order by height desc limit 3;

I didn't know how to put information into a record from standard input in sqlite3, so I'm reading from a file.

Common Lisp


;;Receive n times from standard input and add to list
(defun input (lst n)
	(cond
		((eq (length lst) n)
			lst
		)
		(t
			(input (append lst (list (read)) ) n )
		)
	)
)

;;Create a subarray of the first n elements of the list
(defun gettop (lst n)
	(if (or (= n 0) (null lst))
		nil
		(cons (car lst)
			(gettop (cdr lst) (- n 1))
		)
	)
)

(mapcar #'(lambda (x) (format t "~A~%" x))	;Output list elements separated by line breaks
	(gettop
		(sort (input () 10) #'>)	;Create a list from standard input and sort in descending order
		3							;Create the first three lists
	)
)

Common Lisp was too foreign for me to get used to procedural programming, and it took a lot of time.

Recommended Posts

[AOJ] Descending sort in various languages
HMAC in various languages
python in mongodb in descending sort
AOJ Sort I-
Write classes (or class-like) in various programming languages
Numerical representation of days of the week in various languages
Bubble sort in Python
Custom sort in Python3
Representing Fibonacci sequences using lambda expressions in various languages
Compete for file IO in various languages and compare speeds
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
I implemented N-Queen in various languages and measured the speed
10 Most Popular Programming Languages in 2020
Naturally sort Path in Python
Insertion sort of AOJ exercises
Sort by date in python
I made a prime number table output program in various languages