Pavle_nis

Untitled

Mar 20th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.Data;
  8.  
  9. namespace ConsoleApp51
  10. {
  11.     class Program
  12.     {
  13.         static int target = 0;
  14.         static void Main(string[] args)
  15.         {
  16.             List<int> nums = new List<int>() { 1, 2, 3, 4, 5, 6 };
  17.             target = 999;
  18.  
  19.             generate(nums, 2);
  20.             generate(nums, 3);
  21.             generate(nums, 4);
  22.             generate(nums, 5);
  23.             generate(nums, 6);
  24.         }
  25.  
  26.         static List<string> list1 = new List<string>();
  27.         static List<string> list2 = new List<string>();
  28.         static List<string> list3 = new List<string>();
  29.         static List<string> list4 = new List<string>();
  30.         static List<string> list5 = new List<string>();
  31.         static List<string> list6 = new List<string>();
  32.         private static void generate(List<int> nums, int l)
  33.         {
  34.             for (int i = 0; i < nums.Count; i++)
  35.             {
  36.                 for (int j = i + 1; j < nums.Count; j++)
  37.                 {
  38.                     if (l == 2)
  39.                     {
  40.                         list1.Clear();
  41.                         list1.Add(nums[i].ToString());
  42.                         list1.Add(nums[j].ToString());
  43.  
  44.                         list2.Add(add(list1));
  45.  
  46.                         list2.Add(sub(list1)[0]);
  47.                         list2.Add(sub(list1)[1]);
  48.  
  49.                         list2.Add(mul(list1));
  50.  
  51.                         list2.Add(div(list1)[0]);
  52.                         list2.Add(div(list1)[1]);
  53.                     }
  54.                     else if (l == 3)
  55.                     {
  56.                         addToList(list2, list3, nums);
  57.                     }
  58.                     else if (l == 4)
  59.                     {
  60.                         addToList(list3, list4, nums);
  61.                     }
  62.                     else if (l == 5)
  63.                     {
  64.                         addToList(list4, list5, nums);
  65.                     }
  66.                     else if (l == 6)
  67.                     {
  68.                         addToList(list5, list6, nums);
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.  
  74.         private static string add(List<string> list)
  75.         {
  76.             return "(" + list[0] + "+" + list[1] + ")";
  77.         }
  78.         private static List<string> sub(List<string> list)
  79.         {
  80.             List<string> subList = new List<string>();
  81.  
  82.             subList.Add("(" + list[0] + "-" + list[1] + ")");
  83.             subList.Add("(" + list[1] + "-" + list[0] + ")");
  84.  
  85.             return subList;
  86.         }
  87.         private static string mul(List<string> list)
  88.         {
  89.             return "(" + list[0] + "*" + list[1] + ")";
  90.         }
  91.         private static List<string> div(List<string> list)
  92.         {
  93.             List<string> divList = new List<string>();
  94.  
  95.             divList.Add("(" + list[0] + "/" + list[1] + ")");
  96.             divList.Add("(" + list[1] + "/" + list[0] + ")");
  97.  
  98.             return divList;
  99.         }
  100.         private static void addToList(List<string> lista1, List<string> lista2, List<int> nums)
  101.         {
  102.             List<string> lista3 = new List<string>();
  103.             foreach (string x in lista1)
  104.             {
  105.                 for (int k = 0; k < nums.Count; k++)
  106.                 {
  107.                     if (x.Contains(nums[k].ToString()))
  108.                     {
  109.                         continue;
  110.                     }
  111.                     else
  112.                     {
  113.                         lista3.Clear();
  114.                         lista3.Add(nums[k].ToString());
  115.                         lista3.Add(x);
  116.  
  117.                         lista2.Add(add(lista3));
  118.  
  119.                         lista2.Add(sub(lista3)[0]);
  120.                         lista2.Add(sub(lista3)[1]);
  121.  
  122.                         lista2.Add(mul(lista3));
  123.  
  124.                         lista2.Add(div(lista3)[0]);
  125.                         lista2.Add(div(lista3)[1]);
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment