放牧代码和思想
专注自然语言处理、机器学习算法
    This thing called love. Know I would've. Thrown it all away. Wouldn't hesitate.

UVa 10033 – Interpreter

模拟类。题目不是重点,重点是输入输出的格式。

中文题目:

题目链接

两点教训:

①不要递归,递归RE。

②连用两个cin.ignore();忽略掉最开头的n后面的\n和空白行的\n。

#ifndef ONLINE_JUDGE
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
int RAM[1000];
int REG[10];
int step;
void doCMD(int index)
{
	while (true)
	{
		++step;
		int a = RAM[index] / 100;
		int b = RAM[index] / 10 - 10 * a;
		int c = RAM[index] % 10;
		switch (a)
		{
			case 1:
			{
					  return;
			}break;
			case 2:
			{
					  REG[b] = c;
			}break;
			case 3:
			{
					  REG[b] += c;
					  REG[b] %= 1000;
			}break;
			case 4:
			{
					  REG[b] *= c;
					  REG[b] %= 1000;
			}break;
			case 5:
			{
					  REG[b] = REG[c];
			}break;
			case 6:
			{
					  REG[b] += REG[c];
					  REG[b] %= 1000;
			}break;
			case 7:
			{
					  REG[b] *= REG[c];
					  REG[b] %= 1000;
			}break;
			case 8:
			{
					  REG[b] = RAM[REG[c]];
			}break;
			case 9:
			{
					  RAM[REG[c]] = REG[b];
			}break;
			case 0:
			{
					  if (REG[c])
					  {
						  index = REG[b];
						  continue;
					  }
			}break;
			default:
				break;
		}
		++index;
	}
}

///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	string line;
	cin.ignore();
	cin.ignore();
	while (n--)
	{
		int index = 0;
		step = 0;
		memset(RAM, 0, sizeof(RAM));
		memset(REG, 0, sizeof(REG));
		while (getline(cin, line) && line.length())
		{
			RAM[index] = atoi(line.c_str());
			++index;
		}
		doCMD(0);
		cout << step << endl;
		if (n)
		{
			cout << endl;
		}
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("out.txt");
#endif
	return 0;
}
///////////////////////////End Sub//////////////////////////////////

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » UVa 10033 – Interpreter

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的作品

HanLP自然语言处理包《自然语言处理入门》