Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _3element
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int result1 = WorkWithFile("A.txt");
- int result2 = WorkWithFile("B.txt");
- Console.WriteLine("Файл A: " + result1);
- Console.WriteLine("Файл В: " + result2);
- Console.ReadKey();
- }
- public static int WorkWithFile(string filename) //Метод чтения из файла
- {
- using (var sr = new StreamReader(filename))
- {
- int[] array;
- int N = int.Parse(sr.ReadLine());
- array = new int[N];
- for (int i = 0; i < N; i++) //Читаем весь массив
- {
- array[i] = int.Parse(sr.ReadLine());
- }
- return Calculate(array);
- }
- }
- public static int Calculate(int[] array) //Метод подсчёта ответа
- {
- int minsum = int.MaxValue; //Минимальная сумма изначально должна быть максимальным числом
- for (int i = 0; i < array.Length; i++) //Идем тремя формами по всем элементам массива, перебирая все возможные варианты сумм
- {
- int tempsum = 0;
- for (int j = i+1; j < array.Length; j++)
- {
- for (int k = j+1; k < array.Length; k++)
- {
- tempsum = array[i] + array[j] + array[k]; //варианты сумм
- if(tempsum % 3 == 0) //Если сумма удовлетворяет условию делимости на 3 и..
- {
- if(tempsum < minsum) // ..сумма меньше минимальной суммы
- {
- minsum = tempsum; //запоминаем минимальную сумму
- }
- }
- tempsum = 0;
- }
- }
- }
- return minsum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement