首頁 > web前端 > js教程 > 主體

Javascript 程式檢查兩個數字是否是彼此的位元循環

王林
發布: 2023-09-01 16:05:12
轉載
1238 人瀏覽過

Javascript 程序检查两个数字是否是彼此的位循环

問題陳述 - 我們給了兩個整數,需要檢查這兩個數字是否是彼此的位元循環。

在 JavaScript 中,每個整數都是 32 位元二進位數,表示 0 和 1。這裡,我們需要檢查是否旋轉了第一個數字的 32 位元字串;我們可以在第一個數字總共 32 次旋轉中獲得或不獲得第二個數字的 32 位元字串。

使用 ToString() 方法檢查兩個數字是否相互位元迴圈

toString()方法用於將整數轉換為32位元二進位數字字串。之後,我們可以在二進位字串中新增前導零,使其長度為 32 位元。接下來,我們可以將數字的二進位字串與其自身連接起來,並檢查第二個數字的二進位字串是否作為合併字串的子字串存在。

文法

使用者可以按照以下語法檢查連接字串後兩個數字是否相互位元循環。

let num1BinaryDouble = num1Binary + num1Binary;
let isBitRotation = num1BinaryDouble.includes(num2Binary)
登入後複製

演算法

  • 第 1 步 - 使用 toString() 方法並傳遞 2 作為其參數,將兩個數字轉換為二進位字串。

  • 第 2 步 - 接下來,我們需要將兩個字串的大小設為 32 位元。因此,請在兩個二進位字串中新增前導零。

  • 步驟 3 - 將 num1 的二進位字串合併到自身。

  • 步驟 4 - 檢查合併後的字串是否包含 num2 的二進位字串。如果是,則表示兩個數字都是彼此的位元循環。

範例 1

在下面的範例中,checkBitRotations() 函數實作了上述演算法,以確保兩個數字是否是彼此的位元循環。在輸出中,使用者可以觀察到 1 和 2 是彼此的位元循環,但 1 和 5 不是。

<html>
<body>
   <h3>Checking if <i> two numbers are bit rotations of each other or not </i> in JavaScript</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 1;
      let num2 = 2;
      let num3 = 5;
      function checkBitRotation(num1, num2) {
         let num1Binary = num1.toString(2);
         let num2Binary = num2.toString(2);
         // append remaining zeros at the start of num1BInary and num2Binary to make it's length 32
         while (num1Binary.length < 32) {
            num1Binary = "0" + num1Binary;
         }
         while (num2Binary.length < 32) {
            num2Binary = "0" + num2Binary;
         }
         // double the string
         let num1BinaryDouble = num1Binary + num1Binary;
         // check if num2Binary is present in num1BinaryDouble
         if (num1BinaryDouble.includes(num2Binary)) {
            return true;
         } else {
            return false;
         }
      }
      output.innerHTML += "The " + num1 + " and " + num2 + " are bit rotations of each other " + checkBitRotation(num1, num2) + "<br>";
      output.innerHTML += "The " + num1 + " and " + num3 + " are bit rotations of each other " + checkBitRotation(num1, num3) + "<br>";
   </script>
</body>
</html>
登入後複製

使用 For 迴圈檢查兩個數字是否相互位元迴圈

在這種方法中,我們將把數字轉換為二進位字串。之後,我們將使用 for 迴圈來取得第一個數字的所有旋轉,並將所有旋轉與第二個數字進行比較。如果第一個數字的任何旋轉與第二個數字匹配,則它們是彼此的位元旋轉。

文法

使用者可以按照下面的語法來匹配第一個數字與第二個數字的所有旋轉,並確保它們是彼此的位元旋轉。

for (let i = 0; i < num1Binary.length; i++) {
   if (num1Binary === num2Binary) {
      return true;
   }
   num1Binary = num1Binary[num1Binary.length - 1] + num1Binary.substring(0, num1Binary.length - 1);
}
登入後複製

在上面的語法中,我們將第一個數字與第二個數字逐一進行比較,如果匹配,則傳回 true。

演算法

  • 第 1 步 - 使用 toString() 方法將兩個數字轉換為二進位字串。

  • 第 2 步 - 現在,附加前導零以使它們的長度相等。

  • 第 3 步 - 使用 for 迴圈迭代第一個字串。

  • 第 4 步 - 如果 num1Binary 與 num2Binary 匹配,則傳回 true。

  • 步驟 5 - 在 for 迴圈中,如果第一個數字的目前旋轉與第二個數字不匹配,則旋轉第一個數字並獲得新的旋轉。

  • 第 6 步 - 繼續將下一個輪換與第二個輪換匹配,直到任何輪換匹配。如果任何旋轉不匹配,則傳回 false。

範例 2

在下面的範例中,我們實作了上述演算法來檢查位元旋轉。在這裡,我們逐一取得第一個數字的每個旋轉,並將它們與第二個數字進行比較。如果任何旋轉匹配,我們將返回 true,用戶可以在輸出中觀察到。

<html>
<body>
   <h3>Checking if <i> two numbers are bit rotations of each other or not </i> in JavaScript</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 122;
      let num2 = 2147483678;
      let num3 = 1;
      function checkBitRotation(num1, num2) {
         let num1Binary = num1.toString(2);
         let num2Binary = num2.toString(2);
         // adding leading zeros to make both numbers of the same length
         while (num1Binary.length < num2Binary.length) {
            num1Binary = "0" + num1Binary;
         }
         // checking num1Binary and num2Binary are rotations of each other using for loop
         for (let i = 0; i < num1Binary.length; i++) {
            if (num1Binary === num2Binary) {
               return true;
            }
            num1Binary = num1Binary[num1Binary.length - 1] + num1Binary.substring(0, num1Binary.length - 1);
         }
         return false;
      }
      output.innerHTML += "The " + num1 + " and " + num2 + " are bit rotations of each other " + checkBitRotation(num1, num2) + "<br>";
      output.innerHTML += "The " + num1 + " and " + num3 + " are bit rotations of each other " + checkBitRotation(num1, num3) + "<br>";
   </script>
</body>
</html>
登入後複製

使用者學習了兩種不同的方法來檢查兩個數字是否是彼此的位元循環。在第一種方法中,我們將第一個字串與其自身連接起來,並檢查第二個數字是否作為子字串存在。在第二種方法中,我們使用 for 迴圈找到第一個數字的所有位元旋轉,並將它們與第二個數字進行匹配。

以上是Javascript 程式檢查兩個數字是否是彼此的位元循環的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!