首頁 > 後端開發 > C++ > 印在C程式中產生形如2^X - 1的數字的步驟

印在C程式中產生形如2^X - 1的數字的步驟

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2023-09-07 14:01:02
轉載
615 人瀏覽過

打印在C程序中生成形如2^X - 1的数字的步骤

給定一個數字 n,我們必須使用異或運算來列印將數字製成 2^X-1 形式的步驟。

  • 我們應該進行異或任意 2^M-1 的數字,其中 M 由您選擇,在奇數步長。
  •  在偶數步長,將數字增加1

繼續執行該步驟,直到n變為2^X-1,並列印所有步驟

範例

1

2

3

4

5

6

7

8

9

Input: 22

Output:

   Step 1 : Xor with 15

   Step 2: Increase by 1

   Step 3 : Xor with 7

   Step 4: Increase by 1

   Step 5 : Xor with 1

Input:7

Output: No Steps to be performed

登入後複製

演算法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

int find_leftmost_unsetbit(int n)

START

STEP 1 : DECLARE AND ASSIGN ind = -1, i = 1

STEP 2 : LOOP WHILE n

   IF !(n & 1) THEN,

      ASSIGN ind WITH i

   END IF

   INCREMENT i BY 1

   LEFT SHIFT n BY 1

END WHILe

STEP 3 : RETURN ind

STOP

void perform_steps(int n)

START

STEP 1 : DECLARE AND ASSIGN left = find_leftmost_unsetbit(n)

STEP 2 : IF left == -1 THEN,

   PRINT "No Steps to be performed"

   RETURN

END IF

STEP 3 : DECLARE AND ASSIGN step = 1

STEP 4 : LOOP WHILE find_leftmost_unsetbit(n) != -1

   IF step % 2 == 0 THEN,

      INCREMENT n BY 1

      PRINT "Step n : Increase by 1</p><p>"

   ELSE

      DECLARE AND ASSIGN m =

      find_leftmost_unsetbit(n)

      AND SET num = (pow(2, m) - 1)

      SET n = n ^ num

      PRINT "Step N : Xor with Num

   END IF

   INCREMENT step BY 1

END LOOP

STOP

登入後複製

範例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

#include <stdio.h>

#include <math.h>

//To find the leftmost bit

int find_leftmost_unsetbit(int n){

   int ind = -1;

   int i = 1;

   while (n) {

      if (!(n & 1))

         ind = i;

      i++;

      n >>= 1;

   }

   return ind;

}

void perform_steps(int n){

   // Find the leftmost unset bit

   int left = find_leftmost_unsetbit(n);

   //If there is no bit

   if (left == -1) {

      printf("No Steps to be performed</p><p>");

      return;

   }

   // To count the number of steps

   int step = 1;

   // Iterate till number is in form of 2^x - 1

   while (find_leftmost_unsetbit(n) != -1) {

      // if the step is even then increase by 1

      if (step % 2 == 0) {

         n += 1;

         printf("Step %d: Increase by 1</p><p>", step);

      }

      // if the step is odd then xor with 2^m-1

      else {

         // Finding the leftmost unset bit

         int m = find_leftmost_unsetbit(n);

         int num = (int)(pow(2, m) - 1);

         n = n ^ num;

         printf("Step %d : Xor with %d</p><p>", step, num);

      }

      // To increase the steps

      step += 1;

   }

}

int main(){

   int n = 22;

   perform_steps(n);

   return 0;

}

登入後複製

輸出

如果我們執行上面的程序,那麼它將產生以下輸出-

1

2

3

4

5

Step 1 : Xor with 15

Step 2 : Increase by 1

Step 3 : Xor with 7

Step 4 : Increase by 1

Step 5 : Xor with 1

登入後複製

以上是印在C程式中產生形如2^X - 1的數字的步驟的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板