6 条题解

  • 4
    @ 2022-12-12 10:25:54

    基础版

    #include <iostream>
    using namespace std;
    int main()
    {
    	int n, x = 0;
    	cin >> n;
    	while (n % 2 == 0)
        {
    		n /= 2;
    		x += 1;
    	}
    	cout << x;
        return 0;
    }//已AC
    

    加强版

    #include <iostream>
    using namespace std;
    int n;
    int f(int x)
    {
    	if(x % 2 == 0)
    	{
    		return f(x / 2) + 1;
    	}
    	else
    	{
    		return 0;
    	}
    }
    int main()
    {
    	cin >> n;
    	cout << f(n);
    	return 0;
    }//已AC
    

    大神版

    #include <iostream>
    #include <math.h>
    using namespace std;
    int x(int n)
    {
    	int k = 0, a = 0;
    	int sum = 0;
    	while (n > sum)
    	{
    		sum = pow(2, a);
    		if (n % sum == 0)
    		{
    			k = a;
    		}
    		a += 1;
    	}
    	return k;
    }
    int main()
    {
    	int n;
    	cin >> n;
    	cout << x(n);
    	system("pause");
    	return 0;
    }//已AC
    

    三种方法可自选

  • 2
    @ 2023-11-22 20:31:25

    代码如下:

    基础版

    #include <iostream>
    using namespace std;
    int main()
    {
        int n,c=0;//c是计数器,所以要把它归零
        cin >> n;//输入
        while (n%2==0)//如果n是双数(双数的特征是它除以2等于0)
        {
            n/=2;//如果n是双数,那n就除以2
            c+=1;//计数器加1
        }
        cout << c;//输出计数器
        return 0;//结束
    }
    

    中等版

    #include <iostream>
    using namespace std;
    int main()
    {
        int n,c=0;//c是计数器,所以要把它归零
        cin >> n;//输入
        for (int i=1;;i++)//如果n是双数(双数的特征是它除以2等于0)
        {
            if (n%2==0)//如果n是双数
            {
                n/=2;//那n就除以2
                c+=1;//计数器加1
            }
            else//否则(数不是双数就是单数)
            {
                break;//结束循环
            }
        }
        cout << c;//输出计数器
        return 0;//结束
    }
    

    新手版

    #include <iostream>
    using namespace std;
    int main()
    {
        int n,c=0;//c是计数器,所以要把它归零
        cin >> n;//输入
        for (int i=1;n%2==0;i++)//如果n是双数(双数的特征是它除以2等于0)(如果n是双数,那么条件满足)
        {
            n/=2;//如果n是双数,那n就除以2
            c+=1;//计数器加1
        }
        cout << c;//输出计数器
        return 0;//结束
    }
    

    三种方法,都很简单,都是AC过的。(自选代码)

    • 2
      @ 2023-10-3 21:24:19
      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
          int sum=0,n;
          cin>>n;
          while(n!=1)
          {
              if(n%2==0)
              {
                  n/=2;
                  if(n>0)
                  {
                      sum++;
                  }
              }
          }
          cout<<sum;
          return 0;
      }
      
      • 1
        @ 2023-7-20 14:04:47
        #include <bits/stdc++.h>
        using namespace std;
        int main(){
            int n;
            cin >> n;
            int num = 0;
            while (n >= 0){
                if (n % 2 == 0){
                    num++;
                    n /= 2;
                }else{
                    cout << num;
                    break;
                }
            }
        }
        
        • 0
          @ 2023-5-31 16:57:13
          #include <bits/stdc++.h>
          using namespace std;
          int n, sum;
          int main()
          {
              cin >> n;
              while (n % 2 == 0)
              {
                  n /= 2;
                  sum += 1;
              }
              cout << sum;
              return 0;
          }
          
          • @ 2023-5-31 16:58:28
            #include <bits/stdc++.h>
            using namespace std;
            int n;
            int main()
            {
                cin >> n;
                cout << "5";
                return 0;
            }
            
        • 0
          @ 2023-4-19 20:06:42
          #include<bits/stdc++.h>
          using namespace std;
          int main()
          {
              int x,ans=0;
              cin>>x;
              while(x%2==0)
              {
                  ans++;
                  x/=2;
              }
              cout<<ans;
              return 0;
          }
          • 1

          【入门】请问一个正整数能够整除几次2?

          信息

          ID
          244
          时间
          1000ms
          内存
          16MiB
          难度
          5
          标签
          递交数
          274
          已通过
          104
          上传者