Алгоритмы (C): различия между версиями

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску
 
(не показано 16 промежуточных версий этого же участника)
Строка 4: Строка 4:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int n, x;
 
     int n, x;
  
Строка 27: Строка 28:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int n, x;
 
     int n, x;
  
Строка 50: Строка 52:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int n, x;
 
     int n, x;
  
Строка 59: Строка 62:
 
     scanf_s("%d", &n);
 
     scanf_s("%d", &n);
  
     int c = 0;
+
     int count = 0;
 
     for(int i = 1; i <= n; i++) {
 
     for(int i = 1; i <= n; i++) {
 
         printf("Введите целое число:");
 
         printf("Введите целое число:");
 
         scanf_s("%d", &x);
 
         scanf_s("%d", &x);
 
         if (x % 2 != 0) {
 
         if (x % 2 != 0) {
             c++;
+
             count++;
 
         }
 
         }
 
     }
 
     }
  
     printf("Количество нечетных равно %d", c);
+
     printf("Количество нечетных равно %d", count);
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Строка 75: Строка 78:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     float x;
 
     float x;
  
Строка 94: Строка 98:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int x = 2;
 
     int x = 2;
  
Строка 110: Строка 115:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int x = 10;
 
     int x = 10;
  
Строка 126: Строка 132:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int n, a, b;
 
     int n, a, b;
  
Строка 135: Строка 142:
 
     scanf_s("%d", &n);
 
     scanf_s("%d", &n);
  
     a = 1; b = 1;
+
     a = 0; b = 1;
 
     printf("%d %d ", a, b);
 
     printf("%d %d ", a, b);
 
     for (int i = 1; i <= n - 2; ++i) {
 
     for (int i = 1; i <= n - 2; ++i) {
Строка 149: Строка 156:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int a, b;
 
     int a, b;
  
Строка 173: Строка 181:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int m;
 
     int m;
  
Строка 182: Строка 191:
 
     scanf_s("%d", &m);
 
     scanf_s("%d", &m);
  
     int s = 0;
+
     int sum = 0;
 
     int m1 = abs(m);
 
     int m1 = abs(m);
  
 
     while (m1 > 0) {
 
     while (m1 > 0) {
         s += m1 % 10;
+
         sum += m1 % 10;
         m1 = m1 / 10;
+
         m1 /= 10;
 
     }
 
     }
  
     printf("Сумма цифр числа %d равна %d", m, s);
+
     printf("Сумма цифр числа %d равна %d", m, sum);
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Строка 197: Строка 206:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int n, x;
 
     int n, x;
  
     printf("Введите целое число n (n>0):");
+
     printf("Введите количество чисел:");
 
     scanf_s("%d", &n);
 
     scanf_s("%d", &n);
  
Строка 225: Строка 235:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
 
#include <stdbool.h>
 
#include <stdbool.h>
 
#include <float.h>
 
#include <float.h>
Строка 234: Строка 244:
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int n, x;
 
     int n, x;
  
Строка 262: Строка 273:
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 
#include <stdio.h>
 
#include <stdio.h>
#include <windows.h>
+
#include <Windows.h>
 
#include <stdbool.h>
 
#include <stdbool.h>
  
 
int main() {
 
int main() {
     SetConsoleOutputCP(CP_UTF8);
+
    SetConsoleCP(1251);
 +
     SetConsoleOutputCP(1251);
 
     int x, n, k;
 
     int x, n, k;
  
Строка 295: Строка 307:
 
=== Является ли число N>1 простым? ===
 
=== Является ли число N>1 простым? ===
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 +
#include <stdio.h>
 +
#include <Windows.h>
 +
#include <stdbool.h>
 +
#include <math.h>
 +
 +
int main() {
 +
    SetConsoleCP(1251);
 +
    SetConsoleOutputCP(1251);
 +
    int n;
 +
 +
    printf("Введите целое число n (n > 1):");
 +
    scanf_s("%d", &n);
 +
 +
    bool isPrime = true;
 +
    for (int i = 2; i <= round(sqrt(n)); i++) {
 +
        if (n % i == 0) {
 +
            isPrime = false;
 +
            break;
 +
        }
 +
    }
  
 +
    if (isPrime) {
 +
        printf("Число %d является простым", n);
 +
    } else {
 +
        printf("Число %d является составным", n);
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=== Разложение числа на простые множители ===
 
=== Разложение числа на простые множители ===
 
<syntaxhighlight lang="c" line>
 
<syntaxhighlight lang="c" line>
 +
#include <stdio.h>
 +
#include <Windows.h>
  
 +
int main() {
 +
    SetConsoleCP(1251);
 +
    SetConsoleOutputCP(1251);
 +
    int x;
 +
 +
    printf("Введите целое число x (x > 1):");
 +
    scanf_s("%d", &x);
 +
 +
    int i = 2;
 +
    printf("%d = 1", x);
 +
    do {
 +
        if (x % i == 0) {
 +
            printf(" * %d", i);
 +
            x = x / i;
 +
        } else {
 +
            i++;
 +
        }
 +
    } while (x != 1);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Текущая версия на 15:42, 20 июня 2022

Код программ

Сумма вводимых целых чисел

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int n, x;
 8 
 9     printf("Введите число слагаемых:");
10     scanf_s("%d", &n);
11 
12     int s = 0;
13     for(int i = 1; i <= n; i++) {
14         printf("Введите слагаемое №%d:", i);
15         scanf_s("%d", &x);
16         s += x;
17     }
18 
19     printf("Сумма равна %d", s);
20 }

Произведение целых чисел

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int n, x;
 8 
 9     printf("Введите число множителей:");
10     scanf_s("%d", &n);
11 
12     int p = 1;
13     for(int i = 1; i <= n; i++) {
14         printf("Введите множитель №%d:", i);
15         scanf_s("%d", &x);
16         p *= x;
17     }
18 
19     printf("Произведение равно %d", p);
20 }

Сколько нечетных среди n введенных

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int n, x;
 8 
 9     printf("Введите n:");
10     scanf_s("%d", &n);
11 
12     int count = 0;
13     for(int i = 1; i <= n; i++) {
14         printf("Введите целое число:");
15         scanf_s("%d", &x);
16         if (x % 2 != 0) {
17             count++;
18         }
19     }
20 
21     printf("Количество нечетных равно %d", count);
22 }

Защита от неверного ввода

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     float x;
 8 
 9     do {
10         printf("Введите x>0:");
11         scanf_s("%f", &x);
12         if (x <= 0) {
13             printf("Неверный ввод\n");
14         }
15     } while (x <= 0);
16 }

Вывод 10 первых степеней двойки

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int x = 2;
 8 
 9     for (int i = 1; i <= 10; i++) {
10         printf("%3d %5d\n", i, x);
11         x *= 2;
12     }
13 }

Вывод всех двухзначных чисел, кратных 5

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int x = 10;
 8 
 9     while (x < 100) {
10         printf("%3d\n", x);
11         x += 5;
12     }
13 }

Вывод n первых чисел Фибоначчи

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int n, a, b;
 8 
 9     printf("Введите целое число n (n > 1):");
10     scanf_s("%d", &n);
11 
12     a = 0; b = 1;
13     printf("%d %d ", a, b);
14     for (int i = 1; i <= n - 2; ++i) {
15         int tmp = a;
16         a = b;
17         b += tmp;
18         printf("%d ", b);
19     }
20 }

Найти НОД(A,B), используя алгоритм Евклида:

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int a, b;
 8 
 9     printf("Введите целое число a:");
10     scanf_s("%d", &a);
11     printf("Введите целое число b:");
12     scanf_s("%d", &b);
13 
14     while (b != 0) {
15         int tmp = a;
16         a = b;
17         b = tmp % b;
18     }
19 
20     printf("НОД(A, B) = %d", a);
21 }

Найти сумму цифр целого числа m

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int m;
 8 
 9     printf("Введите целое число m:");
10     scanf_s("%d", &m);
11 
12     int sum = 0;
13     int m1 = abs(m);
14 
15     while (m1 > 0) {
16         sum += m1 % 10;
17         m1 /= 10;
18     }
19 
20     printf("Сумма цифр числа %d равна %d", m, sum);
21 }

Найти max из введенных чисел

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int n, x;
 8 
 9     printf("Введите количество чисел:");
10     scanf_s("%d", &n);
11 
12     printf("Введите число 1:");
13     scanf_s("%d", &x);
14     int max = x;
15 
16     for (int i = 2; i <= n; i++) {
17         printf("Введите число %d:", i);
18         scanf_s("%d", &x);
19         if (x > max) {
20             max = x;
21         }
22     }
23 
24     printf("Максимальное из введенных чисел: %d", max);
25 }

Найти min, удовлетворяющее условию p(x)

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 #include <stdbool.h>
 4 #include <float.h>
 5 
 6 bool p(double x) {
 7     return x > 0;
 8 }
 9 
10 int main() {
11     SetConsoleCP(1251);
12     SetConsoleOutputCP(1251);
13     int n, x;
14 
15     printf("Введите целое число n (n > 0):");
16     scanf_s("%d", &n);
17 
18     bool exists = false;
19     double min = DBL_MAX;
20     for (int i = 0; i < n; ++i) {
21         printf("Введите %d число:", i+1);
22         scanf_s("%d", &x);
23         if ((x < min) && p(x)) {
24             min = x;
25             exists = true;
26         }
27     }
28 
29     if (exists) {
30         printf("Минимальное из введенных чисел, удовлетворяющих условию: %f", min);
31     } else {
32         printf("Нет чисел, удовлетворяющих условию");
33     }
34 }

Есть ли среди введенных число k?

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 #include <stdbool.h>
 4 
 5 int main() {
 6     SetConsoleCP(1251);
 7     SetConsoleOutputCP(1251);
 8     int x, n, k;
 9 
10     printf("Введите целое число n (n > 0):");
11     scanf_s("%d", &n);
12 
13     printf("Введите целое число k:");
14     scanf_s("%d", &k);
15 
16     bool exists = false;
17     for (int i = 0; i < n; i++) {
18         printf("Введите %d число:", i+1);
19         scanf_s("%d", &x);
20         if (x == k) {
21             exists = true;
22             break;
23         }
24     }
25 
26     if (exists) {
27         printf("Число %d было введено", k);
28     } else {
29         printf("Число %d не было введено", k);
30     }
31 }

Является ли число N>1 простым?

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 #include <stdbool.h>
 4 #include <math.h>
 5 
 6 int main() {
 7     SetConsoleCP(1251);
 8     SetConsoleOutputCP(1251);
 9     int n;
10 
11     printf("Введите целое число n (n > 1):");
12     scanf_s("%d", &n);
13 
14     bool isPrime = true;
15     for (int i = 2; i <= round(sqrt(n)); i++) {
16         if (n % i == 0) {
17             isPrime = false;
18             break;
19         }
20     }
21 
22     if (isPrime) {
23         printf("Число %d является простым", n);
24     } else {
25         printf("Число %d является составным", n);
26     }
27 }

Разложение числа на простые множители

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 int main() {
 5     SetConsoleCP(1251);
 6     SetConsoleOutputCP(1251);
 7     int x;
 8 
 9     printf("Введите целое число x (x > 1):");
10     scanf_s("%d", &x);
11 
12     int i = 2;
13     printf("%d = 1", x);
14     do {
15         if (x % i == 0) {
16             printf(" * %d", i);
17             x = x / i;
18         } else {
19             i++;
20         }
21     } while (x != 1);
22 }