aboutsummaryrefslogtreecommitdiff
path: root/challenge-210/ulrich-rieke/cpp/ch-1.cpp
blob: 013b52b5c7b481dcf01b986230db833cc75d1284 (plain)
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <list>

std::vector<std::string> split( const std::string & startline ,
      const std::string & sep ) {
  std::vector<std::string> separated ;
  std::string::size_type start { 0 } ;
  std::string::size_type pos ;
  do {
      pos = startline.find_first_of( sep , start ) ;
      separated.push_back( startline.substr(start , pos - start )) ;
      start = pos + 1 ;
  } while ( pos != std::string::npos ) ;
  return separated ;
}

int main( ) {
  std::cout << "Please enter some integers, separated by a blank!\n" ;
  std::string line ;
  std::getline( std::cin , line ) ;
  std::vector<std::string> numberstrings( split( line , " " ) ) ;
  std::vector<int> numbers ;
  for ( auto s : numberstrings )
      numbers.push_back( std::stoi( s ) ) ;
  std::sort( numbers.begin( ) , numbers.end( ) ) ;
  std::list<int> theNumbers ;
  for ( int i : numbers )
      theNumbers.push_back( i ) ;
  theNumbers.sort( ) ;
  theNumbers.unique( ) ;
  std::vector<int> allSums ;
  for ( auto it = theNumbers.begin( ) ; it != theNumbers.end( ) ; it++ ) {
      int sum = 0 ;
      int ct = std::count( numbers.begin( ) , numbers.end( ) , *it - 1) ;
      sum += ct * (*it - 1) ;
      ct = std::count( numbers.begin( ) , numbers.end( ) , *it ) ;
      sum += ct * *it ;
      ct = std::count( numbers.begin( ) , numbers.end( ) , *it + 1 ) ;
      sum += ct * (*it + 1 ) ;
      allSums.push_back( sum ) ;
  }
  std::cout<< *max_element( allSums.begin( ) , allSums.end( ) ) << std::endl ;
  return 0 ;
}