Advertisement
Pavle_nis

Untitled

Mar 15th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.16 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. namespace ConsoleApp48
  8. {
  9.     class Program
  10.     {
  11.         // add=lambda x,y:[x[0]+y[0],"("+x[1]+"+"+y[1]+")"]
  12.  
  13.         (int first, string last) add(List<int> x, List<string> x1, List<int> y, List<string> y1)
  14.         {
  15.             return (first: (x[0] + y[0]), last: "(" + x1[0] + "+" + y1[0] + ")");
  16.         }
  17.         (int first, string last) sub(List<int> x, List<string> x1, List<int> y, List<string> y1)
  18.         {
  19.             return (first: (x[0] - y[0]), last: "(" + x1[0] + "-" + y1[0] + ")");
  20.         }
  21.         (int first, string last) mul(List<int> x, List<string> x1, List<int> y, List<string> y1)
  22.         {
  23.             return (first: (x[0] * y[0]), last: "(" + x1[0] + "*" + y1[0] + ")");
  24.         }
  25.         (int first, string last) div(List<int> x, List<string> x1, List<int> y, List<string> y1)
  26.         {
  27.             if (y[0] != 0 && !(x[0] % y[0] != 0))
  28.             {
  29.                 return (first: (x[0] / y[0]), last: x1[0] + "/" + y1[0]); // make sure x[0]/y[0] is int
  30.             }
  31.             else
  32.             {
  33.                 return (first: (0), last: x1[0] + "/" + y1[0]);
  34.             }
  35.         }
  36.  
  37.         //dropx=lambda x,y:y
  38.         (int first, string last) dropx(List<int> x, List<string> x1, List<int> y, List<string> y1)
  39.         {
  40.             return (first: y[0], last: y1[0]);
  41.         }
  42.         (int first, string last) idropx(List<int> x, List<string> x1, List<int> y, List<string> y1)
  43.         {
  44.             return (first: x[0], last: x1[0]);
  45.         }
  46.         (int first, string last) idiv(List<int> x, List<string> x1, List<int> y, List<string> y1)
  47.         {
  48.             if (x[0] != 0 && !(y[0] % x[0] != 0))
  49.             {
  50.                 return (first: (y[0] / x[0]), last: y1[0] + "/" + x1[0]); // make sure x[0]/y[0] is int
  51.             }
  52.             else
  53.             {
  54.                 return (first: (0), last: y1[0] + "/" + x1[0]);
  55.             }
  56.         }
  57.         (int first, string last) isub(List<int> x, List<string> x1, List<int> y, List<string> y1)
  58.         {
  59.             return (first: (y[0] - x[0]), last: "(" + y1[0] + "-" + x1[0] + ")");
  60.         }
  61.  
  62.         //ops=[add,sub,mul,div,dropx,isub,idiv,idropx]
  63.         //this is list of functions in if you are familiar with funtion pointer from c++
  64.         //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
  65.  
  66.         string[] ops = new string[] { "add", "sub", "mul", "div", "dropx", "isub", "idiv", "idropx" };
  67.  
  68.  
  69.         static int[] num = new int[] { 1, 2, 3, 4, 5, 6 };
  70.         static int target = 999;
  71.  
  72.         //tab=[[x,str(x)] for x in num]
  73.  
  74.         static List<int> tabint = new List<int>();
  75.         static List<string> tabstr = new List<string>();
  76.         (int first, string last) countdown(int l, int w)
  77.         {
  78.             if(l <= 1)
  79.             {
  80.                 return (first: Math.Abs(tabint[w] - target), last: tabstr[w]);
  81.             }
  82.  
  83.             int dm = 999999;
  84.             string sm = "";
  85.  
  86.             for (int i = 1; i < l; i++)
  87.             {
  88.                 if (tabstr[i].Equals(""))
  89.                 {
  90.                     continue;
  91.                 }
  92.                 List<int> a = new List<int>();
  93.                 a.Add(tabint[i]);
  94.  
  95.                 List<string> a1 = new List<string>();
  96.                 a1.Add(tabstr[i]);
  97.  
  98.                 tabstr[i] = "";
  99.                 for (int j = 0; j < l; j++)
  100.                 {
  101.                     if (tabstr[j].Equals(""))
  102.                     {
  103.                         continue;
  104.                     }
  105.                     List<int> b = new List<int>();
  106.                     b.Add(tabint[j]);
  107.  
  108.                     List<string> b1 = new List<string>();
  109.                     b1.Add(tabstr[j]);
  110.  
  111.                     for (int k = 0; k < ops.Length; k++)
  112.                     {
  113.                         if (ops[k] == "add")
  114.                         {
  115.                             //tab[j]=f(a,b)
  116.                             (int first, string last) = add(a, a1, b, b1);
  117.                             //tab[w][0] is tabint[w]
  118.                             //tab[w][1]  is tabstr[w]
  119.  
  120.                             tabint[j] = first;
  121.                             tabstr[j] = last;
  122.  
  123.                         }
  124.                         else if (ops[k] == "sub")
  125.                         {
  126.                             (int first, string last) = sub(a, a1, b, b1);
  127.  
  128.                             tabint[j] = first;
  129.                             tabstr[j] = last;
  130.                         }
  131.                         else if (ops[k] == "mul")
  132.                         {
  133.                             (int first, string last) = mul(a, a1, b, b1);
  134.  
  135.                             tabint[j] = first;
  136.                             tabstr[j] = last;
  137.                         }
  138.                         else if (ops[k] == "div")
  139.                         {
  140.                             (int first, string last) = div(a, a1, b, b1);
  141.  
  142.                             tabint[j] = first;
  143.                             tabstr[j] = last;
  144.                         }
  145.                         else if (ops[k] == "dropx")
  146.                         {
  147.                             (int first, string last) = dropx(a, a1, b, b1);
  148.  
  149.                             tabint[j] = first;
  150.                             tabstr[j] = last;
  151.                         }
  152.                         else if (ops[k] == "isub")
  153.                         {
  154.                             (int first, string last) = isub(a, a1, b, b1);
  155.  
  156.                             tabint[j] = first;
  157.                             tabstr[j] = last;
  158.                         }
  159.                         else if (ops[k] == "idiv")
  160.                         {
  161.                             (int first, string last) = idiv(a, a1, b, b1);
  162.  
  163.                             tabint[j] = first;
  164.                             tabstr[j] = last;
  165.                         }
  166.                         else if (ops[k] == "idropx")
  167.                         {
  168.                             (int first, string last) = dropx(a, a1, b, b1);
  169.  
  170.                             tabint[j] = first;
  171.                             tabstr[j] = last;
  172.                         }
  173.  
  174.                         (int d, string s) = countdown(l - 1, j);
  175.  
  176.                         if (d < dm || ((d == dm) && s.Length < sm.Length))
  177.                         {
  178.                             sm = s;
  179.                             dm = d;
  180.                         }
  181.                     }
  182.  
  183.                     tabint[j] = b[0];
  184.                     tabstr[j] = b1[0];
  185.                 }
  186.  
  187.                 tabint[i] = a[0];
  188.                 tabstr[i] = a1[0];
  189.             }
  190.  
  191.             return (first: dm, last: sm);
  192.         }
  193.         static void Main(string[] args)
  194.         {
  195.             Program prog = new Program();
  196.  
  197.             for (int i = 0; i < num.Length; i++)
  198.             {
  199.                 tabint.Add(num[i]);
  200.                 tabstr.Add(num[i] + "");
  201.             }
  202.  
  203.             (int d, string s) = prog.countdown(num.Length, 0);
  204.  
  205.             Console.WriteLine(target - d + "=" + s);
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement