n) ... Kompyuterlar ikkilik tizimdan foydalanganligi sababli, logarifmning asosi sifatida 2 ishlatiladi (ya'ni log 2). n). Biroq, bilan bazani almashtirish logaritmalar log a n va jurnal b n faqat doimiy koeffitsient bilan farqlanadi, bu esa O-katta belgisida bekor qilinadi. Shunday qilib, O (log n) - logarifm asosidan qat'iy nazar, logarifmik vaqt algoritmlari uchun standart belgi.
Logarifmik vaqt algoritmlari odatda binar daraxt operatsiyalarida yoki ikkilik qidiruvdan foydalanganda topiladi.
O (log n) algoritmlari yuqori samarali hisoblanadi, chunki har bir elementning bajarilish vaqti elementlar sonining ko'payishi bilan kamayadi.
Bunday algoritmning juda oddiy misoli qatorni ikkiga bo'lish, ikkinchi yarmini yana yarmiga bo'lish va hokazo. Bu O (log n) vaqtni oladi (bu erda n - satr uzunligi, biz bu erda taxmin qilamiz console.log va str.substring doimiy vaqt talab qiladi). Bu shuni anglatadiki, nashrlar sonini ko'paytirish uchun chiziq uzunligi ikki barobarga oshirilishi kerak.
2.1.Butun musbat sonlardan iborat a,b,c qiymatlar berilgan bo’lsa, shu sonlar uchburchak tomonlari bo’la olishini aniqlash algoritmi va dasturini tuzing.
#include
using namespace std;
int main() {
int a, b, c;
cout << "Uchburchakning tomonlarini kiriting:\n";
cin >> a >> b >> c;
if (a + b > c && a + c > b && b + c > a) {
cout << "Uchburchak bo'lish sharti bajariladi\n";
} else {
cout << "Uchburchak bo'lish sharti bajarilmaydi\n";
}
return 0;
}
#include
#include
using namespace std;
double integrand(double x) {
return 1 + pow(x, 3) + cos(x);
}
double integrate(double (*f)(double), double a, double b) {
double h = 0.00001; // integralni hisoblash uchun ishlatiladigan delta x
double sum = 0;
int n = (b - a) / h; // integralni qancha qismga bo'lib hisoblash kerakligi
for (int i = 0; i < n; i++) {
double x = a + i * h;
sum += f(x) * h;
}
return sum;
}
int main() {
double a = 0;
double b = 1;
double integral = integrate(integrand, a, b);
cout << "Natija: " << integral << endl;
return 0;
}
3.Butun sonlardan iborat bir o’lchovli massivni tezkor saralash(Quick sort) usulidan foydalanib, o’sish tartibida saralang.
#include
using namespace std;
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
4.x3-2x2+7x-1=0
#include
#include
using namespace std;
double f(double x) {
return pow(x, 3) - 2 * pow(x, 2) + 7 * x - 1;
}
double iterative_solver(double x0, double tol, int max_iter) {
double x = x0;
double fx = f(x);
int iter = 0;
while (fabs(fx) > tol && iter < max_iter) {
double x1 = (2 * pow(x, 2) - 7 * x + 1) / (x - 3);
double fx1 = f(x1);
if (fabs(fx1) <= tol) {
return x1;
}
x = x1;
fx = fx1;
iter++;
}
return x;
}
int main() {
double x0 = 0; // boshlanish nuqta
double tol = 1e-6; // toleransiya
int max_iter = 100; // maksimal iteratsiya soni
double solution = iterative_solver(x0, tol, max_iter);
cout << "Tenglama yechimi: " << solution << endl;
return 0;
}
Do'stlaringiz bilan baham: |