Advertisement
Pavle_nis

Untitled

Mar 15th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.26 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<int> y)
  14.         {
  15.             return (first: (x[0] + y[0]), last: "(" + x[1] + "+" + y[1] + ")");
  16.         }
  17.         (int first, string last) sub(List<int> x, List<int> y)
  18.         {
  19.             return (first: (x[0] - y[0]), last: "(" + x[1] + "-" + y[1] + ")");
  20.         }
  21.         (int first, string last) mul(List<int> x, List<int> y)
  22.         {
  23.             return (first: (x[0] * y[0]), last: "(" + x[1] + "*" + y[1] + ")");
  24.         }
  25.         (int first, string last) div(List<int> x, List<int> y)
  26.         {
  27.             if (y[0] != 0 && !(x[0] % y[0] != 0))
  28.             {
  29.                 return (first: (x[0] / y[0]), last: x[1] + "/" + y[1]); // make sure x[0]/y[0] is int
  30.             }
  31.             else
  32.             {
  33.                 return (first: (0), last: x[1] + "/" + y[1]);
  34.             }
  35.         }
  36.  
  37.         //dropx=lambda x,y:y
  38.         List<int> dropx(List<int> x, List<int> y)
  39.         {
  40.             return y;
  41.         }
  42.         List<int> idropx(List<int> x, List<int> y)
  43.         {
  44.             return x;
  45.         }
  46.         (int first, string last) idiv(List<int> x, List<int> y)
  47.         {
  48.             if (x[0] != 0 && !(y[0] % x[0] != 0))
  49.             {
  50.                 return (first: (y[0] / x[0]), last: y[1] + "/" + x[1]); // make sure x[0]/y[0] is int
  51.             }
  52.             else
  53.             {
  54.                 return (first: (0), last: y[1] + "/" + x[1]);
  55.             }
  56.         }
  57.         (int first, string last) isub(List<int> x, List<int> y)
  58.         {
  59.             return (first: (y[0] - x[0]), last: "(" + y[1] + "-" + x[1] + ")");
  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.                 List<int> a = new List<int>(tabint[i]);
  89.  
  90.                 for (int j = 0; j < l; j++)
  91.                 {
  92.                     List<int> b = new List<int>(tabint[j]);
  93.  
  94.                     for (int k = 0; k < ops.Length; j++)
  95.                     {
  96.                         if (ops[i] == "add")
  97.                         {
  98.                             //tab[j]=f(a,b)
  99.                             (int first, string last) = add(a, b);
  100.                             //tab[w][0] is tabint[w]
  101.                             //tab[w][1]  is tabstr[w]
  102.  
  103.                             tabint[j] = first;
  104.                             tabstr[j] = last;
  105.  
  106.                         }
  107.                         else if (ops[i] == "sub")
  108.                         {
  109.                             (int first, string last) = sub(a, b);
  110.  
  111.                             tabint[j] = first;
  112.                             tabstr[j] = last;
  113.                         }
  114.                         else if (ops[i] == "mul")
  115.                         {
  116.                             (int first, string last) = mul(a, b);
  117.  
  118.                             tabint[j] = first;
  119.                             tabstr[j] = last;
  120.                         }
  121.                         else if (ops[i] == "div")
  122.                         {
  123.                             (int first, string last) = div(a, b);
  124.  
  125.                             tabint[j] = first;
  126.                             tabstr[j] = last;
  127.                         }
  128.                         else if (ops[i] == "dropx")
  129.                         {
  130.                             List<int> first = dropx(a, b);
  131.  
  132.                             tabint[j] = first[0];
  133.                             tabstr[j] = first[0].ToString();
  134.                         }
  135.                         else if (ops[i] == "isub")
  136.                         {
  137.                             (int first, string last) = isub(a, b);
  138.  
  139.                             tabint[j] = first;
  140.                             tabstr[j] = last;
  141.                         }
  142.                         else if (ops[i] == "idiv")
  143.                         {
  144.                             (int first, string last) = idiv(a, b);
  145.  
  146.                             tabint[j] = first;
  147.                             tabstr[j] = last;
  148.                         }
  149.                         else if (ops[i] == "idropx")
  150.                         {
  151.                             List<int> first = dropx(a, b);
  152.  
  153.                             tabint[j] = first[0];
  154.                             tabstr[j] = first[0].ToString();
  155.                         }
  156.  
  157.                         (int d, string s) = countdown(l - 1, j);
  158.  
  159.                         if (d < dm || ((d == dm) && s.Length < sm.Length))
  160.                         {
  161.                             sm = s;
  162.                             dm = d;
  163.                         }
  164.                     }
  165.  
  166.                     tabint[j] = b[0];
  167.                     tabstr[j] = b[0].ToString();
  168.                 }
  169.  
  170.                 tabint[i] = a[0];
  171.                 tabstr[i] = a[0].ToString();
  172.             }
  173.  
  174.             return (first: dm, last: sm);
  175.         }
  176.         static void Main(string[] args)
  177.         {
  178.             Program prog = new Program();
  179.  
  180.             for (int i = 0; i < num.Length; i++)
  181.             {
  182.                 tabint.Add(num[i]);
  183.                 tabstr.Add(num[i] + "");
  184.             }
  185.  
  186.             (int d, string s) = prog.countdown(num.Length, 0);
  187.  
  188.             Console.WriteLine(target - d + "=" + s);
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement