Instruction Decoder+SPOJ+机智题

 Instruction Decoder

Problem code: ICODER


Mathews uses a brand new 16-bit instruction processor. (Yeah i am being sarcastic!). It has one register (say R) and it supports two instructions: 

  • ADD X; Impact: R = (R + X) mod 65536
  • MUL X; Impact: R = (R * X) mod 65536
  • [For both instructions 0 <= X <= 65535]
Mathews sees a segment of code, but doesnot know what value the register had before the code was being executed. How many possible values can the register have after the segment completed execution?


Input Format:
The input file consists of multiple testcases. 
The first line of each testcase contains one integer,  N. (1 <= N <= 100,000).
The following  N lines contain one instructions each. 
Input terminates with a line containing N=0, which must not be processed. 

Output Format:
For each testcase print one integer in a single line, denoting the number of different values the register can take after code execution.

Sample Input:
1
ADD 3
1
MUL 0
5
MUL 3
ADD 4
MUL 5
ADD 3
MUL 2
8
ADD 32
MUL 5312
ADD 7
MUL 7
ADD 32
MUL 5312
ADD 7
MUL 7
0
Sample Output:
65536
1
32768

16

解决方案:仔细观察,会发现一个规律,每乘多一个2,可能的数就会减小一倍,乘0要特殊判断。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char op[100];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int temp;
        int cnt=65536;
        bool flag=true;
        for(int i=0; i<n; i++)
        {

            scanf("%s %d",op,&temp);
            if(op[0]=='M')
            {
                if(temp==0)
                {
                    flag=false;
                }
                while(temp%2==0)
                {
                    cnt>>=1;
                    temp/=2;
                }
            }
        }
        if(!flag||!cnt)
            printf("1\n");
        else
            printf("%d\n",cnt);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值