Оновити coins.c
This commit is contained in:
72
coins.c
72
coins.c
@@ -7,8 +7,53 @@
|
||||
#define MAX_N 1024
|
||||
#define WORDS ((MAX_N / 64) + 1)
|
||||
#define ITERATIONS 10000000
|
||||
#define NUMBER 86
|
||||
#define NUMBER 999
|
||||
|
||||
//Artem
|
||||
|
||||
int main123(int n) {
|
||||
int a = 0;
|
||||
while (n != 0) {
|
||||
if (n - 50 >= 0) {
|
||||
n -= 50;
|
||||
a++;
|
||||
}
|
||||
else {
|
||||
if (n - 25 >= 0) {
|
||||
n -= 25;
|
||||
a++;
|
||||
}
|
||||
else {
|
||||
if (n - 10 >= 0) {
|
||||
n -= 10;
|
||||
a++;
|
||||
}
|
||||
else {
|
||||
if (n - 5 >= 0) {
|
||||
n -= 5;
|
||||
a++;
|
||||
}
|
||||
else {
|
||||
if (n - 2 >= 0) {
|
||||
n -= 2;
|
||||
a++;
|
||||
}
|
||||
else {
|
||||
if (n - 1 >= 0) {
|
||||
n -= 1;
|
||||
a++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
// ==========================================
|
||||
// 1. TRADITIONAL DP (FOR-LOOP) APPROACH
|
||||
// ==========================================
|
||||
@@ -51,7 +96,7 @@ int min_coins_bitwise(int n, int* coins, int num_coins) {
|
||||
if (n == 0) return 0;
|
||||
if (n > MAX_N) return -1;
|
||||
|
||||
uint64_t state[WORDS] = {0};
|
||||
uint64_t state[WORDS] = { 0 };
|
||||
state[0] = 1ULL;
|
||||
uint64_t next_state[WORDS];
|
||||
|
||||
@@ -87,8 +132,8 @@ int min_coins_bitwise_optimized(int n, int* coins, int num_coins) {
|
||||
int active_words = (n / 64) + 1;
|
||||
|
||||
// Allocate two arrays to act as our "Current" and "Next" states
|
||||
uint64_t bufferA[WORDS] = {0};
|
||||
uint64_t bufferB[WORDS] = {0};
|
||||
uint64_t bufferA[WORDS] = { 0 };
|
||||
uint64_t bufferB[WORDS] = { 0 };
|
||||
|
||||
bufferA[0] = 1ULL;
|
||||
|
||||
@@ -142,7 +187,7 @@ int min_coins_bitwise_optimized(int n, int* coins, int num_coins) {
|
||||
// MAIN BENCHMARK
|
||||
// ==========================================
|
||||
int main() {
|
||||
int ukraine_coins[] = {1, 2, 5, 10, 25, 50};
|
||||
int ukraine_coins[] = { 1, 2, 5, 10, 25, 50 };
|
||||
int num_coins = 6;
|
||||
int n = NUMBER; // 868 cents (Answer: 17 coins + 10 + 5 + 2 + 1 = 21 coins)
|
||||
|
||||
@@ -158,7 +203,7 @@ int main() {
|
||||
total_sum += min_coins_dp(n, ukraine_coins, num_coins);
|
||||
}
|
||||
end = clock();
|
||||
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
|
||||
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
|
||||
printf("1. Traditional DP Time: %f seconds (Answer: %d)\n", cpu_time_used, total_sum / ITERATIONS);
|
||||
|
||||
// --- Benchmark Bitwise ---
|
||||
@@ -168,7 +213,7 @@ int main() {
|
||||
total_sum += min_coins_bitwise(n, ukraine_coins, num_coins);
|
||||
}
|
||||
end = clock();
|
||||
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
|
||||
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
|
||||
printf("2. Native Bitwise Time: %f seconds (Answer: %d)\n", cpu_time_used, total_sum / ITERATIONS);
|
||||
|
||||
// --- Benchmark Bitwise Optimized---
|
||||
@@ -178,7 +223,18 @@ int main() {
|
||||
total_sum += min_coins_bitwise_optimized(n, ukraine_coins, num_coins);
|
||||
}
|
||||
end = clock();
|
||||
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
|
||||
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
|
||||
printf("3. Optimized Bitwise Time: %f seconds (Answer: %d)\n", cpu_time_used, total_sum / ITERATIONS);
|
||||
|
||||
//Artem
|
||||
total_sum = 0; // Reset
|
||||
start = clock();
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
total_sum += main123(n);
|
||||
|
||||
}
|
||||
end = clock();
|
||||
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
|
||||
printf("3. Artemtime: %f seconds (Answer: %d)\n", cpu_time_used, total_sum / ITERATIONS);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user