private void button_ChangeTask_Click(object sender, EventArgs e) { if (checkedListBox_Tasks.SelectedIndex != -1) { ChangeTask changeTask = new ChangeTask(); changeTask.ShowDialog(); if (changeTask.success()) { ActionRegistrator.addRecord(DateTime.Now, Misc.getMethodName(), userName, changeTask.resultName); int index = checkedListBox_Tasks.SelectedIndex; checkedListBox_Tasks.Items.RemoveAt(index); checkedListBox_Tasks.Items.Insert(index, changeTask.resultName); listView1.Items.RemoveAt(index); listView1.Items.Insert(index, changeTask.resultDate.ToString().Substring(0, 16)); notificationManager.addTask(changeTask.resultDate, changeTask.description, changeTask.resultName); savedInstanceManager.deleteTask(index); savedInstanceManager.addTask(changeTask.resultDate, changeTask.resultName, descriptions[index].ToString(), index); } } }
static long getWays(long n, long[] c) { // overlapping data contains info about count of change ways // key has the following structure: // <sum_to_change>:<index_in_coins_array_to_start_calculate_change> var overlappingData = new Dictionary <string, long>(); var tasks = new Stack <ChangeTask>(); // starting from calculation on change for n and entire coins array var task = new ChangeTask() { N = n, CoinIndex = 0 }; tasks.Push(task); while (tasks.Count > 0) { // consider current change task var currTask = tasks.Peek(); // if we have reached last available coin // terminal branch if (currTask.CoinIndex == c.Length - 1) { // remove current task from stack tasks.Pop(); // place overlapping info about posibility of change if (currTask.N % c[currTask.CoinIndex] == 0) { // can change overlappingData.Add(currTask.N + ":" + currTask.CoinIndex, 1); } else { // cannot change overlappingData.Add(currTask.N + ":" + currTask.CoinIndex, 0); } continue; } // list of new change tasks which would be created // while calculating current change var newTasks = new List <ChangeTask>(); // store current rest of sum long currSum = currTask.N; long currResult = 0; var newCoinIndex = currTask.CoinIndex + 1; while (currSum >= 0) { if (overlappingData.ContainsKey(currSum + ":" + newCoinIndex)) { // if there is already calculated change for the rest of sum - add to result currResult += overlappingData[currSum + ":" + newCoinIndex]; } else { // otherwise create new task for calculate it var newTask = new ChangeTask() { N = currSum, CoinIndex = newCoinIndex }; newTasks.Add(newTask); } currSum -= c[currTask.CoinIndex]; } if (newTasks.Count == 0) { // if no change tasks were created - we have successfully calculated change tasks.Pop(); overlappingData.Add(currTask.N + ":" + currTask.CoinIndex, currResult); } else { // othervise, drop calculated result and continue with new tasks // result will be obtained later, when current task will be applied once again foreach (var newTask in newTasks) { tasks.Push(newTask); } } } return(overlappingData[n + ":" + 0]); }