Skip to content

task_01 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
#include <iostream>

int main() { return 0; }
#include "searching_sum.hpp"

/*
Формат ввода:
В первой строке ввода через пробел подаётся 2 целых числа:
sum - заданное число, n - длина массива чисел
В следующей строке подаются n целых чисел - элемента массива

Формат вывода:
В одной строке через пробел выводятся два числа - индексы элементов данного
массива, дающие в сумме sum
*/
int main() {
int sum = 0, n = 0;
std::cin >> sum >> n;
std::vector<int> arr(n);
for (int i = 0; i < n; ++i) std::cin >> arr[i];
std::pair<int, int> answer = SearchingSum(sum, arr);
std::cout << answer.first << " " << answer.second << std::endl;
return 0;
}
15 changes: 15 additions & 0 deletions task_01/src/searching_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "searching_sum.hpp"

/*
Алгоритм для нахождения в массиве чисел, дающих в сумме заданное число
Ассимптотика алгоритма - O(n), затраты по памяти - O(n)
*/
std::pair<int, int> SearchingSum(int sum, std::vector<int> arr) {
std::pair<int, int> ans = {0, arr.size() - 1};
while (ans.first < ans.second) {
if (arr[ans.first] + arr[ans.second] == sum) return ans;
if (arr[ans.first] + arr[ans.second] < sum) ++ans.first;
if (arr[ans.first] + arr[ans.second] > sum) --ans.second;
}
return {-1, -1};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

как отличить отсутствие результата от результата (-1, -1)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что понимается в таком случае под отсутствием результата, ведь вывод (-1, -1) как раз значит, что таких индексов не нашлось?

}
9 changes: 9 additions & 0 deletions task_01/src/searching_sum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SEARCHING_SUM
#define SEARCHING_SUM

#include <map>
#include <vector>

std::pair<int, int> SearchingSum(int sum, std::vector<int> arr);

#endif // SEARCHING_SUM
29 changes: 26 additions & 3 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include "searching_sum.hpp"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
TEST(SearchingSum, Simple) {
int sum1 = 10;
std::vector<int> arr1 = {1, 3, 4, 7, 14};
std::pair<int, int> ans1 = {1, 3};
ASSERT_EQ(ans1, SearchingSum(sum1, arr1));

int sum2 = 19;
std::vector<int> arr2 = {1, 2, 5, 5, 6, 7, 8, 9, 14};
std::pair<int, int> ans2 = {2, 8};
ASSERT_EQ(ans2, SearchingSum(sum2, arr2));

int sum3 = 8;
std::vector<int> arr3 = {};
std::pair<int, int> ans3 = {-1, -1};
ASSERT_EQ(ans3, SearchingSum(sum3, arr3));

int sum4 = -1;
std::vector<int> arr4 = {-7, -5, 0, 1, 3, 4, 6};
std::pair<int, int> ans4 = {0, 6};
ASSERT_EQ(ans4, SearchingSum(sum4, arr4));

int sum5 = 0;
std::vector<int> arr5 = {1, 2, 5, 5, 6, 7, 8, 9, 14};
std::pair<int, int> ans5 = {-1, -1};
ASSERT_EQ(ans5, SearchingSum(sum5, arr5));
}