FZU2294-Uint47 calculator (47位unsigned int 的计算器)(简单模拟与C++ STL MAP的应用)

In the distant space, there is a technologically advanced planet.

在那遥远的太空,有一个科技发达的星球(B612)

One day they provided the Earth with a code that could achieve the ultimate meaning of the universe. People were very happy, but found that this code can only run on computers with a word length of 47 bits. As a good computer scientist, you need to implement a tool to simulate running this code on our computer.

一天,他们给地球送去了一份代码,这份代码可以穷尽宇宙的终极意义。人们老高兴了,但是发现这份代码只能在47位电脑上运行,作为一个优秀的计算机科学家,你得去模拟个工具来促使这份代码在我们的计算机上运行。

This tool needs to simulate the following instructions:

这个工具需要激励(激励-响应即输入-输出)以下命令

"def x n" : define a unsigned 47 bits integer variable named x, with initial value n, n is an integer in [0, 2^47-1]

def x n :定义一个47位无符号整数变量,变量名为x,初始值为n,n是个47位正整数【范围如上】。

"add x y" : means x = x + y

意味着x被赋予了x+y的和

"sub x y" : means x = x - y

。。以下以此类推

"mul x y" : means x = x * y

"div x y" : means x = x / y, we guarantee y is not zero

"mod x y" : means x = x % y, we guarantee y is not zero

When the result of addition and multiplication cannot be represented by 47 bits, the part above 47 bits is truncated.

当加法和乘法的结果(和与乘积)不能以47位表示时,超过47位的部分应当被截除。

When the result of subtraction is less than zero, the result should add 2^47.

当减法的结果小于0时,结果应当加上2的47次幂。

The name of each variable only contains letters and the length does not greater than 20.

每个变量的名字只由字母组成且长度不超过20.

Input

Contains multiple lines of input, one instruction per line.

包含多行输入,一行一条指令

Output

For each instruction, output the value of the first argument after calculation. For example, "def abc 100", then your output will be "abc = 100" in a line with no quotation marks.

对于每条指令,输出计算后的第一个变量值及其名字。

See Sample Output for more information.

Sample Input

def six 6
def abc 1
def bcd 0
sub bcd abc
add abc six
def universe 0
mul abc six
add universe abc
div bcd six
mod bcd abc

Sample Output

six = 6
abc = 1
bcd = 0
bcd = 140737488355327
abc = 7
universe = 0
abc = 42
universe = 42
bcd = 23456248059221
bcd = 5

变量名?变量值?我们很容易就能想到万能的KEY-VALUE红黑树:STL-MAP<>

这道题的思路很简单,而且意外地,题目的数据并不是很大,暴力模拟完全行得通。

  1. 向MAP(或者你定义的结构体)中输入各组变量数据(def 命令下)
  2. 涉及双目运算的时候,先调用map_name.find(name)找到这个两个变量或者暴力循环找到它们。
  3. 然后进行各种运算,注意乘法运算的时候需要取模。
  4. 最后进行负数或越界的各种处理

题目中涉及到的map的基本指令会在代码中给出。 

#include <iostream>
#include <cstdio>
//#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf("%lld",&a)
#define din(a) scanf("%d",&a)
#define printlnlld(a) printf("%lld\n",a)
#define printlnd(a) printf("%d\n",a)
#define printlld(a) printf("%lld",a)
#define printd(a) printf("%d",a)
#define reset(a,b) memset(a,b,sizeof(a))
const int INF=0x3f3f3f3f;
using namespace std;
const double PI=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod=1000000007;
///Schlacht von Stalingrad
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
map<string,ll>variables;
string command;
map<string,ll>::iterator pointer1,pointer2;//为了寻找变量定义的两个迭代器
int DETERMINATION()
{
     ll lim=1<<30;
     lim*=1<<17;//不知道为何,这种位运算不支持1<<47,所以分两步。
     //cout<<lim<<endl;
     while(cin>>command)
    {
       if(command=="def")
       {
          string tmp;
          ll value;
          cin>>tmp>>value;
          variables.insert(pair<string,ll>(tmp,value));//向map中插入元素
          cout<<tmp<<" = "<<value<<endl;
       }
       else
       {
           string name1,name2;
           cin>>name1>>name2;
           pointer1=variables.find(name1);
           pointer2=variables.find(name2);//找到这两个元素(题目没说会有变量未定义的情况,所以无需担心)
//           cout<<pointer1->first<<endl;
//           cout<<pointer2->first<<endl;
           if(command=="add")
              pointer1->second=pointer1->second+pointer2->second;
//由于迭代器是指针的一种,所以指向pair结构体显然要用->符号
           else if(command=="sub")
              pointer1->second=pointer1->second-pointer2->second;
           else if(command=="mul")
              pointer1->second=(pointer1->second*pointer2->second)%lim;
           else if(command=="div")
              pointer1->second=pointer1->second/pointer2->second;
           else if(command=="mod")
              pointer1->second=pointer1->second%pointer2->second;
           while(pointer1->second>lim)//如果越过上界,直接取余就是截断超出部分
              pointer1->second%=lim;
           while(pointer1->second<0)
              pointer1->second+=lim;//如果小于0,那么可能需要加多个Lim来把它恢复到正数
           cout<<pointer1->first<<" = "<<pointer1->second<<endl;
       }
    }
    return 0;
}

以下是暴力代码

#include <iostream>
#include <cstdio>
//#include <bits/stdc++.h>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf("%lld",&a)
#define din(a) scanf("%d",&a)
#define printlnlld(a) printf("%lld\n",a)
#define printlnd(a) printf("%d\n",a)
#define printlld(a) printf("%lld",a)
#define printd(a) printf("%d",a)
#define reset(a,b) memset(a,b,sizeof(a))
const int INF=0x3f3f3f3f;
using namespace std;
const double PI=acos(-1);
typedef long long ll;
typedef long double ld;
///Schlacht von Stalingrad
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
string command;
struct va
{
    string name;
    ll value;
} variables[900000];
ll cnt=0;
int DETERMINATION()
{
    ll ulim=1<<30;
    ulim*=(1<<17);
    ulim--;
   // cout<<ulim<<endl;
    ll lim=ulim+1;
    while(cin>>command)
    {
        if(command[0]=='d'&&command[1]=='e')
        {
            cin>>variables[cnt].name;
            cin>>variables[cnt].value;
            cout<<variables[cnt].name<<" = "<<variables[cnt].value<<endl;
            cnt++;
        }
        else
        {
            string a,b;
            cin>>a>>b;
            ll tmp=0,loc=0,loc2=0;
            for(int i=0; i<cnt; i++)
                if(variables[i].name==a)
                {
                    loc=i;
                    break;
                }
            for(int i=0; i<cnt; i++)
                if(variables[i].name==b)
                {
                    loc2=i;
                    break;
                }
            if(command[0]=='a')
                variables[loc].value=variables[loc2].value+variables[loc].value;
            else if(command[0]=='s')
                variables[loc].value=variables[loc].value-variables[loc2].value;
            else if(command[0]=='m'&&command[1]=='u')
                variables[loc].value=(variables[loc].value*variables[loc2].value)%lim;
            else if(command[0]=='d'&&command[1]=='i')
                variables[loc].value=variables[loc].value/variables[loc2].value;
            else if(command[0]=='m'&&command[1]=='o')
                variables[loc].value=variables[loc].value%variables[loc2].value;
            while(variables[loc].value>lim)
                variables[loc].value%=lim;
            while(variables[loc].value<0)
                variables[loc].value+=lim;
           cout<<variables[loc].name<<" = "<<variables[loc].value<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43874261/article/details/89505568