简单算法合集
//时间戳转日期
var timeToDate = function(time) {
var date = new Date(time);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes()
if (m == 0) {m = '00'}
s = date.getSeconds();
return Y+M+D+h+m
}
//日期转时间戳
var dateToTime = function(date) {
return (new Date(date)).getTime();
}
//提取时间戳的小时或者分
var dateType = function(time, type) {
var date = new Date(time);
if (type == 'h') {
return date.getHours()
} else if ('m') {
var m = date.getMinutes()
if (m == 0) {m = '00'}
return m
}
}
var allot = function(startDate, endDate, gap, num) {
var timeArr = []
var start = dateToTime(startDate)
var end = dateToTime(endDate)
var h = 0, m = 0
for (var i = start; i < end; i += (gap * 3600000) ) {
h = dateType(i, 'h')
m = dateType(i, 'm')
if ( (h >= 6 && h <= 19) || (h == 20 && m == '00') ) {
//console.log('时间' + h + ':' + m)
timeArr.push([timeToDate(i), endDate])
} else {
continue
}
}
var arrLen = timeArr.length
var count = parseInt(num / arrLen)
var rem = num % arrLen
// console.log(arrLen)
// return false
if (count == 0) {
if (rem > 0) {timeArr = timeArr.slice(0, num)}
return timeArr
} else if (count > 0) {
var timeArr2 = timeArr
for (var i = 1; i < count; i++) {
for (var j = 1; j <= timeArr2.length; j++) {
timeArr[length + ] = timeArr2(j)
}
}
if (rem > 0) {
timeArr.push(timeArr.slice(0, rem))
}
}
return timeArr
}
var timeArr = allot('2021-5-31 04:00', '2021-6-2 6:00', 0.5, 100)
console.log(timeArr)
var tree = function (money, type, param, update) { var tmpArr = [], result = [], arr = [], start = 0, two = 0 for(var i in type){ start = two = 0 if (tmpArr[type[i]]) { start = tmpArr[type[i]] * param[i] } else { start = money[type[i]] * param[i] tmpArr[type[i]] = money[type[i]] - start } two = start if (update[i] != 0) { start += update[i] tmpArr[type[i]] -= update[i] } result[i] = {0: two, 1: start} } return result } console.log(tree({1: 100, 22: 80, 33: 60}, [1, 22, 1, 33, 22, 33], [0.2, 0.3, 0.2, 0.3, 0.2, 0.3], [-2, 0, 3, 0, 5, 0]))
var tree = function (money, param, update) { var arrFirst = [], arrTwo = [] var start = 0 for (var i = 0; i < money.length ; i++) { start = money[i] arrTwo = [] for (var l = 0; l < param[i].length ; l++) { for (var j = 0; j < update.length ; j++) { if (update[j][0] == i && update[j][1] == l) { arrTwo[l] = update[j][2] start -= update[j][2] } } if (!arrTwo[l]) { arrTwo[l] = start * param[i][l] start -= start * param[i][l] } } arrFirst[i] = arrTwo } return arrFirst; } console.log(tree([100, 100, 100], [[0.2, 0.3], [0.2, 0.3], [0.2, 0.3, 0.1]], [[0, 0, 5], [0, 1, 7], [1, 2, 6]]))