nyoj 412 Same binary weight ()

Same binary weight

Time Limit: 300 ms | Memory Limit: 65535 KB
Difficulty: 3
 
describe

  The binary weight of a positive  integer is the number of 1's in its binary representation.for example,the decmial number 1 has a binary weight of 1,and the decimal number 1717 (which is 11010110101 in binary) has a binary weight of 7.Give a positive integer N,return the smallest integer greater than N that has the same binary weight as N.N will be between 1 and 1000000000,inclusive,the result is guaranteed to fit in a signed 32-bit interget.

 
enter
  The input has multicases and each case contains a integer N.
output
  For each case,output the smallest integer greater than N that has the same binary weight as N.
sample input
  1717
  4
  7
  12
  555555
Sample output
  1718
  8
  11
  17
  555557
/**
    Analysis: The question is, find a number greater than the number given in the question && the number of 1 in the binary is the same as the number of 1 in the binary of the given number
    Algorithm 1:
        ①, the calculated binary numbers are traversed from left to right
        ②, if the i-th digit is 1 && the i-th + 1 digit is 0:
                swap (A[i], A[i + 1])
                pos = i
                break
        ③, move all 1s from the 0th to the posth position to the far right
**/ 

C/C++ code implementation (algorithm 1):

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <stack>
#include <queue>
#include <map>

using namespace std;

int n;

int main () {
    while (~scanf ("%d", &n)) {
        int A [10000] = {0}, ans = 0, m = n, pos, k = 0, cnt = 0;
        
        while (m) {
            A [k ++] = m%2;
            m /= 2;
        }        
        for (int i = 0; i < k; ++ i) {
            if (A [i] && !A[i + 1]) {
                swap (A[i], A[i + 1]);
                pos = i;
                break;
            }
            if (A[i]) ++ cnt;
        }
        for (int i = 0; i < pos; ++ i) {
            if (cnt) {
                A [i] = 1;
                cnt --;
            } else  {
                A [i] = 0;
            }
        }
        for (int i = 0; i <= k; ++ i) {
            ans += pow (2, i) * A[i]; 
        }
        printf ("%d\n", ans); 
    }
}

Algorithm 2:

/**
  The difference from algorithm 1 is the algorithm for calculating binary ==> bitset <32> A (n) (Note: convert n to binary and store it in A)
    binary to decimal ==> A.to_ulong()
**/

C/C++ implements Algorithm 2:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <stack>
#include <queue>
#include <map>
#include <bitset>

using namespace std;

int n;

int main () {
    while (~scanf ("%d", &n)) {
        bitset < 32 > A (n); // Convert n to binary and store it in A 
        int pos, cnt = 0 ;
         for ( int i = 0 ; i <= 32 ; ++ i) {
             if (A [ i] && !A[i + 1 ]) {
                A [i] = 0;
                A [i + 1] = 1;
                pos = i;
                break;
            }
            if (A[i]) ++ cnt;
        }
        for (int i = 0; i < pos; ++ i) {
            if (cnt) {
                A [i] = 1;
                cnt --;
            } else  {
                A [i] = 0;
            }
        }
    
        printf ( " %d\n " , A.to_ulong()); // Convert the resulting binary to unsigned decimal 
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325066359&siteId=291194637