Advertisement
Pavle_nis

Untitled

Mar 25th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.66 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.             List<string> nums = new List<string>() { "1", "3", "3", "7", "1", "8" };
  19.             List<string> nums1 = new List<string>() { "0", "1", "2", "3", "4", "5" };
  20.             List<string> operators = new List<string>() { "+", "-", "*", "/" };
  21.             target = 999;
  22.  
  23.             for (int i = 2; i < 7; i++)
  24.             {
  25.                 getCombinations(numbersCombinations, nums1, "", 0, i);
  26.             }
  27.             for (int i = 1; i < 6; i++)
  28.             {
  29.                 getCombinations(operatorsCombinations, operators, "", 0, i);
  30.             }
  31.  
  32.             foreach (var x in numbersCombinations)
  33.                 Console.WriteLine(x);
  34.  
  35.             int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0;
  36.             StringBuilder expression = new StringBuilder();
  37.  
  38.             for (int i = 0; i < operatorsCombinations.Count; i++)
  39.             {
  40.                 string operator1 = operatorsCombinations[i];
  41.  
  42.                 for (int j = 0; j < numbersCombinations.Count; j++)
  43.                 {
  44.                     expression.Clear();
  45.                     x1 = Convert.ToInt32(numbersCombinations[j].Substring(0, 1));
  46.                     x2 = Convert.ToInt32(numbersCombinations[j].Substring(1, 1));
  47.  
  48.                     if (operatorsCombinations[i].Length == 1)
  49.                     {
  50.                         if (j == 30)
  51.                         {
  52.                             break;
  53.                         }
  54.  
  55.                         expression.Append(nums[x1]).Append(operator1[0]).Append(nums[x2]);
  56.  
  57.                         calculateExpression(expression.ToString());
  58.                     }
  59.                     else if (operatorsCombinations[i].Length == 2)
  60.                     {
  61.                         if (j == 0)
  62.                         {
  63.                             j = 30;
  64.                         }
  65.                         else if (j == 150)
  66.                         {
  67.                             break;
  68.                         }
  69.  
  70.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  71.  
  72.                         expression.Append("((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2]).Append(")").Append(operator1[1]).Append(nums[x3]).Append(")");
  73.                         calculateExpression(expression.ToString());
  74.                     }
  75.                     else if (operatorsCombinations[i].Length == 3)
  76.                     {
  77.                         if (j == 0)
  78.                         {
  79.                             j = 150;
  80.                         }
  81.                         else if (j == 510)
  82.                         {
  83.                             break;
  84.                         }
  85.  
  86.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  87.  
  88.                         x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
  89.  
  90.                         expression.Append("(((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
  91.                             .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
  92.                             .Append(nums[x4]).Append(")");
  93.  
  94.                         calculateExpression(expression.ToString());
  95.                     }
  96.                     else if (operatorsCombinations[i].Length == 4)
  97.                     {
  98.                         if (j == 0)
  99.                         {
  100.                             j = 510;
  101.                         }
  102.                         else if (j == 1230)
  103.                         {
  104.                             break;
  105.                         }
  106.  
  107.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  108.  
  109.                         x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
  110.  
  111.                         x5 = Convert.ToInt32(numbersCombinations[j].Substring(4, 1));
  112.  
  113.                         expression.Append("((((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
  114.                             .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
  115.                             .Append(nums[x4]).Append(")").Append(operator1[3]).Append(nums[x5]).Append(")");
  116.  
  117.                         calculateExpression(expression.ToString());
  118.                     }
  119.                     else if (operatorsCombinations[i].Length == 5)
  120.                     {
  121.                         if (j == 0)
  122.                         {
  123.                             j = 1230;
  124.                         }
  125.  
  126.                         x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
  127.  
  128.                         x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
  129.  
  130.                         x5 = Convert.ToInt32(numbersCombinations[j].Substring(4, 1));
  131.  
  132.                         x6 = Convert.ToInt32(numbersCombinations[j].Substring(5, 1));
  133.  
  134.                         expression.Append("(((((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
  135.                             .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
  136.                             .Append(nums[x4]).Append(")").Append(operator1[3]).Append(nums[x5]).Append(")").Append(operator1[4]).Append(nums[x6]).Append(")");
  137.  
  138.                         calculateExpression(expression.ToString());
  139.                     }
  140.                 }
  141.             }
  142.  
  143.             string resenje = "";
  144.             int minval = Int32.MaxValue;
  145.  
  146.             foreach (var x in resenja)
  147.             {
  148.                 int k1 = int.Parse(x.Substring(x.IndexOf("=") + 1, x.Length - x.IndexOf("=") - 1));
  149.                 if (k1 > 0 && Math.Abs(k1 - target) < minval)
  150.                 {
  151.                     minval = Math.Abs(k1 - target);
  152.                     resenje = x;
  153.                 }
  154.             }
  155.             Console.WriteLine(resenje);
  156.  
  157.             Console.ReadKey();
  158.         }
  159.  
  160.         static List<string> resenja = new List<string>();
  161.         static DataTable dt = new DataTable();
  162.         private static void calculateExpression(string expression)
  163.         {
  164.             int value = Convert.ToInt32(dt.Compute(expression, ""));
  165.  
  166.             if (value > 0)
  167.             {
  168.                 if (value == target || (value >= target - 100 && value <= target + 100))
  169.                 {
  170.                     resenja.Add(expression + "=" + value.ToString());
  171.                 }
  172.             }
  173.         }
  174.  
  175.         public static void getCombinations(List<string> combinations, List<string> input, String comb, int pos, int length)
  176.         {
  177.             if (pos < length)
  178.             {
  179.                 foreach (string ch in input)
  180.                 {
  181.                     getCombinations(combinations, input, comb + ch, pos + 1, length);
  182.                 }
  183.             }
  184.             else
  185.             {
  186.                 if (!comb.Contains("+") && !comb.Contains("-") && !comb.Contains("*") && !comb.Contains("/"))
  187.                 {
  188.                     if (CheckForDupicate(comb))
  189.                     {
  190.                         combinations.Add(comb);
  191.                     }
  192.                 }
  193.                 else
  194.                 {
  195.                     combinations.Add(comb);
  196.                 }
  197.             }
  198.         }
  199.         public static bool CheckForDupicate(string input)
  200.         {
  201.             return input.Distinct().Count() == input.Length;
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement