Created
March 18, 2019 17:04
-
-
Save igorLisovitskiy/6eba426b3efc27d50b2539d80b4ca334 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApp5 | |
{ | |
class Program | |
{ | |
public static void Task1() { | |
//Найти сумму чисел от 100 до 200 кратных 17 | |
int sum = 0; | |
for (int i = 100; i <= 200; i++) | |
{ | |
if (i % 17 == 0) | |
{ | |
sum += i; | |
} | |
} | |
Console.WriteLine($"Cуммa чисел от 100 до 200 кратных 17: {sum}"); | |
} | |
public static void Task2() | |
{ | |
//Пользователь вводит с клавиатуры три числа n1, n2, n3. Найти максимальное число | |
Console.WriteLine("Enter 3 numbers, I will find max"); | |
int first = Convert.ToInt32(Console.ReadLine()); | |
int second = Convert.ToInt32(Console.ReadLine()); | |
int third = Convert.ToInt32(Console.ReadLine()); | |
if (first > second && first > third) | |
{ | |
Console.WriteLine($"Max is {first}"); | |
} | |
else if (second > first) | |
{ | |
Console.WriteLine($"Max is {second}"); | |
} | |
else{ | |
Console.WriteLine($"Max is {third}"); | |
} | |
} | |
public static void Task3() { | |
/* | |
* 3) Пользователь вводит с клавиатуры натуральное число n произвольной длинны. Найдите количество нечетных цифр этого числа | |
*/ | |
Console.WriteLine("Enter n number, I will count all odd numbers in it"); | |
int input = Convert.ToInt32(Console.ReadLine()); | |
int oddCount = 0; | |
int check; | |
while (input > 0) { | |
check = input % 10; | |
if (check % 2 != 0) { | |
oddCount++; | |
} | |
input /= 10; | |
} | |
Console.WriteLine($"Odd: {oddCount}"); | |
} | |
public static void Task4() | |
{ | |
/* | |
4) Пользователь вводит с клавиатуры размерность одномерного массива n и заполняет его элементами. Найти и вывести на экран два наибольших элемента этого массива. (Значения элементов массива могут повторятся). | |
Примеры: | |
В массиве [1, 7, 6, 4, 9] два максимальных элемента — это числа 9 и 7. | |
В массиве [5, 1, 2, 9, 9] два максимальных элемента — это числа 9 и 9. | |
*/ | |
Console.WriteLine("Enter the length of array"); | |
int arrLength = Convert.ToInt32(Console.ReadLine()); | |
int[] arr = new int[arrLength]; | |
Console.WriteLine($"Enter {arrLength} numbers"); | |
int index = arrLength - 1; | |
while (index >= 0) { | |
arr[index] = Convert.ToInt32(Console.ReadLine()); | |
index--; | |
} | |
int firstMax = 0, secondMax = 0; | |
for (int i = 0; i < arr.Length; i++) | |
{ | |
if (arr[i] > firstMax) { | |
firstMax = arr[i]; | |
}else if (arr[i] > secondMax) | |
{ | |
secondMax = arr[i]; | |
} | |
} | |
Console.WriteLine($"Max values are: {firstMax}, {secondMax}"); | |
} | |
public static void Task5() { | |
/* | |
5) Пользователь вводит с клавиатуры размерность двумерного массива arr (n – число строк и m – число столбцов) и заполняет его элементами. Создать 2 одномерных массива arr1[n] и arr2[m]. Заполнить массив arr1 максимальными значениями элементов каждой строки массива arr. | |
Заполнить массив arr2 минимальными значениями элементов каждого столбца массива arr. | |
Вывести элементы массивов arr1 и arr2 на экран. | |
*/ | |
Console.WriteLine("Enter the number of rows a 2d matrix"); | |
int rows = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine("Enter the number of columns a 2d matrix"); | |
int cols = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine("Enter the values of your 2d matrix"); | |
int index = cols - 1; | |
int[,] matrix = new int[rows,cols]; | |
for (int i = 0; i <= matrix.GetUpperBound(0); i++) | |
{ | |
for (int j = 0; j <= matrix.GetUpperBound(1); j++) | |
{ | |
Console.WriteLine("Enter the value:"); | |
matrix[i, j] = Convert.ToInt32(Console.ReadLine()); | |
} | |
} | |
Console.WriteLine("Матрица"); | |
for (int i = 0; i <= matrix.GetUpperBound(0); i++) | |
{ | |
for (int j = 0; j <= matrix.GetUpperBound(1); j++) | |
{ | |
Console.Write(matrix[i, j] + "\t"); | |
} | |
Console.WriteLine(); | |
} | |
int[] arr1 = new int[rows]; | |
int[] arr2 = new int[cols]; | |
for (int i = 0; i <= matrix.GetUpperBound(0); i++) | |
{ | |
int maxInRow = 0; | |
for (int j = 0; j <= matrix.GetUpperBound(1); j++) | |
{ | |
if (matrix[i, j] > maxInRow) | |
{ | |
maxInRow = matrix[i, j]; | |
} | |
if (arr2[j] != 0 && arr2[j] < matrix[i, j]) | |
{ | |
arr2[j] = matrix[i, j]; | |
} | |
else { | |
arr2[j] = matrix[i, j]; | |
} | |
} | |
arr1[i] = maxInRow; | |
} | |
Console.WriteLine($"Максимальные значения элементов каждой строки : {string.Join(", ", arr1)}"); | |
Console.WriteLine($"Минимальные значения элементов каждого столбца : {string.Join(", ", arr2)}"); | |
} | |
static void Main(string[] args) | |
{ | |
/* | |
1. .Net фреймворк, предоставляющая среду выполнения и библиотеки классов для языков программирования вроде C#. | |
2. Программа на C# это класс в файле с расширением .cs, с точкой входа в виде метода Main. | |
3. Переменные именованные области памяти которые хранят данные определенного типа. В C# по конвенции свойства классов именуются в PascalCase, локальные в camelCase | |
4. Виды циклов while, for, foreach, do while. | |
while выполняется если условие указанное в скобках истинно | |
while(true){ | |
WriteLine("Infinite Loop"); | |
} | |
do while выполняется хотя бы один раз, затем проверяется условие в скобках | |
do{ | |
WriteLine("Will be executed once!"); | |
}while(false); | |
for содержит три конструкции инициализация, условие, итерация. | |
for(;;){ | |
WriteLine("Infinite Loop"); | |
} | |
foreachвыполнет последовательные перебор элементов в коллекици | |
foreach (var item in collection) | |
{ | |
} | |
5) операция инкремента выполняет увеличение значения переменной на 1. Префиксная отличается от постфиксной тем что вначале число увеличивается на 1, затем используется в выражении, а в постфиксной значение вначале используется а затем величивается на 1.\ | |
*/ | |
// Задачи | |
Task5(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment