9 条题解

  • 4
    @ 2023-11-7 18:57:15

    已AC:

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll n, minn = 999999999999999, pos;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            if ((int)abs(n - pow(2, i)) > minn)
                break;
            if ((int)abs(n - pow(2, i)) < minn)
            {
                minn = abs(n - pow(2, i));
                pos = pow(2, i);
            }
        }
        cout << pos << endl;
        return 0;
    }
    
    • 2
      @ 2023-10-4 23:25:45
      #include<bits/stdc++.h>
      using namespace std;
      int n,a,b;
      int main()
      {
          a=2;
          cin>>n;
          while(a<n)
          {
              a*=2;
          }
          b=a/2;
          if(a-n<n-b)
          {
              cout<<a;
          }
          else
          {
              cout<<b;
          }
          return 0;
      }
      
      • 1
        @ 2023-9-3 20:13:31
        #include <bits/stdc++.h>
        using namespace std;
        int n,pre,cur = 1;
        int main()
        {
        cin >> n;
        while(cur < n)
        cur <<= 1;
        pre = cur >> 1;
        if (n - pre > cur - n) cout << cur;
        else cout << pre;
        return 0;
        }
        
        • 1
          @ 2023-7-29 22:21:04
          #include <bits/stdc++.h>
          using namespace std;
          int n,x=2;
          int main()
          {
              cin>>n;
              while(x<n)
                  x*=2;
              if(x-n<n-x/2)//判断比n大的数和比n小的数,哪个与n的绝对值更小
                  cout<<x<<endl;
              else
                  cout<<x/2<<endl;
              return 0;
          }
          
          • 1
            @ 2023-7-7 21:21:56
            #include <iostream>
            #include <cmath>
            using namespace std;
            int main()
            {
            	int n;
            	cin >> n;
            	int min = 2 * pow(10, 9);
            	int i = 3;
            	while (1)
            	{
            		if (abs(pow(2, i) - n) < min)
            		{
            			min = abs(pow(2, i) - n);
            		}
            		if (abs(pow(2, i + 1)) - n >= min)
            		{
            			break;
            		}
            		i++;
            	}
            	cout << pow(2, i);
            	return 0;
            }
            
            • 1
              @ 2023-7-6 8:38:13

              纯暴力就行,时间复杂度仅有 logn\log n

              #include <bits/stdc++.h>
              using namespace std;
              int n,k=1;
              int main(){
                  cin>>n;
                  while (k<n){
                      k*=2;
                  }
                  cout<<(k-n<n-k/2?k:k/2);
                  return 0;
              }
              
              • 1
                @ 2023-4-22 15:49:11
                #include <bits/stdc++.h> 
                using namespace std;
                int main()
                {
                    int n, x = 2;
                    cin >> n;
                    for (int i = 2; x < n; i++)
                    {
                        x *= 2;
                    }
                    if (n - x <= x * 2 - n)
                    {
                        cout << x;
                    }
                    else
                    {
                        cout << x * 2;
                    }
                    return 0;
                }
                
                • 1
                  @ 2022-12-10 10:24:36
                  #include <iostream>
                  using namespace std;
                  int main()
                  { 
                      int n, i;
                      cin >> n;
                      i = 2;
                      while (i < n)
                      { 
                          i = i * 2;
                      }
                      if (i - n < n - i / 2)
                      { 
                          cout<<i<<endl;
                      }
                      else
                      { 
                          cout << i / 2 << endl;
                      }
                      return 0;
                  }
                  //已AC
                  
                  • 0
                    @ 2023-7-30 20:49:23

                    我是老六

                    #include <iostream>
                    using namespace std;
                    int main()
                    {
                    cout<<64;
                    return 0;
                    }
                    
                    
                    
                    • 1

                    信息

                    ID
                    76
                    时间
                    1000ms
                    内存
                    16MiB
                    难度
                    1
                    标签
                    递交数
                    130
                    已通过
                    100
                    上传者