Project Ruler 算法练习之除数问题
2014-06-04 22:20
239 查看
问题描述:
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that49/98 = 4/8,
which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
思路:
1.找到1位数的所有分数
2.找到两位数的分数,把分子分母有重叠的部分去掉,如果相等,存入数组
3.对比两个数组的结果,找到相同的
javascript实现:
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that49/98 = 4/8,
which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
思路:
1.找到1位数的所有分数
2.找到两位数的分数,把分子分母有重叠的部分去掉,如果相等,存入数组
3.对比两个数组的结果,找到相同的
javascript实现:
(function(){ var dig = function (m,n){ var arr = new Array(); for(j=m+1;j<n;j++){ for(var i = m;i < j ;i++){ arr.push({i:i,j:j}); } } return arr; } var filter = function(arr){ var ret =new Array(); for(var i = 0; i < arr.length ; i++){ var strN = arr[i].i.toString(); var strM = arr[i].j.toString(); if(strN.indexOf(strM[0]) != -1&&strM[0]!='0'){ if(strN[0] == strM[0] && parseInt(strN)/parseInt(strM) == parseInt(strN[1]) / parseInt(strM[1])){ret.push({i:strN[1],j:strM[1],ori:strN+"/"+strM});} else if(strN[1] == strM[0] && parseInt(strN)/parseInt(strM) == parseInt(strN[0]) / parseInt(strM[1])){ret.push({i:strN[0],j:strM[1],ori:strN+"/"+strM});} } else if(strN.indexOf(strM[1]) != -1&&strM[1]!='0'){ if(strN[0] == strM[1]&& parseInt(strN)/parseInt(strM) == parseInt(strN[1]) / parseInt(strM[0])){ret.push({i:strN[1],j:strM[0],ori:strN+"/"+strM});} else if (strN[0] == strM[1] && parseInt(strN)/parseInt(strM) == parseInt(strN[1]) / parseInt(strM[0])){ret.push({i:strN[1],j:strM[0],ori:strN+"/"+strM});} } } return ret; } var ret1 = dig(1,10); var ret2= filter(dig(10,100)); console.log(ret2); for(var j = 0 ;j < ret1.length ;j++){ for(var i = 0;i < ret2.length; i++){ //console.log(ret2[i].i + "/" + ret2[i].j + " = " + ret1[j].i + "/" + ret1[j].j); if(parseInt(ret2[i].i) / parseInt(ret2[i].j) == parseInt(ret1[j].i) / parseInt(ret1[j].j)){ console.log(ret2[i].ori + " = " + ret1[j].i + "/" + ret1[j].j); } } } })();
相关文章推荐
- Project Ruler 算法练习之 10 进制 转 2进制 以及数字对称
- ProjectRuler 算法练习之 位数组成字符串同样的整数
- ProjectRuler 算法练习之 位数组成字符串相同的整数
- Project Ruler 算法练习之 Truncate Prime
- 算法基础——1.6练习(数字数码管、隐藏密码问题)
- [算法导论]练习16.1-4 活动教室分配(区间着色问题)
- 算法练习-NOJ-1045-六数码问题
- 算法练习-NOJ-1006-堡垒问题
- 蛙蛙推荐:算法练习:最大间隙问题
- 剑指offer-算法题练习:part9 变态跳台阶-斐波那契数列问题
- 算法练习-NOJ-1008-素数环问题
- 算法练习-NOJ-1007-8皇后问题
- 剑指offer-算法题练习:part7 斐波那契数列-斐波那契数列问题
- 每周算法练习——最近对问题
- 每周算法练习——用动态规划求解最短路径问题
- 算法练习-NOJ-1009-迷宫问题
- 算法基础——2.7练习(通信编码、格式检查问题)
- 贪心算法练习:乘船问题
- 每周算法练习——大数的乘法问题
- 练习程序:算法系列14:狼、羊、菜和农夫过河问题