JavaScript 程式計算可被 4 整除的旋轉次數


在本教程中,我們將學習如何計算給定數字可被 4 整除的旋轉總數。

問題陳述 - 我們給定一個數字值。我們需要按順時針或逆時針方向旋轉數字,並計算可被 4 整除的旋轉總數。

在這裡,我們將學習兩種不同的方法來計算可被 4 整除的旋轉次數。

旋轉數字並檢查它是否可被 4 整除

在這種方法中,我們將首先將數字轉換為字串。我們可以對長度為 n 的字串進行 n 次旋轉。我們將刪除字串的第一個字元,並將其新增到字串的末尾。之後,我們可以檢查旋轉生成的新的數字是否可被 4 整除。

語法

使用者可以按照以下語法來檢查旋轉是否可被 4 整除以及旋轉數字字串。

for ( ) {
   if (parseInt(numStr) % 4 == 0) {
      count++;
   }
   numStr = numStr.substring(1, len) + numStr[0];
}

在上述語法中,parseInt() 方法用於將字串轉換為數字,substring() 方法用於旋轉字串。

演算法

  • 步驟 1 - 使用 toString() 方法將數字轉換為字串。

  • 步驟 2 - 使用 for 迴圈對長度為 'n' 的字串進行總共 'n' 次旋轉。

  • 步驟 3 - 使用 parseInt() 方法將字串轉換為數字,並檢查該數字是否可被 4 整除。如果數字可被 4 整除,則將計數變數的值增加 1。

  • 步驟 4 - 使用 substring() 方法獲取從第 1 個索引開始的子字串。此外,將字串的第一個字元附加到子字串的末尾。這樣,我們可以旋轉字串並生成一個新數字。

示例 1

在下面的示例中,我們定義了 countRotations() 函式,該函式實現了上述演算法並返回可被 4 整除的旋轉總數。在輸出中,使用者可以觀察到數字的總共 2 次旋轉可被 4 整除。

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 4 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      // JavaScript program to find the total count of rotations divisible by 4
      let countRotations = (number) => {
         let numStr = number.toString();
         let len = numStr.length;
         let count = 0;
         
         // Loop to traverse the string
         for (let i = 0; i < len; i++) {
         
            // Check if the string is divisible by 4
            if (parseInt(numStr) % 4 == 0) {
               count++;
            }
            
            // Rotate the string
            numStr = numStr.substring(1, len) + numStr[0];
         }
         return count;
      }
      let number = 121342435345;
      output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
   </script>
</body>
</html>

檢查每對兩位數是否可被 4 整除

如果任何數字的最後兩位數可被 4 整除,我們可以說該數字可被 4 整除。在旋轉數字時,每對兩位數都出現在數字的末尾。因此,我們可以檢查是否有任何一對兩位數可被 4 整除;我們可以說與該對相關的旋轉次數可被 4 整除。

語法

使用者可以按照以下語法從數字中提取兩位數對並檢查它是否可被 4 整除。

let lastDigit = num % 10;
num = Math.floor(num / 10);
let secondLastDigit = num % 10;
if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
   count++;
}

在上述語法中,我們從數字中獲取最後一位和倒數第二位。之後,我們使用這兩位建立一個兩位數,並檢查它是否可被 4 整除。如果是,則增加計數變數的值。

演算法

  • 步驟 1 - 如果數字是一位數,則檢查它是否可被 4 整除。如果是,則返回 1;否則,返回 0。

  • 步驟 2 - 如果數字包含兩位或更多位,則將 'count' 變數初始化為 0。

  • 步驟 3 - 現在,我們需要使用數字的最後一位和第一位建立一個對。使用模運算子獲取最後一位,使用 Math.log() 方法獲取第一位。

  • 步驟 4 - 將最後一位乘以 10,並將第一位乘以它。之後,檢查結果是否可被 4 整除。如果是,則將計數的值增加 1。

  • 步驟 5 - 使用 while 迴圈檢查其他兩位數對。在 while 迴圈中,使用模運算子獲取最後一位和倒數第二位。使用這兩位建立一個對,並檢查該對是否可被 2 整除。如果是,則將計數的值增加 1。

示例 2

在此示例中,countRotations() 函式計算可被 4 整除的兩位數對的數量。它實現了上述演算法,並在所有操作完成後返回計數的值。

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 4 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      function countRotations(number) {
         //If the length of the number is equal to 1, check if the digit is a multiple of 4
         if (number < 10) {
            return number % 4 == 0 ? 1 : 0;
         } else {
            // Initialize count of rotations divisible by 4
            let count = 0;
            let num = number;
            //Check for the last digit and the first digit
            let lastDigit = number % 10;
            // Get the first digit from the number
            let firstDigit = Math.floor(number / Math.pow(10, Math.floor(Math.log10(number))));
            //If the last digit and first digit are divisible by 4, then add 1 to count
            if ((lastDigit * 10 + firstDigit) % 4 == 0) {
               count++;
            }
            while (num > 0) {
               // get last digit of number
               let lastDigit = num % 10;
               // get second last digit of number
               num = Math.floor(num / 10);
               let secondLastDigit = num % 10;
               if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
                  count++;
               }
            }
            return count;
         }
      }
      let number = 90645232432;
      output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
   </script>
</body>
</html>

使用者學習瞭如何找到可被 4 整除的數字的旋轉總數。我們已經看到了兩種不同的方法。第一種方法將數字轉換為字串,旋轉字串,再次將字串轉換為數字,並檢查新生成的旋轉是否可被 4 整除。

第二種方法計算可被 4 整除的兩位數對的總數。

更新於: 2023年4月20日

125 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告