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 整除的两位数对的总数。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP