欢迎光临散文网 会员登陆 & 注册

相邻的1

2023-07-07 15:20 作者:喵雕沙  | 我要投稿

题目描述

给定N 个正整数 x ,判断 X  的二进制中是否有相邻的1。例如 1011, 表示有相邻的1.

(1<=n<=1000)

输入

输入格式:
     第一行一个整数N,第二行N个正整数。

输出

N行,”yes“   表示有相邻的1 ,“no"表示 没有

样例输入 复制

1 3

样例输出 复制

yes

#include <iostream>
using namespace std;
int main()
{
    int N,x;
    cin>>N;
    for(int i=1;i<=N;i++)
    {
        cin>>x;
        bool ans=false;
        bool is=false;
        while(x!=0)
        {
            if(x%2==1)
            {
                if(ans==true)
                {
                    cout<<"yes"<<endl;
                    is=true;
                    break;
                }
                else
                    ans=true;
            }
            else
                ans=false;
            x/=2;
        }
        if(!is)
            cout<<"no"<<endl;
    }
    return 0;
}


相邻的1的评论 (共 条)

分享到微博请遵守国家法律