计算能被 8 整除的旋转次数的 JavaScript 程序


问题陈述 − 给定一个数字,我们需要旋转该数字,并找到能被 8 整除的旋转总数。

在这里,我们将学习两种不同的方法来计算能被 8 整除的旋转次数。

旋转数字并检查旋转结果是否能被 8 整除

第一种方法是旋转数字,逐个获取所有可能的旋转结果。同时,检查旋转结果是否能被 8 整除。如果是,则将计数加 1。

语法

用户可以按照以下语法来计算通过旋转数字得到的能被 8 整除的旋转次数。

for ( ) {
   str = lastDigit + str.substring(0, str.length - 1);
   let num = parseInt(str);
   if (num % 8 == 0) {
      count++;
   }
}

在上述语法中,我们取数字字符串的最后一位数字,并将其附加到字符串的开头,从而旋转数字。

算法

  • 步骤 1 − 将计数变量初始化为 0,表示初始计数为零。

  • 步骤 2 − 使用 for 循环遍历数字字符串,并将总旋转次数设置为数字字符串的长度。

  • 步骤 3 − 在 for 循环中,获取数字字符串的最后一位数字。同时,获取包含前 n-1 位数字的子字符串。

  • 步骤 4 − 将最后一位数字附加到子字符串的开头,以旋转数字字符串。

  • 步骤 5 − 使用 parseInt() 方法从字符串中提取数字。

  • 步骤 6 − 检查旋转结果是否能被 8 整除。如果是,则将计数的值加 1。

  • 步骤 7 − 使用 for 循环检查所有旋转结果后,返回计数的值。

示例 1

在下面的示例中,rotationsDivisibleBy8() 函数将一个数字作为参数,并返回能被 8 整除的旋转总数。此外,我们首先使用 toString() 方法将数字转换为字符串,然后实现上述算法来计算能被 8 整除的旋转次数。

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 8 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      const rotationsDivisibleBy8 = (number) => {
         let count = 0;
         //Count rotations divisible by 8 by rotating numbers
         let str = number.toString();
         for (let i = 0; i < str.length; i++) {
         
            //Get the last character of the string
            let lastDigit = str[str.length - 1];
            
            // rotating number
            str = lastDigit + str.substring(0, str.length - 1);
            
            // convert string to integer
            let num = parseInt(str);
            
            //Check if num is divisible by 8
            if (num % 8 == 0) {
               count++;
            }
         }
         return count;
      }
      let number = 90645232432;
      output.innerHTML = "Total count of rotations divisible by 8 of " + number + " is " + rotationsDivisibleBy8(number);
   </script>
</body>
</html>

检查三位数字对是否能被 8 整除

如果任何数字的最后三位数字能被 8 整除,我们可以说整个数字能被 8 整除。因此,在这里我们可以取连续的三位数字对,并检查该对是否能被 8 整除。如果是,则表示包含该三位数结尾的旋转结果能被 8 整除。

语法

用户可以按照以下语法来计算能被 8 整除的旋转次数。

for ( ) {
   let pairOf3 = numStr.substring(i, i + 3);
   if (pairOf3 % 8 == 0) {
      count++;
   }
}

在上述语法中,我们使用了 substring() 方法来获取三位数字对。

算法

  • 步骤 1 − 使用 toString() 方法将数字转换为字符串。

  • 步骤 2 − 如果数字的长度等于 1,则如果数字能被 8 整除,则返回 1;否则,返回 0。

  • 步骤 3 − 如果数字的长度等于 2,则检查两个可能的旋转结果中能被 8 整除的旋转次数,并返回计数。

  • 步骤 4 − 对于超过 3 位数字的数字,使用 substring() 方法提取连续的三位数字对。之后,检查该对是否能被 8 整除,并增加计数的值。

  • 步骤 5 − 同时,检查包含最后两位数字和第一位数字、最后一位数字和前两位数字的对,并相应地增加“计数”的值。

示例 2

在下面的示例中,我们使用 for 循环和 substring() 方法来获取 n-2 个三位数字对,并检查它们是否能被 8 整除。在输出中,用户可以看到给定的数字包含总共 5 个能被 8 整除的旋转结果。

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 8 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      const rotationsDivisibleBy8 = (number) => {
         let count = 0;
         let numStr = number.toString();
         let n = numStr.length;
         if (n == 1) {
            // for 1 digit
            return number % 8 == 0 ? 1 : 0;
         }
         else if (n == 2) {
         
            // for 2 digits
            if (number % 8 == 0) {
               count++;
            }
            let temp = numStr.substring(1, 2) + numStr.substring(0, 1);
            if (temp % 8 == 0) {
               count++;
            }
            return count;
         }
         else {
         
            // for 3 digits
            for (let i = 0; i < n - 2; i++) {
               let pairOf3 = numStr.substring(i, i + 3);
               if (pairOf3 % 8 == 0) {
                  count++;
               }
            }
            
            // for last two and first digit
            let lastTwo = numStr.substring(n - 2, n);
            let firstDigit = numStr.substring(0, 1);
            let lastTwoFirstDigit = lastTwo + firstDigit;
            if (lastTwoFirstDigit % 8 == 0) {
               count++;
            }
            
            // for last digit and first two digits
            let lastDigit = numStr.substring(n - 1, n);
            let firstTwo = numStr.substring(0, 2);
            let lastDigitFirstTwo = lastDigit + firstTwo;
            if (lastDigitFirstTwo % 8 == 0) {
               count++;
            }
            return count;
         }
      }
      let number = 104104104104104;
      output.innerHTML = "Total count of rotations divisible by 8 of " + number + " is " + rotationsDivisibleBy8(number);
   </script>
</body>
</html>

用户学习了两种不同的方法来计算能被 8 整除的旋转总数。在第一种方法中,我们获取所有可能的旋转结果,并检查它们是否能被 8 整除。在第二种方法中,我们利用数字能被 8 整除的特性,即数字的最后三位数字必须能被 8 整除。

更新于:2023年4月20日

浏览量:105

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.