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.Data;
- using System.Text.RegularExpressions;
- using System.Diagnostics;
- namespace testconsole
- {
- class Program
- {
- //0 1 2
- //?+?-?*?+?*?*?+?+?-?*?+?*?*?+?+?-?*?+?*?*?+?+?-?*?+?*?*?
- static void Main(string[] args)
- {
- #region INPUT
- //int first = Convert.ToInt32(Console.ReadLine());
- int length = 7;
- //int length = 455;
- //string expression = Console.ReadLine();
- string expression = "?+?-?*?+?*?*?";
- //string expression = "?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?";
- //string[] temp = Console.ReadLine().Split(' ');
- //string[] temp = "1 2 4".Split(' ');
- string[] temp = "0 0 7".Split(' ');
- List<int> counts = new List<int>();
- foreach (var item in temp)
- {
- counts.Add(Convert.ToInt32(item));
- }
- #endregion
- #region CALCULATE
- expression = expression.Replace("+?*", "+MP0*");
- expression = expression.Replace("-?*", "-MN0*");
- expression = expression.Replace("*?+", "*MP0+");
- expression = expression.Replace("*?-", "*MN0-");
- expression = expression.Replace("-?-", "-NN-");
- expression = expression.Replace("-?+", "-NN+");
- expression = expression.Replace("+?-", "+PP-");
- expression = expression.Replace("+?+", "-PP+");
- expression = expression.Replace("-?", "-NN");
- expression = expression.Replace("?-", "PP-");
- expression = expression.Replace("+?", "+PP");
- expression = expression.Replace("?+", "PP+");
- if (expression.Substring(0, 2) == "?*")
- expression = "MP0*" + expression.Substring(2);
- if (expression.Substring(expression.Length - 2) == "*?")
- expression = expression.Substring(0, expression.Length - 2) + "*MP0";
- int mpCount = 0;
- int mnCount = 0;
- while (expression.Contains("MP" + mpCount + "*?*"))
- {
- expression = expression.Replace("MP" + mpCount + "*?*", "MP" + mpCount + "*MP" + (mpCount + 1) + "*");
- mpCount++;
- }
- int tempmnCount = 0;
- while (expression.Contains("MN" + mnCount + "*?*"))
- {
- expression = expression.Replace("MN" + mnCount + "*?*", "MN" + mnCount + "*MN" + (mnCount + 1) + "*");
- if(expression.Contains("MN" + mnCount + "*MP0"))
- {
- expression = expression.Replace("MN" + mnCount + "*MP0", "MN" + mnCount + "*MN" + mnCount + 1);
- tempmnCount = mnCount + 1;
- }
- mnCount++;
- }
- if (expression.Contains("MN0*MP0"))
- {
- expression = expression.Replace("MN0*MP0", "MN0*MN0");
- }
- if (tempmnCount > mnCount) mnCount = tempmnCount;
- int countsIndex = 0;
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- while (counts[countsIndex] > 0 && mnCount >= 0)
- {
- if (expression.Contains("MN" + mnCount))
- {
- expression = ReplaceFirst(expression, "MN" + mnCount, countsIndex.ToString());
- counts[countsIndex]--;
- }
- else
- mnCount--;
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- }
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- while (counts[countsIndex] > 0 && expression.Contains("NN"))
- {
- expression = ReplaceFirst(expression, "NN", countsIndex.ToString());
- counts[countsIndex]--;
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- }
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- while (counts[countsIndex] > 0 && expression.Contains("PP"))
- {
- expression = ReplaceFirst(expression, "PP", countsIndex.ToString());
- counts[countsIndex]--;
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- }
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- while (counts[countsIndex] > 0 && mpCount >= 0)
- {
- if (expression.Contains("MP" + mpCount))
- {
- expression = ReplaceFirst(expression, "MP" + mpCount, countsIndex.ToString());
- counts[countsIndex]--;
- }
- else
- mpCount--;
- while (counts[countsIndex] <= 0 && countsIndex < 2)
- countsIndex++;
- }
- #endregion
- //Stopwatch stopwatch1 = new Stopwatch();
- //Stopwatch stopwatchX = new Stopwatch();
- //stopwatch1.Start();
- //for (var i = 0; i<200; i++)
- Console.WriteLine(Evaluate(expression));
- //stopwatch1.Stop();
- //stopwatchX.Start();
- //for(var j = 0; j<200; j++)
- Console.WriteLine(EvaluateX(expression));
- //stopwatchX.Stop();
- //Console.WriteLine(stopwatch1.Elapsed);
- //Console.WriteLine(stopwatchX.Elapsed);
- Console.ReadLine();
- }
- public static long EvaluateX(string expression)
- {
- long result = 0;
- //while((index = expression.IndexOf('*')) != -1)
- //{
- // string calc = expression.Substring(index-1, 3);
- // expression = expression.Replace(calc, (Convert.ToInt16(calc[0]) * Convert.ToInt16(calc[2])).ToString());
- //}
- //while(((index = expression.IndexOf('-')) != -1) || ((index = expression.IndexOf('+')) != -1))
- //{
- // //thiswillnotwork :/
- //}
- List<string> numbers = expression.Split(new char[] { '*', '+', '-' }).ToList();
- var temp = expression.Replace("0", "");
- temp = temp.Replace("1", "");
- List<char> signs = temp.Replace("2", "").ToList();
- while(signs.Contains('*'))
- {
- int index = signs.IndexOf('*');
- numbers[index] = (Convert.ToInt64(numbers[index]) * Convert.ToInt64(numbers[index + 1])).ToString();
- numbers.RemoveAt(index + 1);
- signs.RemoveAt(index);
- }
- while(signs.Contains('-'))
- {
- int index = signs.IndexOf('-');
- result -= Convert.ToInt64(numbers[index + 1]);
- numbers.RemoveAt(index + 1);
- signs.RemoveAt(index);
- }
- for(var i = 0; i < numbers.Count; i++)
- {
- result += Convert.ToInt64(numbers[i]);
- }
- //result = 0;
- return result;
- }
- public static string ReplaceFirst(string text, string search, string replace)
- {
- int pos = text.IndexOf(search);
- if (pos < 0)
- {
- return text;
- }
- return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
- }
- public static double Evaluate(string expression)
- {
- DataTable table = new DataTable();
- table.Columns.Add("expression", typeof(string), expression);
- DataRow row = table.NewRow();
- table.Rows.Add(row);
- return double.Parse((string)row["expression"]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement