UVA 18451
题目链接:
http://www.bnuoj.com/v3/problem_show.php?pid=18451
题意:
10个寄存器,最多1000个ram里存放的指令,未存放的地方标记为000.
指令格式abc,然后题目中给出每个指令对应的操作,求执行了几次操作后退出。
思路:
阅读题。
看了代码秒懂,就是每次会从在ram中当前位置向下移动读取下一位置的指令,除非执行了0指令。
源码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 10000 + 5;
int ram[MAXN], reg[MAXN];
char str[MAXN];
int main()
{
int T;
scanf("%d", &T);
getchar();
fgets(str, MAXN, stdin);
int f = 1;
while(T--){
// getline(str, MAXN, stdin);
memset(ram, 0, sizeof(ram));
memset(reg, 0, sizeof(reg));
int cnt = 0;
while(fgets(str, MAXN, stdin) != NULL && str[0] != '\n'){
for(int i = 0 ; i < 3 ; i++) ram[cnt] = ram[cnt] * 10 + str[i] - '0';
cnt++;
}
int ans = 0;
int p = 0;
while(1){
ans++;
int op = ram[p] / 100, x = ram[p] / 10 % 10, y = ram[p] % 10;
if(op == 1) break;
if(op == 2) reg[x] = y;
if(op == 3) reg[x] = (reg[x] + y) % 1000;
if(op == 4) reg[x] = (reg[x] * y) % 1000;
if(op == 5) reg[x] = reg[y];
if(op == 6) reg[x] = (reg[x] + reg[y]) % 1000;
if(op == 7) reg[x] = (reg[x] * reg[y]) % 1000;
if(op == 8) reg[x] = ram[reg[y]];
if(op == 9) ram[reg[y]] = reg[x]; ///这里容易出问题
if(op == 0 && reg[y]) p = reg[x];
else p++;
}
if(f) f = 0;
else printf("\n");
printf("%d\n", ans);
}
return 0;
}