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

+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.