美文网首页
腾讯2017秋招秋招笔试编程题-----2

腾讯2017秋招秋招笔试编程题-----2

作者: 王伯庸 | 来源:发表于2018-09-20 13:32 被阅读0次

    腾讯2017秋招笔试题,位运算---2

    题目描述

    游戏里面有很多各式各样的任务,其中有一种任务玩家只能做一次,这类任务一共有1024个,任务ID范围[1,1024]。请用32个unsigned int类型来记录着1024个任务是否已经完成。初始状态都是未完成。 输入两个参数,都是任务ID,需要设置第一个ID的任务为已经完成;并检查第二个ID的任务是否已经完成。 输出一个参数,如果第二个ID的任务已经完成输出1,如果未完成输出0。如果第一或第二个ID不在[1,1024]范围,则输出-1。

    c++代码如下:

    #include<iostream>
    #include<cmath>
    #include<string>
    using namespace std;
    int main()
    {
        unsigned int remember[32];
        unsigned int a,b;
        while(cin>>a>>b)
        {
            if(a<0||a>1024||b<0||b>1024)
            {
                cout<<-1<<endl;
                continue;
            }
            unsigned int first=a/32-1;
            unsigned int second=a%32-1;
            remember[first]|=1<<second;
            unsigned int bfirst=b/32-1;
            unsigned int bsecond=b%32-1;
            unsigned int temp=1<<bsecond;
            if(remember[bfirst]&temp)
                cout<<1<<endl;
            else
                cout<<0<<endl;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:腾讯2017秋招秋招笔试编程题-----2

          本文链接:https://www.haomeiwen.com/subject/qtzgnftx.html