挺简单的
2026-02-07 14:31:31
发布于:浙江
这道题可以通过手搓一个拥有 8MB 内存、64 位系统、使用类似汇编的语言的虚拟机来完成。
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned int
#define N 1048576
int memory[N],pos;
string code[N]={
"input 0", "input 1",
"input 2", "write 3 1",
"copy 0 4", "copy 1 5",
"write 7 1", "write 8 2",
"if == 5 6 17", "calc and 5 7 9",
"if == 9 6 13", "calc mul 3 4 3",
"calc mod 3 2 3", "calc div 5 8 5",
"calc mul 4 4 4", "calc mod 4 2 4",
"goto 8",
"output int 0", "write 10 94",
"output char 10", "output int 1",
"write 11 32", "output char 11",
"write 10 109", "output char 10",
"write 10 111", "output char 10",
"write 10 100", "output char 10",
"output char 11", "output int 2",
"write 10 61", "output char 10",
"output int 3", "exit"
};
string readStr(int x,int y) // 从 code[x][y] 开始读取一个字符串, 读到空格为止
{
pos=y;
string ret="";
int len=code[x].length();
while(pos<len && code[x][pos]==' ')
{
++pos;
}
while(pos<len && code[x][pos]!=' ')
{
ret=ret+code[x][pos++];
}
return ret;
}
int readInt(int x,int y) // 从 code[x][y] 开始读取一个整数, 读到非数字为止
{
pos=y;
int ret=0, len=code[x].length();
bool f=0;
while(pos<len && !isdigit(code[x][pos]))
{
if(code[x][pos]=='-')
{
f=1;
}
++pos;
}
while(pos<len && isdigit(code[x][pos]))
{
ret=(ret<<3)+(ret<<1)+(code[x][pos++]^48);
}
return f?-ret:ret;
}
signed main()
{
for(int i=0;;i=(i+1)%N)
{
string op=readStr(i,0);
if(op=="input")
{
int x=readInt(i,pos);
cin>>memory[x];
}
else if(op=="output")
{
string mode=readStr(i,pos);
if(mode=="int")
{
int x=readInt(i,pos);
cout<<memory[x];
}
else if(mode=="char")
{
int x=readInt(i,pos);
cout<<char(memory[x]%128);
}
else
{
cout<<"\n\nError: Invalid Code at i = "<<i<<"\n\n";
break;
}
}
else if(op=="write")
{
int x=readInt(i,pos), y=readInt(i,pos);
memory[x]=y;
}
else if(op=="copy")
{
int x=readInt(i,pos), y=readInt(i,pos);
memory[y]=memory[x];
}
else if(op=="calc") // and or not xor add sub mul div mod
{
string mode=readStr(i,pos);
int x=readInt(i,pos), y=readInt(i,pos), z=(mode=="not" ? 0 : readInt(i,pos));
x=memory[x], y=memory[y];
if(mode=="and")
{
memory[z]=x&y;
}
else if(mode=="or")
{
memory[z]=x|y;
}
else if(mode=="not")
{
memory[y]=~x;
}
else if(mode=="xor")
{
memory[z]=x^y;
}
else if(mode=="add")
{
memory[z]=x+y;
}
else if(mode=="sub")
{
memory[z]=x-y;
}
else if(mode=="mul")
{
memory[z]=x*y;
}
else if(mode=="div")
{
if(y==0)
{
cout<<"\n\nError: Division by Zero\n\n";
break;
}
memory[z]=x/y;
}
else if(mode=="mod")
{
memory[z]=x%y;
}
else
{
cout<<"\n\nError: Invalid Code at i = "<<i<<"\n\n";
break;
}
}
else if(op=="goto")
{
int x=readInt(i,pos);
i=x-1;
}
else if(op=="if") // < > == <= >= !=
{
string mode=readStr(i,pos);
int x=readInt(i,pos), y=readInt(i,pos), z=readInt(i,pos);
x=memory[x], y=memory[y];
if(mode=="<")
{
if(x<y)
{
i=z-1;
}
}
else if(mode==">")
{
if(x>y)
{
i=z-1;
}
}
else if(mode=="==")
{
if(x==y)
{
i=z-1;
}
}
else if(mode=="<=")
{
if(x<=y)
{
i=z-1;
}
}
else if(mode==">=")
{
if(x>=y)
{
i=z-1;
}
}
else if(mode=="!=")
{
if(x!=y)
{
i=z-1;
}
}
else
{
cout<<"\n\nError: Invalid Code at i = "<<i<<"\n\n";
break;
}
}
else if(op=="++")
{
int x=readInt(i,pos);
memory[x]++;
}
else if(op=="--")
{
int x=readInt(i,pos);
memory[x]--;
}
else if(op=="exit")
{
break;
}
else
{
cout<<"\n\nError: Invalid Code at i = "<<i<<"\n\n";
break;
}
}
return 0;
}
程序解释
对其中每种指令的介绍({} 表示必选参数,[] 表示可选参数):
input {x}:输入一个整数(范围 ,)),并将其存放于内存的 x 号位置。
output {mode} {x}:以 mode 形式(int 或 char,其中 char 形式是按照 ASCII 码输出)输出内存的 x 号位置中的数据。
write {x} {y}:将内存的 x 号位置的数据设为 y。
copy {x} {y}:将内存的 x 号位置的数据拷贝至内存的 y 号位置。
calc {mode} {x} {y} [z](当 mode 为 not 时无需 z):将内存的 x 号位置的数据与内存的 y 号位置的数据进行 mode 运算(and、or、xor、add、sub、mul、div 或 mod,其中 div 操作在除数为 0 时会提示 Error: Division by Zero 并退出程序),并将结果存放在内存的 z 号位置。
注:当 mode 为 not 时,其效果为将内存的 x 号位置的数据按位取反的结果存放在内存的 y 号位置;此处的 and、or、xor 和 not 均为位运算,逻辑运算是可以通过这个实现的,故没有单独制作。
goto {x}:跳转至第 x 行(从 0 开始)代码。
if {mode} {x} {y} {z}:如果内存的 x,y 号位置的数据满足 mode 这一关系(mode 可在 <、>、==、<=、>= 和 != 中选择),则跳转至第 z 行(从 0 开始)代码。
++ {x}:将内存的 x 号位置的数据加 1。
-- {x}:将内存的 x 号位置的数据减 1。
exit:退出程序。
解析:基本相当于这段代码 (绝对不是我懒得对这么长的代码写逐行解析了):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,tmp,mod,ans=1,cnt=0,p;
int main()
{
cin>>a>>b>>mod;
p=a;
tmp=b;
while(tmp)
{
if(tmp&1)
{
ans*=p;
ans%=mod;
}
tmp/=2;
p*=p;
p%=mod;
}
cout<<a<<"^"<<b<<" mod "<<mod<<"="<<ans;
return 0;
}
这里空空如也






有帮助,赞一个