日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

6kyu Persistent Bugger

發(fā)布時間:2025/4/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 6kyu Persistent Bugger 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:

Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

編寫一個函數(shù),持久性,它接受一個正參數(shù)num并返回它的乘法持久性,這就是你必須將數(shù)字中的數(shù)字乘上數(shù)字直到你到達一個數(shù)字的次數(shù)。

For example:

persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4 // and 4 has only one digit persistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126, // 1*2*6 = 12, and finally 1*2 = 2 persistence(4) === 0 // because 4 is already a one-digit number

?Sample Tests:

describe('Initial Tests', function () {Test.assertEquals(persistence(39),3);Test.assertEquals(persistence(4),0);Test.assertEquals(persistence(25),2);Test.assertEquals(persistence(999),4); });

?

答案: 

    // 1

????????????? function persistence(num) {

???????????????????? var times = 0;

???????????????????? num = num.toString();

???????????????????? while (num.length > 1) {

??????????????????????????? times++;

??????????????????????????? num = num.split('').map(Number).reduce((a, b) => a * b).toString();

???????????????????? }

???????????????????? return times;

????????????? }

?

????????????? // 2

????????????? const persistence = num => {

???????????????????? return `${num}`.length > 1 ? 1 + persistence(`${num}`.split('').reduce((a, b) => a * +b)) : 0;

???????????????????? // +b 隱式類型轉換 字符串轉數(shù)字 +前為空自動默認為求和運算而不是字符串拼接

???????????????????? // * - / % 都可以隱式類型轉換 字符串轉數(shù)字

???????????????????? // 此處并不需要+? a和b都是字符串 a*b 隱式轉換為數(shù)字

????????????? }

?

????????????? // 3

????????????? function persistence(num) {

???????????????????? var i = 0;

???????????????????? for (i; num.toString().length > 1; i++) {

??????????????????????????? num = num.toString().split('').reduce()(function(x,y){return x * y});

???????????????????? }

???????????????????? return i;

????????????? }

?

????????????? // 4

????????????? const prod = (n) => (n + '').split('').reduce((p,v) => p * v, 1)

????????????? function persistence(num) {

???????????????????? let p = 0;

???????????????????? while (num > 9) {

??????????????????????????? num = prod (num);

??????????????????????????? p++;

???????????????????? }

???????????????????? return p;

????????????? }

轉載于:https://www.cnblogs.com/tong24/p/7306247.html

總結

以上是生活随笔為你收集整理的6kyu Persistent Bugger的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。