Advertisement
Pavle_nis

Untitled

Mar 27th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.58 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 ConsoleApp58
  10. {
  11.     class Program
  12.     {
  13.         static List<string> numbersCombinations = new List<string>();
  14.         static List<string> operatorsCombinations = new List<string>();
  15.         static int target = 0;
  16.         static void Main(string[] args)
  17.         {
  18.             var watch = System.Diagnostics.Stopwatch.StartNew();
  19.  
  20.             List<string> nums = new List<string>() { "1", "3", "3", "7", "1", "8" };
  21.             List<string> nums1 = new List<string>() { "0", "1", "2", "3", "4", "5" };
  22.             List<string> operators = new List<string>() { "+", "-", "*", "/" };
  23.             target = 999;
  24.  
  25.             for (int i = 2; i < 7; i++)
  26.             {
  27.                 getCombinations(numbersCombinations, nums1, "", 0, i);
  28.             }
  29.             for (int i = 1; i < 6; i++)
  30.             {
  31.                 getCombinations(operatorsCombinations, operators, "", 0, i);
  32.             }
  33.  
  34.             generate(nums);
  35.  
  36.             string resenje = "";
  37.             int minval = Int32.MaxValue;
  38.  
  39.             //resenja = resenja.Distinct().ToList();
  40.  
  41.             foreach (var x in resenja)
  42.             {
  43.                 int k1 = int.Parse(x.Substring(x.IndexOf("=") + 1));
  44.                 if (k1 > 0 && Math.Abs(k1 - target) < minval)
  45.                 {
  46.                     minval = Math.Abs(k1 - target);
  47.                     resenje = x;
  48.                 }
  49.             }
  50.  
  51.             watch.Stop();
  52.             var elapsedMs = watch.ElapsedMilliseconds;
  53.             Console.WriteLine(elapsedMs);
  54.             Console.WriteLine(resenje);
  55.  
  56.             Console.ReadKey();
  57.         }
  58.  
  59.         private static void generate(List<string> nums)
  60.         {
  61.             int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0;
  62.             StringBuilder expression = new StringBuilder();
  63.             int s = 0, s1 = 0, s2 = 0, s3 = 0, s4 = 0;
  64.  
  65.             for (int i = 0; i < operatorsCombinations.Count; i++)
  66.             {
  67.                 string operator1 = operatorsCombinations[i];
  68.  
  69.                 for (int j = 0; j < numbersCombinations.Count; j++)
  70.                 {
  71.                     expression.Clear();
  72.                     x1 = Convert.ToInt32(numbersCombinations[j].Substring(0, 1));
  73.                     x2 = Convert.ToInt32(numbersCombinations[j].Substring(1, 1));
  74.  
  75.                     if (operator1.Length == 1)
  76.                     {
  77.                         if (j == 30)
  78.                         {
  79.                             break;
  80.                         }
  81.  
  82.                         expression.Append(nums[x1]).Append(operator1[0]).Append(nums[x2]);
  83.  
  84.                         s = calculateExpression(operator1[0].ToString(), int.Parse(nums[x1]), int.Parse(nums[x2]));
  85.  
  86.                         if(checkExpression(expression.ToString(), s))
  87.                         {
  88.                             return;
  89.                         }
  90.                     }
  91.                     else if (operator1.Length == 2)
  92.                     {
  93.                         if (j == 0)
  94.                         {
  95.                             j = 30;
  96.                         }
  97.                         else if (j == 150)
  98.                         {
  99.                             break;
  100.                         }
  101.  
  102.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  103.  
  104.                         expression.Append("((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2]).Append(")").Append(operator1[1]).Append(nums[x3]).Append(")");
  105.  
  106.                         s = calculateExpression(operator1[1].ToString(), calculateExpression(operator1[0].ToString(), int.Parse(nums[x1]), int.Parse(nums[x2])), int.Parse(nums[x3]));
  107.  
  108.                         if (checkExpression(expression.ToString(), s))
  109.                         {
  110.                             return;
  111.                         }
  112.                     }
  113.                     else if (operator1.Length == 3)
  114.                     {
  115.                         if (j == 0)
  116.                         {
  117.                             j = 150;
  118.                         }
  119.                         else if (j == 510)
  120.                         {
  121.                             break;
  122.                         }
  123.  
  124.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  125.  
  126.                         x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
  127.  
  128.                         expression.Append("(((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
  129.                             .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
  130.                             .Append(nums[x4]).Append(")");
  131.  
  132.                         s1 = calculateExpression(operator1[0].ToString(), int.Parse(nums[x1]), int.Parse(nums[x2]));
  133.                         s2 = calculateExpression(operator1[1].ToString(), s1, int.Parse(nums[x3]));
  134.                         s = calculateExpression(operator1[2].ToString(), s2, int.Parse(nums[x4]));
  135.  
  136.                         if (checkExpression(expression.ToString(), s))
  137.                         {
  138.                             return;
  139.                         }
  140.                     }
  141.                     else if (operator1.Length == 4)
  142.                     {
  143.                         if (j == 0)
  144.                         {
  145.                             j = 510;
  146.                         }
  147.                         else if (j == 1230)
  148.                         {
  149.                             break;
  150.                         }
  151.  
  152.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  153.  
  154.                         x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
  155.  
  156.                         x5 = Convert.ToInt32(numbersCombinations[j].Substring(4, 1));
  157.  
  158.                         expression.Append("((((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
  159.                             .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
  160.                             .Append(nums[x4]).Append(")").Append(operator1[3]).Append(nums[x5]).Append(")");
  161.  
  162.                         s1 = calculateExpression(operator1[0].ToString(), int.Parse(nums[x1]), int.Parse(nums[x2]));
  163.                         s2 = calculateExpression(operator1[1].ToString(), s1, int.Parse(nums[x3]));
  164.                         s3 = calculateExpression(operator1[2].ToString(), s2, int.Parse(nums[x4]));
  165.                         s = calculateExpression(operator1[3].ToString(), s3, int.Parse(nums[x5]));
  166.  
  167.                         if (checkExpression(expression.ToString(), s))
  168.                         {
  169.                             return;
  170.                         }
  171.                     }
  172.                     else if (operator1.Length == 5)
  173.                     {
  174.                         if (j == 0)
  175.                         {
  176.                             j = 1230;
  177.                         }
  178.  
  179.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  180.  
  181.                         x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
  182.  
  183.                         x5 = Convert.ToInt32(numbersCombinations[j].Substring(4, 1));
  184.  
  185.                         x6 = Convert.ToInt32(numbersCombinations[j].Substring(5, 1));
  186.  
  187.                         expression.Append("(((((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
  188.                             .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
  189.                             .Append(nums[x4]).Append(")").Append(operator1[3]).Append(nums[x5]).Append(")").Append(operator1[4]).Append(nums[x6]).Append(")");
  190.  
  191.                         s1 = calculateExpression(operator1[0].ToString(), int.Parse(nums[x1]), int.Parse(nums[x2]));
  192.                         s2 = calculateExpression(operator1[1].ToString(), s1, int.Parse(nums[x3]));
  193.                         s3 = calculateExpression(operator1[2].ToString(), s2, int.Parse(nums[x4]));
  194.                         s4 = calculateExpression(operator1[3].ToString(), s3, int.Parse(nums[x5]));
  195.                         s = calculateExpression(operator1[4].ToString(), s4, int.Parse(nums[x6]));
  196.  
  197.                         if (checkExpression(expression.ToString(), s))
  198.                         {
  199.                             return;
  200.                         }
  201.                     }
  202.                 }
  203.             }
  204.         }
  205.         static List<string> resenja = new List<string>();
  206.         private static bool checkExpression(string expression, int value)
  207.         {
  208.             if (value > 0)
  209.             {
  210.                 if (value == target)
  211.                 {
  212.                     resenja.Add(expression + "=" + value.ToString());
  213.  
  214.                     return true;
  215.                 }
  216.                 else if (value >= target - 100 && value <= target + 100)
  217.                 {
  218.                     if (!resenja.Any(s => (expression + "=" + value.ToString()).Contains(s)))
  219.                     {
  220.                         resenja.Add(expression + "=" + value.ToString());
  221.                     }
  222.  
  223.                     return false;
  224.                 }
  225.             }
  226.  
  227.             return false;
  228.         }
  229.         private static int calculateExpression(string operator1, int num1, int num2)
  230.         {
  231.             switch (operator1)
  232.             {
  233.                 case "+":
  234.                     return num1 + num2;
  235.                 case "-":
  236.                     return num1 - num2;
  237.                 case "*":
  238.                     return num1 * num2;
  239.                 case "/":
  240.                     return num1 / num2;
  241.             }
  242.  
  243.             return 0;
  244.         }
  245.  
  246.         public static void getCombinations(List<string> combinations, List<string> input, String comb, int pos, int length)
  247.         {
  248.             if (pos < length)
  249.             {
  250.                 foreach (string ch in input)
  251.                 {
  252.                     getCombinations(combinations, input, comb + ch, pos + 1, length);
  253.                 }
  254.             }
  255.             else
  256.             {
  257.                 if (!comb.Contains("+") && !comb.Contains("-") && !comb.Contains("*") && !comb.Contains("/"))
  258.                 {
  259.                     if (CheckForDupicate(comb))
  260.                     {
  261.                         combinations.Add(comb);
  262.                     }
  263.                 }
  264.                 else
  265.                 {
  266.                     combinations.Add(comb);
  267.                 }
  268.             }
  269.         }
  270.         public static bool CheckForDupicate(string input)
  271.         {
  272.             return input.Distinct().Count() == input.Length;
  273.         }
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement