almost lost everything there :/

This commit is contained in:
ldlda committed 2024-10-24 02:34:49 +07:00
1 parent 2930b7217d
commit ad3ce0bd48
6 files changed
+217 -6

No files matched your search

+3 -6
View File
@@ -7,13 +7,11 @@ Suppose we would like to implement a queue system for a commercial website.
Given that the website offers a list of items, and each item has a limited quantity in stock, implement a queue of customers, such that each customer can buy only a few products among the available items.
- Determine the item name, quantity, and price in stock.
- Specify a queue of n customers; each customer can buy k products from one
item (k is different for each customer).
- Specify a queue of n customers; each customer can buy k products from one item (k is different for each customer).
- Customers take turns to enter (enqueue) and leave (dequeue) the queue according to the FIFO order to purchase wanted products.
- If a customer successfully purchases products, display a message and reduce the number of products in stock. Otherwise, display a warning message if the item has been run out.
Implement the above problems in C/C++ using a Queue data structure. Write
a main function for testing all written functions (init(), display(), enqueue(), dequeue(),...).
Implement the above problems in C/C++ using a Queue data structure. Write a main function for testing all written functions (init(), display(), enqueue(), dequeue(),...).
## ex 2
@@ -24,7 +22,6 @@ Choose one method to implement a Stack data structure (Array-based or Stack usin
- A website may contain a url and a title; suppose that we have visited a list of websites.
- The order of visited websites is stored in a Backward stack according to FILO.
- If we return to the previous website, we pop the Backward stack and push it into the Forward stack.
- And otherwise, when we forward to the next website, we retrieve the top
website from the Forward stack and push it into the Backward stack.
- And otherwise, when we forward to the next website, we retrieve the top website from the Forward stack and push it into the Backward stack.
Write a main function to test all written functions (init(), display(), push(), pop(),...).
+36
View File
@@ -0,0 +1,36 @@
#include <stdio.h>
int calculate_iter(int n)
{
int c = 0;
while (n > 0)
{
c += n % 10;
n /= 10;
}
return c;
}
int calculate_recur(int n)
{
if (n <= 0)
return 0;
return n % 10 + calculate_iter(n / 10);
}
// both complexity O(logn)
int main(int argc, char const *argv[])
{
int w;
printf("number: ");
scanf("%d", &w);
int a = calculate_iter(w);
int b = calculate_recur(w);
if (a != b)
printf("oops! %d != %d\n", a, b);
else
printf(" yay! %d == %d\n", a, b);
// main(argc, argv);
return 0;
}
+53
View File
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdbool.h>
bool _sphenic(int num, int cnt, int pr)
{
// Base case: If we have already found 3 distinct primes, check if the number is fully divided.
if (cnt == 3)
return num == 1;
// Try dividing the number by the next prime factor.
while (++pr <= num)
{
if (num % pr == 0)
{
num /= pr;
// If the same prime factor divides it again, it's not sphenic.
if (num % pr == 0)
return false;
// Recursively check for the next prime and increase the count of distinct primes.
return _sphenic(num, cnt + 1, pr);
}
}
// If we run out of primes without finding 3 distinct ones, it's not sphenic.
return false;
}
bool rsphenic(int n)
{
// Sphenic numbers are always greater than or equal to 30.
if (n < 30)
return false;
// Initialize the recursion to start with prime checking from 2.
return _sphenic(n, 0, 1);
}
int main()
{
int N = 200;
for (int i = 1; i < N; i++)
{
// printf("%d\n", i);
if (rsphenic(i))
printf("%d ", i);
}
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
# obj
verify whether a set of 3 values is a Pythagorean triple. A Pythagorean triple is a set of 3 positive numbers, which are expressed as a2 + b2 = c2.
Example:
- (3, 5, 4) is a Pythagorean triple, 3^2 + 4^2 = 5^2.
- (10, 8, 6) is a Pythagorean triple, 6^2 + 8^2 = 10^2.
- (6, 8, 13) is not a Pythagorean triple.
## 1
- Write a pseudo-code to find all Pythagorean triples from a given array using Iteration.
- Implement your proposed algorithm in C/C++. You have to initialize a random array (any size >= 6, any value) in the main() function.
- Calculate the complexity of your algorithm. Justify the answer (comment directly in your source files).
## 2
- Propose another algorithm using recursive functions and implement in C/C++ to perform the above problem.
- Calculate the complexity of your algorithm. Justify the answer (comment directly in your source files).
+50
View File
@@ -0,0 +1,50 @@
# i especially hate this one
## prompt
In this problem, we try to convert a decimal fraction into binary number. In the input, we are given an fraction decimal number n and integer k, convert decimal number n into equivalent binary number up-to k precision after decimal point.
We have to follow the algorithm:
1. Convert the integral part of decimal to binary equivalent:
- Divide the decimal number by 2 and store remainders in array.
- Divide the quotient by 2.
- Repeat step 2 until we get the quotient equal to zero.
- Equivalent binary number would be reverse of all remainders of step 1.
2. Convert the fractional part of decimal to binary equivalent
- Multiply the fractional decimal number by 2.
- Integral part of resultant decimal number will be first digit of fraction binary number.
- Repeat step 1 using only fractional part of decimal number and then step 2.
Let's take an example for n = 4.47, k = 4
Step 1: Conversion of 4 to binary
1. 4/2 : Remainder = 0 : Quotient = 2
2. 2/2 : Remainder = 0 : Quotient = 1
3. 1/2 : Remainder = 1 : Quotient = 0
So equivalent binary of integral part of decimal is 100.
Step 2: Conversion of .47 to binary
1. 0.47 \* 2 = 0.94, Integral part: 0
2. 0.94 \* 2 = 1.88, Integral part: 1
3. 0.88 \* 2 = 1.76, Integral part: 1
4. 0.76 \* 2 = 1.52, Integral part: 1
So equivalent binary of fractional part of decimal is .0111
The final result of conversion n = 4.47 (with precision k = 4): 100.0111
## 1
- Write a pseudo-code to convert a natural number into binary using Iteration.
- Implement your proposed algorithm in C/C++.
- Calculate the complexity of your algorithm. Justify the answer (comment directly in your source files).
## 2
- Propose another algorithm using recursive functions and implement in C/C++ to perform the above problem.
- Calculate the complexity of your algorithm. Justify the answer (comment directly in your source files).
+55
View File
@@ -0,0 +1,55 @@
# id rather use chatgpt
## 1
Initialize a random array of your choice. The current goal is to verify whether an array number is
centered cube.
Note: a centered cube number is calculated by the following formula:
𝑓(𝑛) = 𝑛
3 + (𝑛 + 1)
3
For example, the centered cube numbers are:
f(1) = 1
3 + (1 + 1)
3 = 9
f(2) = 2
3 + (2 + 1)
3 = 8 + 27 = 35
f(3) = 3
3 + (3 + 1)
3 = 27 + 64 = 91
...
- Write pseudo-code using Recursion (if necessary, Iteration) to check all values in the given
array are centered cube or not.
- Write a program in C/C++ to complete your proposed algorithm in the pseudo-code. .
Note: The program must include a recursive function and the main function.
- Calculate the complexity of your algorithm. Justify the answer.
## 2
This problem requires to traverse and display a tree using the following process:
- Visit a node and find its child nodes.
- Put the child nodes into the queue.
- Pop a node in the queue and repeat the process until all nodes are visited.
Justify and print the traversal result for the below tree.
10
4 8
13 20 90
## 3
Given an array of n elements, the Bubble sort algorithm swaps repeatedly the adjacent elements
if they are in the wrong order (ascending order). We want to implement an improve version of this
sorting algorithm.
The idea is to sort both maximum (at the end) and minimum (at the beginning) in one pass and the
array is sorted from both ends.
- Traverse from left to right and compare adjacent elements and the maximum is placed at
right side.
- Traverse from right to left and compare adjacent elements and the minimum is placed at
left side.
- Repeat the process until the array is sorted.
Implement this algorithm in C/C++. Calculate the complexity of the above algorithm. Justify your
answer.