main
Nikita Tokarchuk 3 years ago
parent 14d0b7f9e2
commit 31d1a5f464
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 6
      CMakeLists.txt
  2. 124
      main.cpp

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.21)
project(max_sum_squal_digits)
set(CMAKE_CXX_STANDARD 14)
add_executable(max_sum_squal_digits main.cpp)

@ -0,0 +1,124 @@
#include <iostream>
#include <vector>
#include <array>
#include <stack>
template<typename T>
class Stack {
private:
size_t _len;
std::vector<T> _data;
public:
explicit Stack(size_t alloc_size = 128);
size_t push(T elem);
T pop();
size_t len();
T front();
};
template<typename T>
size_t Stack<T>::push(T elem) {
if (this->_data.size() > this->_len) {
this->_data[this->_len] = elem;
this->_len++;
return this->_len;
}
this->_data.push_back(elem);
this->_len = this->_data.size();
return this->_len;
};
template<typename T>
Stack<T>::Stack(size_t alloc_size) : _len(0), _data(alloc_size) {};
template<typename T>
T Stack<T>::pop() {
if (this->_len == 0) {
std::abort();
}
this->_len--;
return this->_data[this->_len];
}
template<typename T>
size_t Stack<T>::len() {
return this->_len;
}
template<typename T>
T Stack<T>::front() {
if (this->_len == 0) {
std::abort();
}
return this->_data[this->_len - 1];
};
size_t digits_sum(int number) {
size_t result = 0;
while(number != 0) {
result += number%10;
number /= 10;
}
return result;
}
typedef std::pair<int, int> two_biggest_t;
typedef std::array<two_biggest_t, 81> index_t;
int solution(std::vector<int> A) {
index_t index;
for (int number : A) {
size_t sum = digits_sum(number);
auto &two_biggest = index[sum];
if (number > two_biggest.first) {
if (number > two_biggest.second) {
two_biggest.first = two_biggest.second;
two_biggest.second = number;
} else {
two_biggest.first = number;
}
}
}
int biggest_sum = -1;
for (auto &sum : index) {
if (sum.first == 0) {
continue;
}
int local_sum = sum.first + sum.second;
if (local_sum > biggest_sum) {
biggest_sum = local_sum;
}
}
return biggest_sum;
}
int main() {
// std::cout << digits_sum(100) << std::endl;
std::cout << solution({51, 32, 43}) << std::endl;
return 0;
}
Loading…
Cancel
Save