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
在下面的示例中,我们定义了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>
检查每一对2位数是否能被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 - 如果数字包含两个或更多个数字,则将计数变量初始化为0。
-
步骤3 - 现在,我们需要使用数字的最后一个数字和第一个数字创建一个数字对。使用模运算符获取最后一个数字,使用Math.log()方法获取第一个数字。
-
步骤4 - 将最后一个数字乘以10,然后将第一个数字乘以该结果。然后,检查结果是否能被4整除。如果是,则增加计数的值。
-
步骤5 - 使用while循环来检查其他两个数字的对。在while循环中,使用模运算符获取最后一个数字和倒数第二个数字。使用这两个数字创建一个对,并检查该对是否能被2整除。如果是,则增加计数的值。
示例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整除的两位数对的总数。