Advertisement
stanevplamen

03.01.08.ExpressionKn

Jul 2nd, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. using System.Text;
  5.  
  6. class Expression
  7. {
  8.     static void Main()
  9.     {
  10.         string input = Console.ReadLine(); //"(c+y)";
  11.         string notUsed = Console.ReadLine();
  12.         int n = int.Parse(Console.ReadLine()); //50;
  13.         char first = input[1];
  14.         char second = input[3];
  15.         int k = 0;
  16.         BigInteger factorialN = 1;
  17.         for (int i = 1; i <= n; i++)
  18.         {
  19.             factorialN = factorialN * i;
  20.         }
  21.  
  22.         StringBuilder sb = new StringBuilder();
  23.         int currentPowerfirst = n;
  24.         int currentPowersecond = 0;
  25.         for (int i = 1; i <= n + 1; i++)
  26.         {
  27.             BigInteger factorialK = 1;
  28.             for (int j = 1; j <= k; j++)
  29.             {
  30.                 factorialK = factorialK * j;
  31.             }
  32.             BigInteger factorialNminusK = 1;
  33.             for (int j = 1; j <= n - k; j++)
  34.             {
  35.                 factorialNminusK = factorialNminusK * j;
  36.             }
  37.  
  38.             // za vs. k: N! / ((N-K)! * K!)
  39.  
  40.             BigInteger tempResult = factorialN / (factorialK * factorialNminusK);
  41.  
  42.             if (k == 0)
  43.             {
  44.                 sb.AppendFormat("({0}^{1})+", first, currentPowerfirst);
  45.             }
  46.             else if (k == n)
  47.             {
  48.                 sb.AppendFormat("({0}^{1})", second, currentPowersecond);
  49.             }
  50.             else
  51.             {
  52.                 sb.AppendFormat("{0}({1}^{2})({3}^{4})+", tempResult, first, currentPowerfirst, second, currentPowersecond);
  53.             }
  54.             currentPowerfirst--;
  55.             currentPowersecond++;
  56.             k++;
  57.         }
  58.         string result = sb.ToString();
  59.         Console.WriteLine(result);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement