Compare commits
2 Commits
2a3417e95f
...
trunk
| Author | SHA1 | Date | |
|---|---|---|---|
| 5120cc20c2 | |||
| 84298c0b81 |
59
axis/task1.c
Normal file
59
axis/task1.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int solution(char *str) {
|
||||
static const size_t LEN = 3;
|
||||
static const char* one = "one";
|
||||
static const char* two = "two";
|
||||
int operand[2] = {0, 0};
|
||||
int operand_index = 0;
|
||||
char op = 0x00;
|
||||
|
||||
char* ptr = str;
|
||||
while (*ptr != '\0') {
|
||||
int cmp = strncmp(ptr, one, LEN);
|
||||
if (cmp == 0) {
|
||||
operand[operand_index++] = 1;
|
||||
ptr += LEN;
|
||||
}
|
||||
cmp = strncmp(ptr, two, LEN);
|
||||
if (cmp == 0) {
|
||||
operand[operand_index++] = 2;
|
||||
ptr += LEN;
|
||||
}
|
||||
|
||||
if (operand_index == 2) {
|
||||
operand_index = 0;
|
||||
int a = operand[0];
|
||||
int b = operand[1];
|
||||
int res = 0;
|
||||
if (op == '+') res = a + b;
|
||||
else res = a - b;
|
||||
operand[operand_index++] = res;
|
||||
}
|
||||
|
||||
if (*ptr == '+') {
|
||||
op = '+';
|
||||
} else if (*ptr == '-') {
|
||||
op = '-';
|
||||
} else {
|
||||
assert(true && "Invalid parsing!");
|
||||
}
|
||||
++ptr;
|
||||
|
||||
}
|
||||
|
||||
return operand[0];
|
||||
}
|
||||
|
||||
int main() {
|
||||
char* str = "one+two-one-one+two+one";
|
||||
int ret = solution(str);
|
||||
printf("input: %s = %d\n", str, ret);
|
||||
return 0;
|
||||
}
|
||||
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