回文链长算法 [英] Palindrome chain length algorithm
问题描述
我正在尝试编写一个函数来计算找到回文所需的次数。对于那些不熟悉这个词的人来说,回文是一个单词,它是一个单词,短语或序列,它向后读取与向前相同的单词。例如女士,阿巴等。在这个函数中,我想使用回文数。条件是:
1)如果输入数字是回文数,则返回0,因为创建回文需要0步。
2)如果数字不是a回文,我需要添加一个反向输入并检查数字是否为回文。
例如:
输入为87
I am trying to write a function that counts how many times it took to find a palindrome. For those who are new to this word, a palindrome is a word which is a word, phrase, or sequence that reads the same backward as forward. For example "madam" , "aba" and ect. In this function, I would like to work with palindrome numbers. The conditions are:
1)if the input number is palindrome, return 0, since it took 0 steps to create a palindrome.
2) If the number is NOT a palindrome, I would need to add a reversed input to it and check if the number is a palindrome.
For example:
the input is 87
87 + 78 = 165; 165 + 561 = 726; 726 + 627 = 1353; 1353 + 3531 = 4884
4884是回文并且我们花了4步才能实现这一目标,所以输出必须是4
4884 is palindrome and it took us 4 steps to achieve this, so the output has to be 4
CountPalSteps(87) -> 4
如何计算这个链?我想要实现的目标:
1)如果数字不是回文数,请通过添加反转数字使其成为一个数字。例如
How can I count this chain? What I would like to achieve:
1)If a number is not a palindrome, make it one by adding a reversed number. For example
palin(123){
123 + 321 = 444
}
现在444是回文并且它花了我实现这一目标的一步!所以,输出应该是1!
注意,我只需要使用额外的!
我尝试过:
444 is palindrome now and it took me one step to achieve this! So, the output should be 1!
Note, I would only need to use addition!
What I have tried:
function palin(arg) {
var reversed = +arg.toString().split("").reverse().join("");
if(arg === reversed){ return "The number is palindrome"}
else {
var sum = arg + reversed;
var reversSum = +sum.toString().split("").reverse().join("");
if(sum !== reversSum){//what should I put in here?}
}
}
推荐答案
试试这个
try this
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script>
var count = 0;
var final = 0;
function GetpalinCount(arg) {
if (!isPalin(arg)) {
count++;
var reversed = arg.toString().split("").reverse().join("");
var sum = parseInt(arg) + parseInt(reversed);
GetpalinCount(sum);
}
else {
final = arg;
}
}
function isPalin(value)
{
var reversed = value.toString().split("").reverse().join("");
return value.toString() === reversed;
}
var find = prompt('enter the integer value');
GetpalinCount(find);
alert('count= ' + count + '\n' + 'final Value= ' + final);
</script>
</head>
<body>
</body>
</html>
演示: - JSFiddle [ ^ ]
这是HomeWork,目的是让你思考如何解决p roblem。
一切都在陈述中:
This is HomeWork, the goal is to make you think about how to solve the problem.
everything is in the statements:
1)如果输入数字是回文数,返回0,因为创建一个回文需要0步。
1)if the input number is palindrome, return 0, since it took 0 steps to create a palindrome.
你的
Your
return "The number is palindrome"
与声明不符。它应该很容易纠正。
do not match the statement. It should be easy to correct.
2)如果数字不是回文,我需要添加一个反向输入并检查该数字是否为回文。
2) If the number is NOT a palindrome, I would need to add a reversed input to it and check if the number is a palindrome.
你已经有总和
,你只需检查是否总和
是回文。并在步数上加1。
利用你的知识:
palin(arg)
87 + 78 = 165; 165 + 561 = 726; 726 + 627 = 1353; 1353 + 3531 = 4884
你知道:
palin(4884)=> 0
palin(1353)=> 1因为= palin(4884)+ 1
palin(726)=> 2因为= palin(1353)+ 1
您应该能够自己找到解决方案。
You already have the sum
, you just have to check if sum
is a palindrome. and add 1 to the number of steps.
take advantage of your knowledge:
palin(arg)
87 + 78 = 165; 165 + 561 = 726; 726 + 627 = 1353; 1353 + 3531 = 4884
you know that:
palin(4884) => 0
palin(1353) => 1 because = palin(4884) + 1
palin(726) => 2 because = palin(1353) + 1
You should be able to find a solution by yourself.
这篇关于回文链长算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!