正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
数据范围:
let str = readline(); let arr = str.split(' '); let a = arr[0], b = arr[1] function gys(num1,num2){ if((num1-num2)<0){ [num1,num2]=[num2,num1] } while(num2!=0){ var temp2=num1%num2; num1=num2; num2=temp2; } return num1;//此时返回的就是最大公约数 } function scm(a,b) { return a*b/gys(a,b) } console.log(scm(a,b))
var fn = function(a, b){ let max = Math.max(a,b); let min = Math.min(a,b); if(a == 1 || b == 1){ return max; } let find = 0 ,result; for(let i = 1; i <= max; i++){ let n1 = max * i; for(let j = 1; j <= max; j++){ let n2 = min * j; if(n1 === n2){ result = n1; find = 1; break; } } if(find) break; } return result; }
// 先求出最大公约数,然后再求最小公倍数 while(line=readline()){ var arr = line.split(" "); var numA = parseInt(arr[0]); var numB = parseInt(arr[1]); console.log(numA * numB / getGcd(numA, numB)); } function getGcd(m, n) { let max = Math.max(m, n); let min = Math.min(m, n); if(max % min === 0) { return min; } else { return getGcd(max % min, min); } }
// 题目里面a和b是用\n分割的,测试用例中使用'空格'分隔的,我佛了。 const lines = []; while(line = readline()) { lines.push(line); } const gcd = (a, b)=> a%b?gcd(b, a%b):b; const lcm = (a, b) => a*b/gcd(a, b); lines .map(str=>str.split(' ')) .map(strArr=>strArr.map(strn => parseInt(strn, 10))) .map(arr=>lcm(arr[0], arr[1])) .forEach(print)
varreadline=require("readline");varr1=readline.createInterface({input:process.stdin,output:process.stdout});r1.on("line",function(line){line=line.trim();vararr=line.split(' ');varresult=maxNum(arr[0],arr[1]);arr=[];console.log(result);})functionmaxNum(num1,num2){vartemp=Math.max(num1,num2);vartmp=Math.min(num1,num2);vari;while(temp%tmp!=0){i=temp%tmp;temp=tmp;tmp=i;}returnnum1*num2/tmp;-----------------------------------------------------------------------------var line=readline(); var result=[]; line=line.trim(); var arr=line.split(' '); var result=maxNum(arr[0],arr[1]); console.log(result); function maxNum(num1,num2){ var temp=Math.max(num1,num2); var tmp=Math.min(num1,num2); var i; while(temp%tmp!=0){ i=temp%tmp; temp=tmp; tmp=i; } return num1*num2/tmp;}
while(line = readline()){ var arr = line.split(' ').map(function(item){ return parseInt(item); }); var a = parseInt(arr[0]), b = parseInt(arr[1]); var num = a*b; while(a%b){ var c = a%b; a = b; b = c; } console.log(num/b); }
var readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.on('line', function(line){ var tokens = line.split(' '); //求a和b的最大公约数 function gcd(a,b){ if(b === 0){ return a; } return gcd(b,a%b); } //求a和b的最小公倍数,其中需要a和b的最大公约数 function lcm(a,b,gcd){ return a*b/gcd; } var a = parseInt(tokens[0]); var b = parseInt(tokens[1]) var gcd = gcd(a,b); var lcm = lcm(a,b,gcd); console.log(lcm); });
var readline=require("readline"); var r1=readline.createInterface({ input:process.stdin, output:process.stdout }); r1.on("line",function(line){ line=line.trim(); var arr=line.split(' '); var result=maxNum(arr[0],arr[1]); arr=[]; console.log(result); }) function maxNum(num1,num2){ var temp=Math.max(num1,num2); var tmp=Math.min(num1,num2); var i; while(temp%tmp!=0){ i=temp%tmp; temp=tmp; tmp=i; } return num1*num2/tmp; }思路应该都没有什么问题,给出的输入方式和实际输入不一致,给出的是多行输入,实际是单行输入,中间以空格隔开,哎,坑爹的题目。。。