-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcups.cpp
90 lines (67 loc) · 1.81 KB
/
cups.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @file cups.cpp
* @author William Weston
* @brief Cups Problem From Kattis
* @version 0.1
* @date 2023-07-30
*
* @copyright Copyright (c) 2023
*
* Source: https://open.kattis.com/problems/cups
*/
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
struct Cup final
{
std::string colour;
double radius;
};
auto make_cup( std::string const& input ) -> Cup;
auto main() -> int
{
for ( auto N = int{}; std::cin >> N; )
{
auto cups = std::vector<Cup>();
cups.reserve( N );
// call ignore when switching from stream to getline
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while ( N-- )
{
auto line = std::string();
std::getline( std::cin, line );
cups.push_back( make_cup( line ) );
}
std::sort( cups.begin(),
cups.end(),
[] ( Cup const& lhs, Cup const& rhs ) { return lhs.radius < rhs.radius; } );
for ( auto const& cup : cups )
std::cout << cup.colour << '\n';
}
return EXIT_SUCCESS;
}
auto make_cup( std::string const& input ) -> Cup
{
auto const is_alpha = []( char ch) { return std::isalpha( static_cast<unsigned char>( ch ) ); };
auto iss = std::istringstream( input );
char const first = iss.peek();
if ( is_alpha( first ) )
{
auto colour = std::string();
auto radius = double{};
iss >> colour >> radius;
return Cup{ colour, radius };
}
else // error path radius has been double
{
auto colour = std::string();
auto radius = double{};
iss >> radius >> colour;
return Cup{ colour, radius / 2.0 };
}
}