UVa 10033 Interpreter
又是一个杯具的2Y。题目很简单,一步一步来就行啦。
以下是我的代码:
#include
<
iostream
>
#include < string >
#include < algorithm >
#include < cstdio >
#include < cstring >
using namespace std;
int n,cnt,r[ 17 ];
string cmd[ 1007 ];
void Init()
{
n =- 1 ;
cnt = 0 ;
memset(r, 0 , sizeof (r));
for ( int i = 0 ;i < 1000 ;i ++ )
cmd[i] = " 000 " ;
}
void _2( const string & s)
{
r[s[ 1 ] - ' 0 ' ] = s[ 2 ] - ' 0 ' ;
}
void _3( const string & s)
{
r[s[ 1 ] - ' 0 ' ] += s[ 2 ] - ' 0 ' ;
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _4( const string & s)
{
r[s[ 1 ] - ' 0 ' ] *= s[ 2 ] - ' 0 ' ;
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _5( const string & s)
{
r[s[ 1 ] - ' 0 ' ] = r[s[ 2 ] - ' 0 ' ];
}
void _6( const string & s)
{
r[s[ 1 ] - ' 0 ' ] += r[s[ 2 ] - ' 0 ' ];
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _7( const string & s)
{
r[s[ 1 ] - ' 0 ' ] *= r[s[ 2 ] - ' 0 ' ];
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _8( const string & s)
{
r[s[ 1 ] - ' 0 ' ] = atoi(cmd[r[s[ 2 ] - ' 0 ' ]].c_str());
}
void _9( const string & s)
{
int t(r[s[ 1 ] - ' 0 ' ]);
string tmp;
while (t > 0 )
{
tmp += ( char )(t % 10 + ' 0 ' );
t /= 10 ;
}
while (tmp.size() < 3 )
tmp += " 0 " ;
reverse(tmp.begin(),tmp.end());
cmd[r[s[ 2 ] - ' 0 ' ]] = tmp;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen( " data.in " , " r " ,stdin);
#endif
int T;
cin >> T;cin. get ();
cin. get ();
for ( int case_num = 1 ;case_num <= T;case_num ++ )
{
Init();
string s;
while (getline(cin,s) && s != "" )
{
n ++ ;
cmd[n] = s;
}
for ( int i = 0 ;i < 1000 ;i ++ )
{
cnt ++ ;
if (cmd[i] == " 100 " )
break ;
switch (cmd[i][ 0 ])
{
case ' 2 ' :
_2(cmd[i]);
break ;
case ' 3 ' :
_3(cmd[i]);
break ;
case ' 4 ' :
_4(cmd[i]);
break ;
case ' 5 ' :
_5(cmd[i]);
break ;
case ' 6 ' :
_6(cmd[i]);
break ;
case ' 7 ' :
_7(cmd[i]);
break ;
case ' 8 ' :
_8(cmd[i]);
break ;
case ' 9 ' :
_9(cmd[i]);
break ;
case ' 0 ' :
if (r[cmd[i][ 2 ] - ' 0 ' ])
i = r[cmd[i][ 1 ] - ' 0 ' ] - 1 ;
break ;
}
}
if (case_num != 1 )
cout << endl;
cout << cnt << endl;
}
return 0 ;
}
#include < string >
#include < algorithm >
#include < cstdio >
#include < cstring >
using namespace std;
int n,cnt,r[ 17 ];
string cmd[ 1007 ];
void Init()
{
n =- 1 ;
cnt = 0 ;
memset(r, 0 , sizeof (r));
for ( int i = 0 ;i < 1000 ;i ++ )
cmd[i] = " 000 " ;
}
void _2( const string & s)
{
r[s[ 1 ] - ' 0 ' ] = s[ 2 ] - ' 0 ' ;
}
void _3( const string & s)
{
r[s[ 1 ] - ' 0 ' ] += s[ 2 ] - ' 0 ' ;
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _4( const string & s)
{
r[s[ 1 ] - ' 0 ' ] *= s[ 2 ] - ' 0 ' ;
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _5( const string & s)
{
r[s[ 1 ] - ' 0 ' ] = r[s[ 2 ] - ' 0 ' ];
}
void _6( const string & s)
{
r[s[ 1 ] - ' 0 ' ] += r[s[ 2 ] - ' 0 ' ];
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _7( const string & s)
{
r[s[ 1 ] - ' 0 ' ] *= r[s[ 2 ] - ' 0 ' ];
r[s[ 1 ] - ' 0 ' ] %= 1000 ;
}
void _8( const string & s)
{
r[s[ 1 ] - ' 0 ' ] = atoi(cmd[r[s[ 2 ] - ' 0 ' ]].c_str());
}
void _9( const string & s)
{
int t(r[s[ 1 ] - ' 0 ' ]);
string tmp;
while (t > 0 )
{
tmp += ( char )(t % 10 + ' 0 ' );
t /= 10 ;
}
while (tmp.size() < 3 )
tmp += " 0 " ;
reverse(tmp.begin(),tmp.end());
cmd[r[s[ 2 ] - ' 0 ' ]] = tmp;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen( " data.in " , " r " ,stdin);
#endif
int T;
cin >> T;cin. get ();
cin. get ();
for ( int case_num = 1 ;case_num <= T;case_num ++ )
{
Init();
string s;
while (getline(cin,s) && s != "" )
{
n ++ ;
cmd[n] = s;
}
for ( int i = 0 ;i < 1000 ;i ++ )
{
cnt ++ ;
if (cmd[i] == " 100 " )
break ;
switch (cmd[i][ 0 ])
{
case ' 2 ' :
_2(cmd[i]);
break ;
case ' 3 ' :
_3(cmd[i]);
break ;
case ' 4 ' :
_4(cmd[i]);
break ;
case ' 5 ' :
_5(cmd[i]);
break ;
case ' 6 ' :
_6(cmd[i]);
break ;
case ' 7 ' :
_7(cmd[i]);
break ;
case ' 8 ' :
_8(cmd[i]);
break ;
case ' 9 ' :
_9(cmd[i]);
break ;
case ' 0 ' :
if (r[cmd[i][ 2 ] - ' 0 ' ])
i = r[cmd[i][ 1 ] - ' 0 ' ] - 1 ;
break ;
}
}
if (case_num != 1 )
cout << endl;
cout << cnt << endl;
}
return 0 ;
}