test done
This commit is contained in:
60
axis/task2.cpp
Normal file
60
axis/task2.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
|
||||||
|
// D: Vector of numbers that show distance.
|
||||||
|
// C: Vector of numbers that show quantities to be delivered at D[K], where K is the location.
|
||||||
|
// P: The totalt numbers of product available.
|
||||||
|
// Delivery start from lowest distance.
|
||||||
|
// Only delivers when P is the same or greater as C[K].
|
||||||
|
// If we should deliver everything then the order left need to be 0
|
||||||
|
// else we don't deliver even if we have more product available.
|
||||||
|
int solution(std::vector<int> &D, std::vector<int> &C, int P) {
|
||||||
|
assert(D.size() == C.size() && "Vector D and C needs to be the same size");
|
||||||
|
std::vector<std::tuple<int, int>> zip{};
|
||||||
|
for (std::size_t i = 0; i < D.size(); ++i)
|
||||||
|
zip.emplace_back(D[i], C[i]);
|
||||||
|
std::sort(std::begin(zip), std::end(zip), [](auto const& a, auto const& b) {
|
||||||
|
return std::get<0>(a) < std::get<0>(b);
|
||||||
|
});
|
||||||
|
|
||||||
|
int delivered = 0;
|
||||||
|
for (std::size_t i = 0; i < zip.size(); ++i) {
|
||||||
|
auto const CK = std::get<1>(zip[i]);
|
||||||
|
auto const total = delivered + CK;
|
||||||
|
std::printf("CK: %d, total: %d\n", CK, total);
|
||||||
|
if (total >= P) break;
|
||||||
|
if (P - total > 0 && i == zip.size() - 1) break;
|
||||||
|
delivered = total;
|
||||||
|
}
|
||||||
|
if (delivered == 0) return 0;
|
||||||
|
return P - delivered;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// std::vector<int> D = {5, 11, 1, 3};
|
||||||
|
// std::vector<int> C = {6, 1, 3, 2};
|
||||||
|
// int P = 7;
|
||||||
|
// int res = solution(D, C, P);
|
||||||
|
// std::printf("%d\n", res);
|
||||||
|
|
||||||
|
// {
|
||||||
|
// std::vector<int> D = {10, 15, 1};
|
||||||
|
// std::vector<int> C = {10, 1, 2};
|
||||||
|
// int P = 3;
|
||||||
|
// int res = solution(D, C, P);
|
||||||
|
// std::printf("test 2: %d\n", res);
|
||||||
|
// }
|
||||||
|
|
||||||
|
{
|
||||||
|
std::vector<int> D = {1, 4, 2, 5};
|
||||||
|
std::vector<int> C = {4, 9, 2, 3};
|
||||||
|
int P = 19;
|
||||||
|
int res = solution(D, C, P);
|
||||||
|
std::printf("test 3: %d\n", res);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
31
axis/task3.c
Normal file
31
axis/task3.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
char* solution(int N) {
|
||||||
|
static const int size = (int)('z' - 'a');
|
||||||
|
const int interval = (int)ceil((double)N / (double)size);
|
||||||
|
|
||||||
|
char* str = (char*)malloc((N + 1) * sizeof(char));
|
||||||
|
str[N] = '\0';
|
||||||
|
int count = 0;
|
||||||
|
char offset = 0;
|
||||||
|
for (int i = 0; i < N; ++i) {
|
||||||
|
str[i] = 'a' + offset;
|
||||||
|
if (++count >= interval) {
|
||||||
|
count = 0;
|
||||||
|
++offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
{
|
||||||
|
char* str = solution(3);
|
||||||
|
printf("%s\n", str);
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user