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 ConsoleApp51
- {
- class Program
- {
- static int target = 0;
- static void Main(string[] args)
- {
- List<int> nums = new List<int>() { 1, 2, 3, 4, 5, 6 };
- target = 999;
- generate(nums, 2);
- generate(nums, 3);
- generate(nums, 4);
- generate(nums, 5);
- generate(nums, 6);
- }
- static List<string> list1 = new List<string>();
- static List<string> list2 = new List<string>();
- static List<string> list3 = new List<string>();
- static List<string> list4 = new List<string>();
- static List<string> list5 = new List<string>();
- static List<string> list6 = new List<string>();
- private static void generate(List<int> nums, int l)
- {
- for (int i = 0; i < nums.Count; i++)
- {
- for (int j = i + 1; j < nums.Count; j++)
- {
- if (l == 2)
- {
- list1.Clear();
- list1.Add(nums[i].ToString());
- list1.Add(nums[j].ToString());
- list2.Add(add(list1));
- list2.Add(sub(list1)[0]);
- list2.Add(sub(list1)[1]);
- list2.Add(mul(list1));
- list2.Add(div(list1)[0]);
- list2.Add(div(list1)[1]);
- }
- else if (l == 3)
- {
- addToList(list2, list3, nums);
- }
- else if (l == 4)
- {
- addToList(list3, list4, nums);
- }
- else if (l == 5)
- {
- addToList(list4, list5, nums);
- }
- else if (l == 6)
- {
- addToList(list5, list6, nums);
- }
- }
- }
- }
- private static string add(List<string> list)
- {
- return "(" + list[0] + "+" + list[1] + ")";
- }
- private static List<string> sub(List<string> list)
- {
- List<string> subList = new List<string>();
- subList.Add("(" + list[0] + "-" + list[1] + ")");
- subList.Add("(" + list[1] + "-" + list[0] + ")");
- return subList;
- }
- private static string mul(List<string> list)
- {
- return "(" + list[0] + "*" + list[1] + ")";
- }
- private static List<string> div(List<string> list)
- {
- List<string> divList = new List<string>();
- divList.Add("(" + list[0] + "/" + list[1] + ")");
- divList.Add("(" + list[1] + "/" + list[0] + ")");
- return divList;
- }
- private static void addToList(List<string> lista1, List<string> lista2, List<int> nums)
- {
- List<string> lista3 = new List<string>();
- foreach (string x in lista1)
- {
- for (int k = 0; k < nums.Count; k++)
- {
- if (x.Contains(nums[k].ToString()))
- {
- continue;
- }
- else
- {
- lista3.Clear();
- lista3.Add(nums[k].ToString());
- lista3.Add(x);
- lista2.Add(add(lista3));
- lista2.Add(sub(lista3)[0]);
- lista2.Add(sub(lista3)[1]);
- lista2.Add(mul(lista3));
- lista2.Add(div(lista3)[0]);
- lista2.Add(div(lista3)[1]);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment