Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- namespace ConsoleApp58
- {
- class Program
- {
- static List<string> numbersCombinations = new List<string>();
- static List<string> operatorsCombinations = new List<string>();
- static int target = 0;
- static void Main(string[] args)
- {
- List<string> nums = new List<string>() { "1", "3", "3", "7", "1", "8" };
- List<string> nums1 = new List<string>() { "0", "1", "2", "3", "4", "5" };
- List<string> operators = new List<string>() { "+", "-", "*", "/" };
- target = 999;
- for (int i = 2; i < 7; i++)
- {
- getCombinations(numbersCombinations, nums1, "", 0, i);
- }
- for (int i = 1; i < 6; i++)
- {
- getCombinations(operatorsCombinations, operators, "", 0, i);
- }
- int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0;
- StringBuilder expression = new StringBuilder();
- for (int i = 0; i < operatorsCombinations.Count; i++)
- {
- string operator1 = operatorsCombinations[i];
- for (int j = 0; j < numbersCombinations.Count; j++)
- {
- expression.Clear();
- x1 = Convert.ToInt32(numbersCombinations[j].Substring(0, 1));
- x2 = Convert.ToInt32(numbersCombinations[j].Substring(1, 1));
- if (operatorsCombinations[i].Length == 1)
- {
- if (j == 30)
- {
- break;
- }
- expression.Append(nums[x1]).Append(operator1[0]).Append(nums[x2]);
- calculateExpression(expression.ToString());
- }
- else if (operatorsCombinations[i].Length == 2)
- {
- if (j == 0)
- {
- j = 30;
- }
- else if (j == 150)
- {
- break;
- }
- x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
- expression.Append("((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2]).Append(")").Append(operator1[1]).Append(nums[x3]).Append(")");
- calculateExpression(expression.ToString());
- }
- else if (operatorsCombinations[i].Length == 3)
- {
- if (j == 0)
- {
- j = 150;
- }
- else if (j == 510)
- {
- break;
- }
- x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
- x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
- expression.Append("(((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
- .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
- .Append(nums[x4]).Append(")");
- calculateExpression(expression.ToString());
- }
- else if (operatorsCombinations[i].Length == 4)
- {
- if (j == 0)
- {
- j = 510;
- }
- else if (j == 1230)
- {
- break;
- }
- x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
- x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
- x5 = Convert.ToInt32(numbersCombinations[j].Substring(4, 1));
- expression.Append("((((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
- .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
- .Append(nums[x4]).Append(")").Append(operator1[3]).Append(nums[x5]).Append(")");
- calculateExpression(expression.ToString());
- }
- else if (operatorsCombinations[i].Length == 5)
- {
- if (j == 0)
- {
- j = 1230;
- }
- x3 = Convert.ToInt32(numbersCombinations[j].Substring(2, 1));
- x4 = Convert.ToInt32(numbersCombinations[j].Substring(3, 1));
- x5 = Convert.ToInt32(numbersCombinations[j].Substring(4, 1));
- x6 = Convert.ToInt32(numbersCombinations[j].Substring(5, 1));
- expression.Append("(((((").Append(nums[x1]).Append(operator1[0]).Append(nums[x2])
- .Append(")").Append(operator1[1]).Append(nums[x3]).Append(")").Append(operator1[2])
- .Append(nums[x4]).Append(")").Append(operator1[3]).Append(nums[x5]).Append(")").Append(operator1[4]).Append(nums[x6]).Append(")");
- calculateExpression(expression.ToString());
- }
- }
- }
- string resenje = "";
- int minval = Int32.MaxValue;
- foreach (var x in resenja)
- {
- int k1 = int.Parse(x.Substring(x.IndexOf("=") + 1, x.Length - x.IndexOf("=") - 1));
- if (k1 > 0 && Math.Abs(k1 - target) < minval)
- {
- minval = Math.Abs(k1 - target);
- resenje = x;
- }
- }
- Console.WriteLine(resenje);
- Console.ReadKey();
- }
- static List<string> resenja = new List<string>();
- static DataTable dt = new DataTable();
- private static void calculateExpression(string expression)
- {
- int value = Convert.ToInt32(dt.Compute(expression, ""));
- if (value > 0)
- {
- if (value == target || (value >= target - 100 && value <= target + 100))
- {
- resenja.Add(expression + "=" + value.ToString());
- }
- }
- }
- public static void getCombinations(List<string> combinations, List<string> fin, String pwd, int pos, int length)
- {
- if (pos < length)
- {
- foreach (string ch in fin)
- {
- getCombinations(combinations, fin, pwd + ch, pos + 1, length);
- }
- }
- else
- {
- if (!pwd.Contains("+"))
- {
- if (CheckForDupicate(pwd))
- {
- combinations.Add(pwd);
- }
- }
- else
- {
- combinations.Add(pwd);
- }
- }
- }
- public static bool CheckForDupicate(string input)
- {
- return input.Distinct().Count() == input.Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement