最新公告
  • 欢迎您光临码农资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!加入我们
  • JavaScript 程序计算可被 4 整除的旋转次数

    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整除,则增加该值count 变量减 1。

    • 步骤 4 – 使用 substring() 方法从第一个索引获取子字符串。此外,将字符串的第一个字符附加到子字符串的末尾。这样,我们就可以旋转字符串并生成一个新的数字。

    示例 1

    在下面的示例中,我们定义了 countRotations() 函数,该函数实现上述算法并返回可被 4 整除的总旋转次数。在输出中,用户可以观察到该数字的总旋转次数可被 4 整除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>
    

    检查每对 2 位数字是否能被 4 整除

    如果任何数字的最后 2 位数字可以被 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 整除。如果是,我们增加 count 变量的值。

    算法

    • 步骤 1 – 如果数字是个位数,检查它是否能被 4 整除。如果是,则返回 1;否则返回 1。否则返回0。

    • 第 2 步 – 如果数字包含两个或更多数字,则将“count”变量初始化为 0。

    • 第 3 步 – 现在,我们需要使用数字的最后一位数字和第一位数字创建一对。使用模运算符获取最后一位数字,使用 Math.log() 方法获取第一个数字。

    • 步骤 4 – 将最后一位数字乘以 10,再将第一位数字乘以 10。然后检查结果是否能被4整除。如果能被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 整除的两位数字对的总数。

    想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
    本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
    如有侵权请发送邮件至1943759704@qq.com删除

    码农资源网 » JavaScript 程序计算可被 4 整除的旋转次数
    • 14会员总数(位)
    • 15748资源总数(个)
    • 1095本周发布(个)
    • 35 今日发布(个)
    • 112稳定运行(天)

    提供最优质的资源集合

    立即查看 了解详情