Advertisement
Pavle_nis

Untitled

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