题解 AT3718 【[ABC081B] Shift only】

题目传送门


分析

直接暴力。
我们可以根据题意进行模拟,使用二重循环即可。


代码讲解

  1. 定义变量\(n\)和计数数组\(cnt\),再定义数组\(a\)并输入。
    int a[1000000];
    int n,cnt=0;;
    cin>>n;
    int n,cnt=0;;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
  1. 使用一个死循环,当无法继续\(\div2\)时,结束循环。
        bool pd=true;//①
        for(int j=1;j<=n;j++) 
        {
            if(a[j]%2!=0)
            {
                pd=false;
                break;
            } 
        }
        if(pd==false)break;//②
        if(pd)
        {
            cnt++;
            for(int j=1;j<=n;j++)
                a[j]/=2;
        } 

①判断能否被\(2\)整除使用的布尔型变量。
②循环的结束条件。

  1. 输出最终结果。
    cout<<cnt; 

CODE

#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int main()
{
    int n,cnt=0;;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;;i++)
    {
        bool pd=true;
        for(int j=1;j<=n;j++) 
        {
            if(a[j]%2!=0)
            {
                pd=false;
                break;
            } 
        }
        if(pd==false)break;
        if(pd)
        {
            cnt++;
            for(int j=1;j<=n;j++)
                a[j]/=2;
        } 
    } 
    cout<<cnt; 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tearing/p/12376401.html
今日推荐