Вот задача:
Вот моё решение
Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace __6_6
{
class Program
{
static string[] arrRAM = new string[1000];
static int[] registers = new int[10];
static int currRam = 0;
static void Main(string[] args)
{
int i;
//Init
for (i = 0; i < 1000; i++)
{
arrRAM[i] = "000";
}
for (i = 0; i < 10; i++)
{
registers[i] = 0;
}
string[] strArr = File.ReadAllLines("input.txt");
List<int> CaseStarts = new List<int>();
int k = 0;
for (i = 1; i < strArr.Length; i++)
{
if (strArr[i].Equals(string.Empty))
{
CaseStarts.Add(k);
}
if (strArr[i].Length == 3)
{
arrRAM[k++] = strArr[i];
}
}
foreach(int j in CaseStarts)
{
ExecuteCase(j);
}
Console.ReadKey();
}
static void ExecuteCase(int Start)
{
int digit1, digit2, digit3;
int instructions = 0;
currRam = Start;
do
{
digit1 = int.Parse(arrRAM[currRam].Substring(0, 1));
digit2 = int.Parse(arrRAM[currRam].Substring(1, 1));
digit3 = int.Parse(arrRAM[currRam].Substring(2, 1));
switch (digit1)
{
case 2:
registers[digit2] = digit3 % 1000;
instructions++;
currRam++;
break;
case 3:
registers[digit2] += digit3;
registers[digit2] %= 1000;
instructions++;
currRam++;
break;
case 4:
registers[digit2] *= digit3;
registers[digit2] %= 1000;
instructions++;
currRam++;
break;
case 5:
registers[digit2] = registers[digit3];
registers[digit2] %= 1000;
instructions++;
currRam++;
break;
case 6:
registers[digit2] += registers[digit3];
registers[digit2] %= 1000;
instructions++;
currRam++;
break;
case 7:
registers[digit2] *= registers[digit3];
registers[digit2] %= 1000;
instructions++;
currRam++;
break;
case 8:
registers[digit2] = int.Parse(arrRAM[registers[digit3]]);
instructions++;
currRam++;
break;
case 9:
arrRAM[registers[digit3]] = string.Format("{0:000}", registers[digit2]);
instructions++;
currRam++;
break;
case 0:
currRam++;
if (registers[digit3] != 0)
{
currRam = registers[digit2];
instructions++;
}
break;
}
} while (arrRAM[currRam] != "100");
Console.WriteLine("{0}", instructions);
}
}
}
Вот input файл
Code: Select all
1
299
492
495
399
492
495
399
283
279
689
078
100
000
000