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;
- namespace ConsoleApp48
- {
- class Program
- {
- // add=lambda x,y:[x[0]+y[0],"("+x[1]+"+"+y[1]+")"]
- (int first, string last) add(List<int> x, List<string> x1, List<int> y, List<string> y1)
- {
- return (first: (x[0] + y[0]), last: "(" + x1[0] + "+" + y1[0] + ")");
- }
- (int first, string last) sub(List<int> x, List<string> x1, List<int> y, List<string> y1)
- {
- return (first: (x[0] - y[0]), last: "(" + x1[0] + "-" + y1[0] + ")");
- }
- (int first, string last) mul(List<int> x, List<string> x1, List<int> y, List<string> y1)
- {
- return (first: (x[0] * y[0]), last: "(" + x1[0] + "*" + y1[0] + ")");
- }
- (int first, string last) div(List<int> x, List<string> x1, List<int> y, List<string> y1)
- {
- if (y[0] != 0 && !(x[0] % y[0] != 0))
- {
- return (first: (x[0] / y[0]), last: x1[0] + "/" + y1[0]); // make sure x[0]/y[0] is int
- }
- else
- {
- return (first: (0), last: x1[0] + "/" + y1[0]);
- }
- }
- //dropx=lambda x,y:y
- List<int> dropx(List<int> x, List<int> y)
- {
- return y;
- }
- List<int> idropx(List<int> x, List<int> y)
- {
- return x;
- }
- (int first, string last) idiv(List<int> x, List<string> x1, List<int> y, List<string> y1)
- {
- if (x[0] != 0 && !(y[0] % x[0] != 0))
- {
- return (first: (y[0] / x[0]), last: y1[0] + "/" + x1[0]); // make sure x[0]/y[0] is int
- }
- else
- {
- return (first: (0), last: y1[0] + "/" + x1[0]);
- }
- }
- (int first, string last) isub(List<int> x, List<string> x1, List<int> y, List<string> y1)
- {
- return (first: (y[0] - x[0]), last: "(" + y[1] + "-" + x[1] + ")");
- }
- //ops=[add,sub,mul,div,dropx,isub,idiv,idropx]
- //this is list of functions in if you are familiar with funtion pointer from c++
- //so in C# i have no idea function pointer worker or not so there you can store them as string then call it like this
- string[] ops = new string[] { "add", "sub", "mul", "div", "dropx", "isub", "idiv", "idropx" };
- static int[] num = new int[] { 1, 2, 3, 4, 5, 6 };
- static int target = 999;
- //tab=[[x,str(x)] for x in num]
- static List<int> tabint = new List<int>();
- static List<string> tabstr = new List<string>();
- (int first, string last) countdown(int l, int w)
- {
- if(l <= 1)
- {
- return (first: Math.Abs(tabint[w] - target), last: tabstr[w]);
- }
- int dm = 999999;
- string sm = "";
- for (int i = 1; i < l; i++)
- {
- if (tabstr[i].Equals(""))
- {
- continue;
- }
- List<int> a = new List<int>();
- a.Add(tabint[i]);
- List<string> a1 = new List<string>();
- a1.Add(tabstr[i]);
- tabstr[i] = "";
- for (int j = 0; j < l; j++)
- {
- if (tabstr[j].Equals(""))
- {
- continue;
- }
- List<int> b = new List<int>(tabint[j]);
- b.Add(tabint[j]);
- List<string> b1 = new List<string>();
- b1.Add(tabstr[j]);
- for (int k = 0; k < ops.Length; j++)
- {
- if (ops[k] == "add")
- {
- //tab[j]=f(a,b)
- (int first, string last) = add(a, a1, b, b1);
- //tab[w][0] is tabint[w]
- //tab[w][1] is tabstr[w]
- tabint[j] = first;
- tabstr[j] = last;
- }
- else if (ops[k] == "sub")
- {
- (int first, string last) = sub(a, a1, b, b1);
- tabint[j] = first;
- tabstr[j] = last;
- }
- else if (ops[k] == "mul")
- {
- (int first, string last) = mul(a, a1, b, b1);
- tabint[j] = first;
- tabstr[j] = last;
- }
- else if (ops[k] == "div")
- {
- (int first, string last) = div(a, a1, b, b1);
- tabint[j] = first;
- tabstr[j] = last;
- }
- else if (ops[k] == "dropx")
- {
- List<int> first = dropx(a, b);
- tabint[j] = first[0];
- tabstr[j] = first[0].ToString();
- }
- else if (ops[k] == "isub")
- {
- (int first, string last) = isub(a, a1, b, b1);
- tabint[j] = first;
- tabstr[j] = last;
- }
- else if (ops[k] == "idiv")
- {
- (int first, string last) = idiv(a, a1, b, b1);
- tabint[j] = first;
- tabstr[j] = last;
- }
- else if (ops[k] == "idropx")
- {
- List<int> first = dropx(a, b);
- tabint[j] = first[0];
- tabstr[j] = first[0].ToString();
- }
- (int d, string s) = countdown(l - 1, j);
- if (d < dm || ((d == dm) && s.Length < sm.Length))
- {
- sm = s;
- dm = d;
- }
- }
- tabint[j] = b[0];
- tabstr[j] = b[0].ToString();
- }
- tabint[i] = a[0];
- tabstr[i] = a[0].ToString();
- }
- return (first: dm, last: sm);
- }
- static void Main(string[] args)
- {
- Program prog = new Program();
- for (int i = 0; i < num.Length; i++)
- {
- tabint.Add(num[i]);
- tabstr.Add(num[i] + "");
- }
- (int d, string s) = prog.countdown(num.Length, 0);
- Console.WriteLine(target - d + "=" + s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement