Created
May 21, 2022 14:51
-
-
Save gmakc-094423/cecef22e126fec9c0eb597fe09d87065 to your computer and use it in GitHub Desktop.
Домашнее задание к 9 семинару
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 64: Задайте значение N. Напишите программу, которая выведет все натуральные числа в промежутке от N до 1. | |
// N = 5 -> "5, 4, 3, 2, 1" | |
// N = 8 -> "8, 7, 6, 5, 4, 3, 2, 1" | |
Console.Clear(); | |
Console.WriteLine($"Задача 64: Задайте значение N. Напишите программу, которая выведет все натуральные числа в промежутке от N до 1."); | |
int n = InputNumbers("Введите n: "); | |
int count = 2; | |
PrintNumber(n, count); | |
Console.Write(1); | |
void PrintNumber(int n, int count) | |
{ | |
if (count > n) return; | |
PrintNumber(n, count + 1); | |
Console.Write(count + ", "); | |
} | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 66: Задайте значения M и N. Напишите программу, которая найдёт сумму натуральных элементов в промежутке от M до N. | |
// M = 1; N = 15 -> 120 | |
// M = 4; N = 8. -> 30 | |
Console.Clear(); | |
Console.WriteLine($"Задача 66: Задайте значения M и N. Напишите программу, которая найдёт сумму натуральных элементов в промежутке от M до N"); | |
int m = InputNumbers("Введите m: "); | |
int n = InputNumbers("Введите n: "); | |
int temp = m; | |
if (m > n) | |
{ | |
m = n; | |
n = temp; | |
} | |
PrintSumm(m, n, temp=0); | |
void PrintSumm(int m, int n, int summ) | |
{ | |
summ = summ + n; | |
if (n <= m) | |
{ | |
Console.Write($"Сумма элементов= {summ} "); | |
return; | |
} | |
PrintSumm(m, n - 1, summ); | |
} | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Задача 68: Напишите программу вычисления функции Аккермана с помощью рекурсии. Даны два неотрицательных числа m и n. | |
// m = 2, n = 3 -> A(m,n) = 9 | |
Console.Clear(); | |
Console.WriteLine($"Задача 68: Напишите программу вычисления функции Аккермана с помощью рекурсии. Даны два неотрицательных числа m и n."); | |
int m = InputNumbers("Введите m: "); | |
int n = InputNumbers("Введите n: "); | |
int functionAkkerman = Ack(m, n); | |
Console.Write($"Функция Аккермана = {functionAkkerman} "); | |
int Ack(int m, int n) | |
{ | |
if (m == 0) return n + 1; | |
else if (n == 0) return Ack(m - 1, 1); | |
else return Ack(m - 1, Ack(m, n - 1)); | |
} | |
int InputNumbers(string input) | |
{ | |
Console.Write(input); | |
int output = Convert.ToInt32(Console.ReadLine()); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment