简单算法学习(node.js)
牛客网算法学习,自己的一点想法
勾股数
const readline = require("readline");
function hz(p, q) {
let max = (p > q) ? p : q
for (let i = 2; i <= max; i++) {
if (p % i == 0 && q % i == 0) {
return 0
}
}
return 1
}
function hz2(a, b, c) {
if (hz(a, b) && hz(b, c) && hz(a, c)) {
return 1
} else {
return 0
}
}
function main(m, n) {
let flag = 0
for (let x = m; x <= n; x++) {
for (let y = x + 1; y <= n; y++) {
for (let z = y + 1; z <= n; z++) {
if (hz2(x, y, z) && Math.pow(x, 2) + Math.pow(y, 2) == Math.pow(z, 2)) {
console.log(`${x} ${y} ${z}`)
flag++
}
}
}
}
if (flag == 0) {
console.log('NA')
}
}
function main(m, n) {
let flag = 0
for (let x = m; x <= n; x++) {
for (let y = x + 1; y <= n; y++) {
for (let z = y + 1; z <= n; z++) {
if (hz(x, y, z) && Math.pow(x, 2) + Math.pow(y, 2) == Math.pow(z, 2)) {
console.log(`${x} ${y} ${z}`)
flag++
}
}
}
}
if (flag == 0) {
console.log('NA')
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const lines = []
rl.on('line', function (line) {
//let [m,n] = line.split(' ').map(Number);
lines.push(line)
//console.log(lines)
if(lines.length==2){
let m = lines[0]
let n = lines[1]
main(m,n)
rl.close()
}
});
rl.on('close',function(){
process.exit(0)
})
// let lines = line.split(' ').map(Number);
整数对最小和
const readline = require("readline");
function px(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let a = arr[i]
arr[i] = arr[j]
arr[j] = a
}
}
}
return arr
}
function main(arr1, arr2, num) {
let arr3 = px(arr1)
let arr4 = px(arr2)
let arr5 = []
let n = []
let z =0
for (let a = 0; a < arr1.length; a++) {
for(let b=0;b<arr2.length;b++){
let m =arr3[a]+arr4[b]
n.push(m)
}
}
arr5 = px(n)
for(let l = 0;l<num;l++){
z = z+arr5[l]
}
//console.log(arr5)
console.log(z)
}
// var arr1=[1,1,3,2]
// var arr2 = [1,3,4,1]
// var num = 2
// main(arr1,arr2,num)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const lines = []
rl.on('line', function (line) {
let arr = []
arr = line.split(' ').map(Number)
lines.push(arr)
//console.log(lines[2][0])
if(lines.length==3){
let a = lines[0]
let b =lines[1]
let c = lines[2]
// console.log(a)
// console.log(b)
// console.log(c[0])
main(a,b,c)
rl.close()
}
//main()
})
rl.on('close',function(){
process.exit(0)
})
猴子爬山
const readline = require("readline");
function main(n){
if(n==1){
return 1
}else if(n==2){
return 1
}else if(n==3){
return 2
}else{
return main(n-1)+main(n-3)
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line',function(line){
let num = main(line)
console.log(num)
rl.close()
})
rl.on('close',function(){
process.exit(0)
})
任务调度
const readline = require("readline");
function main(num1,num2,arr){
//console.log(arr)
let time = 0
for(let i = 0;i<num2;i++){
if(arr[i]>num1){
if(i+1>=num2){
arr.push(0)
}
arr[i+1]=arr[i+1]+(arr[i]-num1)
arr[i]=3
}
}
//console.log(arr)
if(arr.length>num2){
if(arr[num2]%num1===0){
time = time + 5 + (arr[num2]/num1)
}else{
time = time + 5 + Math.ceil(arr[num2]/num1)
//console.log(Math.ceil(arr[num2]/num1))
}
}else{
time = time + num2
}
console.log(time)
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const lines=[]
rl.on('line',function(line){
let le = line.split(' ').map(Number)
lines.push(le)
if(lines.length===3){
let arr1 = []
for(let j = 0;j<lines[1][0];j++){
arr1.push(lines[2][j])
}
//console.log(arr1)
let a = lines[0][0];
let b = lines[1][0];
main(a,b,arr1)
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
// var num1 = 4
// var num2 = 5
// var arr = [5,4,1,1,1]
// main(num1,num2,arr)
身高排序
const readline = require("readline");
function main(arr1, arr2) {
let arr3 = []
for (let i = 0; i < arr1[1]; i++) {
let m = Math.abs(arr2[i] - arr1[0])
arr3.push(m)
}
for (let i = 0; i < arr1[1]; i++) {
for (let j = i + 1; j < arr1[1]; j++) {
if (arr3[i] > arr3[j]) {
let a = arr2[i]
arr2[i] = arr2[j]
arr2[j] = a
let b = arr3[i]
arr3[i] = arr3[j]
arr3[j] = b
} else if (arr3[i] = arr3[j]) {
if (arr2[i] > arr2[j]) {
let c = arr2[i]
arr2[i] = arr2[j]
arr2[j] = c
let d = arr3[i]
arr3[i] = arr3[j]
arr3[j] = d
}
}
}
}
return arr2
}
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines = []
rl.on('line',function(line){
let le = line.split(' ').map(Number)
lines.push(le)
//console.log(lines)
if(lines.length===2){
let num = []
for(let i=0;i<lines[0][1];i++){
num.push(lines[1][i])
}
var l=''
var res = main(lines[0],num)
for(let j=0;j<res.length;j++){
l = l+`${res[j]}`+' '
}
console.log(l.trim())
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
// var arr1 = [100, 10]
// var arr2 = [95, 96, 97, 98, 99, 101, 102, 103, 104, 105]
// main(arr1, arr2)
单词反转
const readline = require("readline");
function main(arr1,num1,num2){
//let arr1 = String.split(' ')
let arr2=[]
for(let i=num2;i>=num1;i--){
arr2.push(arr1[i])
}
//console.log(arr2)
var res = arr2.join(' ')
return res.trim()
}
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines=[]
rl.on('line',function(line){
lines.push(line.split(' '))
//console.log(lines)
if(lines.length===3){
console.log(main(lines[0],lines[1][0],lines[2][0]))
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
//main('hello world!',0,3)
双十一
const readline = require("readline");
function main(arr,num){
let sum = []
let res = []
if(arr.length>=3){
if(arr.sort(function(a,b){return a-b})[0]>num){
return -1
}else{
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
for(let k=j+1;k<arr.length;k++){
sum.push(arr[i]+arr[j]+arr[k])
}
}
}
for(let m=0;m<sum.length;sum++){
if(sum[m]<=num){
res.push(sum[m])
}
}
if(res.length===0){
return -1
}else{
res.sort(function(a,b){return b-a})
//console.log(res[0])
return(res[0])
}
}
}else{
return -1
}
//console.log()
}
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines = []
rl.on('line',function(line){
lines.push(line.split(',').map(Number))
if(lines.length===2){
console.log(main(lines[0],lines[1]))
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
//main([23,30,40],26)
找字符
const readline = require("readline");
function main(arr1, arr2) {
var newArr1 = [...new Set(arr1.split(""))]
var newArr2 = [...new Set(arr2.split(""))]
var newArr3 = []
//console.log(newArr1)
for (let i = 0; i < newArr2.length; i++) {
if (newArr1.indexOf(newArr2[i]) >= 0) {
newArr3.push(newArr2[i])
//console.log(newArr3)
}
}
var arr = newArr3.sort(function (a, b) { return a.charCodeAt() - b.charCodeAt() })
//console.log(arr)
return (arr.join(""))
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const lines = []
rl.on('line', function (line) {
lines.push(line)
//console.log(lines)
if (lines.length === 2) {
console.log(main(lines[0], lines[1]))
rl.close()
}
})
rl.on('close', function () {
process.exit(0)
})
// var a = ["bach","bbaaccddfg"]
// main(a[0],a[1])
数据分类
const readline = require("readline");
// var size = 256;
// var sizeBytes = Buffer.alloc(4);
// sizeBytes.writeUInt32LE(size, 0);
// console.log(sizeBytes);
//处理数据,将数组转化为4个字节
function buffZh(num) {
let byte = Buffer.alloc(4)
byte.writeUInt32LE(num, 0)
let sum = byte[0] + byte[1] + byte[2] + byte[3]
return sum
}
function main(arr) {
let arr1 = []
let arr2 = []
let sum = []
for (let i = 2; i < 12; i++) {
arr1.push(buffZh(arr[i]))
}
for (let j = 0; j < 10; j++) {
// if ((arr1[j] % arr[1]) < arr[0]) {
// arr2.push(arr1[j]%arr[1])
// arr2.sort(function (a, b) { return b - a })
// }
arr2.push(arr1[j] % arr[1])
arr2.sort(function (a, b) { return b - a })
}
for (let k = 0; k < arr[0]; k++) {
sum.push(0)
}
for (let m = 0; m < arr2.length; m++) {
for (let n = 0; n < arr[0]; n++) {
if (arr2[m] === n) {
sum[n]++
}
}
}
sum.sort(function(a,b){return b-a})
return sum[0]
}
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
//const lines=[]
rl.on('line',function(line){
console.log(main(line.split(' ').map(Number)))
rl.close()
})
rl.on('close',function(){
process.exit(0)
})
// var a = [3, 4, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265]
// main(a)
最小叶子节点
const readline = require("readline");
function getMinIndex(arr) {
let min = arr[1]
let minIndex = 0
for (let i = 1; i < arr.length; i++) {
if (arr[i] != -1) {
if (min > arr[i]) {
min = arr[i]
minIndex = i
}
}
}
return minIndex
}
function main(arr) {
let arr1 = []
let min = getMinIndex(arr) + 1
arr.unshift('NA')
arr.splice(min + 1)
for (let i = arr.length-1; i > 0;) {
arr1.push(arr[i])
if (i % 2 === 0) {
i = i / 2
} else if (i % 2 === 1) {
i = (i - 1) / 2
}
//console.log(i)
}
// console.log(arr1)
return arr1.reverse()
}
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
//const lines=[]
rl.on('line',function(line){
let result = main(line.split(' ').map(Number))
console.log(result.join(' '))
rl.close()
})
rl.on('close',function(){
process.exit(0)
})
// main([3,5,7,-1,-1,2,4])
//main([5, 9, 8, -1, -1, 7, -1, -1, -1, -1, -1, 6])
// main([5,4,3,2,1])
快速货车
const readline = require("readline");
function main(arr, num) {
//console.log(arr)
arr.sort(function (a, b) { return a - b })
var a = 0
var sum = 0
//console.log(arr)
for (let i = 0; i < arr.length; i++) {
if (a + arr[i] <= num) {
a = a + arr[i]
sum++
}
}
return sum
// console.log(sum)
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const lines = []
rl.on('line', function (line) {
lines.push(line.split(',').map(Number))
//console.log(lines)
if (lines.length === 2) {
let result = main(lines[0], lines[1][0])
console.log(result)
rl.close()
}
})
rl.on('close', function () {
process.exit(0)
})
// main([5,10,2,11],20)
航天器
const readline = require("readline");
function main(arr) {
let area = 0
let arr1 = []
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
area = arr[j] * (j - i)
} else if (arr[i] <= arr[j]) {
area = arr[i] * (j - i)
}
arr1.push(area)
}
}
//console.log(arr1)
arr1.sort(function (a, b) { return b - a })
return arr1[0]
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// const lines = []
rl.on('line', function (line) {
let lines = line.split(',').map(Number)
//console.log(lines)
let result = main(lines)
console.log(result)
rl.close()
})
rl.on('close', function () {
process.exit(0)
})
//main([10,9,8,7,6,5,4,3,2,1])
单词接龙
const readline = require("readline");
function px(num,arr){
arr.splice(num,1)
arr.sort()
arr.sort(function(a,b){return b.length-a.length})
//console.log(arr)
return arr
}
//px(0,['word','dd','da','dc','dword','d'])
function main(num,arr){
let firstWord = arr[num]
let newArr = px(num,arr)
for(let i=0;i<arr.length;i++){
if(newArr[i][0]===firstWord[firstWord.length-1]){
firstWord = firstWord + newArr[i]
newArr[i]=" "
//console.log(newArr)
}
}
return firstWord
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const lines = []
rl.on('line', function (line) {
lines.push(line)
let arr = []
for(let i=0;i<parseInt(lines[1]);i++){
arr.push(lines[i+2])
}
if(lines.length == 2+parseInt(lines[1])){
console.log(main(parseInt(lines[0]),arr))
rl.close()
}
})
rl.on('close', function () {
process.exit(0)
})
// main(1,['word','dd','da','dc','dword','d'])
第K长字串
const readline = require("readline");
function main(num,arr){
//let newArr = [...new Set(arr)]
//arr.split('')
//console.log(arr)
let arr1 = []
let arr2 = []
let arr3 = []
let sum = 0
for(let i = 0;i<arr.length;i++){
if(arr[i] !== arr[i-1]){
arr1.push(i)
}
}
//console.log(arr1)
for(let j =0;j<arr1.length;j++){
let m =arr1[j]
let n = arr1[j+1]
arr2 .push(arr.slice(m,n))
}
for(let k =0;k<arr2.length;k++){
if(arr2[k].length>=num && !arr3.includes(arr2[k][0])){
arr3.push(arr2[k][0])
sum++
}
}
if(sum>0){
return sum
}else{
return -1
}
//console.log(arr3)
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const lines = []
rl.on('line', function (line) {
lines.push(line)
//console.log(lines)
if(lines.length===2){
let result = main(parseInt(lines[1]),lines[0])
console.log(result)
rl.close()
}
})
rl.on('close', function () {
process.exit(0)
})
// var a= main(2,['AABAAA'])
// console.log(a)
喊七
const readline = require("readline");
function lastSeven(arr){
let sum = eval(arr.join('+'))
let sumSeven = 0
for(let i=1;i<=200; i++){
if(i%7===0 || i%10===7){
sumSeven ++
//console.log(sumSeven)
if(sumSeven===sum){
return i
// console.log(i)
// break;
}
}
}
}
function main(arr){
let sum = lastSeven(arr)
//console.log(sum)
let arr1 = []
for(let i=0;i<arr.length;i++){
arr1.push(0)
}
for(let j=1;j<=sum;j++){
if(j%7===0 || j%10===7){
let k = j%arr.length
arr1[k-1]++
}
}
return arr1.join(' ')
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
var lines = []
rl.on('line', function (line) {
lines = line.split(' ').map(Number)
//console.log(lines)
let result = main(lines)
console.log(result)
rl.close()
})
rl.on('close', function () {
process.exit(0)
})
//main([0,0,0,2,1])
// console.log(a)
删除最少字符
const readline = require("readline");
function main(arr){
let arr1 = [...new Set(arr)]
let arr3 = []
let arr4 = []
arr = arr.split('')
//console.log(arr1)
for(let i=0;i<arr1.length;i++){
var arr2 = arr.filter(function(item){
return item==arr1[i]
})
arr3.push(arr2.length)
}
var a = arr3[0]
//var minIndex = 0
for(let j=0;j<arr3.length;j++){
if(a>arr3[j]){
a = arr3[j]
//minIndex = j
}
}
for(let k=0;k<arr3.length;k++){
if(arr3[k]===a){
arr4.push(arr1[k])
}
}
for(let l=0;l<arr4.length;l++){
arr = arr.filter(function(item){
return item !== arr4[l]
})
}
if(arr.length>0){
return arr.join('')
}else{
return 'empty'
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// var lines = []
rl.on('line', function (line) {
let result = main(line)
console.log(result)
rl.close()
})
rl.on('close', function () {
process.exit(0)
})
// main('bbaccdd')
用户调度
const readline = require("readline");
function min(arr){
var arr1 = []
if(arr[2]<=arr[0] && arr[2]<=arr[1]){
//console.log(arr[2])
arr1[0]=2
arr1[1]=arr[2]
}else if( arr[1]<=arr[0] && arr[1]<=arr[2]){
arr1[0]=1
arr1[1]=arr[1]
}else if(arr[0]<=arr[1] && arr[0]<=arr[2]){
arr1[0]=0
arr1[1]=arr[0]
}
return arr1
}
function min2(num,arr){
let arr1 = []
var a = 0
for(let j=0;j<3;j++){
arr1.push(arr[j])
}
arr1.splice(num,1)
//console.log(arr)
if(arr1[0]<=arr1[1]){
a=arr1[0]
}else{
a=arr1[1]
}
//console.log(arr)
for(let i=2;i>=0;i--){
if(a===arr[i]){
return [i,a]
//console.log(i)
}
}
}
function main(num,arr){
var m = min(arr[0])
var sum = m[1]
for(let i=1;i<num;i++){
m = min2(m[0],arr[i])
//console.log(m)
sum = sum+m[1]
}
//console.log(sum)
return sum
}
//main(3,[[1,2,3],[1,2,3],[4,5,6]])
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
var lines = []
rl.on('line', function (line) {
lines.push(line.split(' ').map(Number))
if(lines.length === 1+lines[0][0]){
let arr=[]
for(let i = 1;i<=lines[0][0];i++){
arr.push(lines[i])
}
var result = main(lines[0][0],arr)
console.log(result)
rl.close()
}
})
rl.on('close', function () {
process.exit(0)
})
合并数组
const readline = require("readline");
function px(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i].length < arr[j].length) {
let arr1 = arr[j]
arr[j] = arr[i]
arr[i] = arr1
}
}
}
return arr[0].length
}
function main(num, arr) {
let arrRes = []
let newArr = []
for (let i = 0; i < arr.length; i++) {
newArr.push(arr[i])
}
newArr.push(arr)
let n = px(newArr)
//console.log(arr)
let arr1 = []
for (let k = 0; k < n / num + 1; k++) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].length >= num) {
arr1 = arr[i].splice(0, num)
for (let j = 0; j < num; j++) {
arrRes.push(arr1[j])
}
} else if (0 < arr[i].length < num) {
arr1 = arr[i].splice(0, arr[i].length)
for (let j = 0; j < arr1.length; j++) {
arrRes.push(arr1[j])
}
} else {
continue;
}
//console.log(arrRes)
}
}
return arrRes.join(',')
}
// main(3,[[2,5,6,7,9,5,7],[1,7,4,3,4]])
// //main(4,[[1,2,3,4,5,6],[1,2,3],[1,2,3,4]])
// //main(3,[[1,2,3,4],[1,2]])
// //px([[1, 2, 3], [1, 2], [1, 2, 3, 4]])
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
var lines = []
rl.on('line', function (line) {
lines.push(line.split(',').map(Number))
//console.log(lines.length)
if (lines.length >= 2) {
// console.log(lines.length)
if (lines.length === 2 + lines[1][0]) {
//console.log('aaaaaa')
let arr = []
for (let i = 2; i <= lines[0][0]; i++) {
arr.push(lines[i])
}
var result = main(lines[0][0], arr)
console.log(result)
rl.close()
}
}
})
rl.on('close', function () {
process.exit(0)
})
磁盘容量
const readline = require("readline");
function fg(String) {
var m = String.replace(/[MGT]/g, function (a) {
return a + ' '
})
var n = m.trim().split(' ')
return n
}
function Sum(arr) {
let sum = 0
let num = 0
for (let i = 0; i < arr.length; i++) {
if (arr[i].slice(-1) === "M") {
num = Number(arr[i].slice(0, -1))
} else if (arr[i].slice(-1) === "G") {
num = Number(arr[i].slice(0, -1)) * 1024
} else if (arr[i].slice(-1) === "T") {
num = Number(arr[i].slice(0, -1)) * 1024 * 1024
}
sum = sum + num
}
//console.log(sum)
return sum
}
//fg('3M12G9M')
//Sum(['1G','2M','3M'])
function main(num, arr) {
let sum = []
for (let i = 0; i < num; i++) {
let newArr = fg(arr[i])
sum.push(Sum(newArr))
}
// console.log(sum)
// console.log(arr)
for (let i = 0; i < num; i++) {
for (let j = i + 1; j < num; j++) {
if (sum[i] > sum[j]) {
let b = sum[i]
sum[i] = sum[j]
sum[j] = b
let a = arr[i]
arr[i] = arr[j]
arr[j] = a
}
}
}
//console.log(arr)
return arr
//console.log(sum)
}
//main(5,['1T','20M','3G','10G6T','3M12G9M'])
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
var lines = []
rl.on('line', function (line) {
lines.push(line)
if (lines.length === 1 + Number(lines[0][0])) {
let arr = []
for (let i = 1; i <= Number(lines[0][0]); i++) {
arr.push(lines[i])
}
var result = main(Number(lines[0][0]), arr)
for(let i=0;i<result.length;i++){
console.log(result[i])
}
rl.close()
}
})
rl.on('close', function () {
process.exit(0)
})
运动会
const readline = require("readline");
function main(num,arr1,arr2){
let troops = []
for(let k = 1;k<=num;k++){
troops.push(k)
}
for(let i=0;i<num;i++){
for(let j=i+1;j<num;j++){
if(arr1[i]>arr1[j]){
let a=arr1[i]
arr1[i] = arr1[j]
arr1[j] = a
let b = troops[i]
troops[i] = troops[j]
troops[j] = b
let c = arr2[i]
arr2[i] = arr2[j]
arr2[j] = c
}else if(arr1[i]===arr1[j]){
if(arr2[i]>arr2[j]){
let m = arr2[i]
arr2[i] = arr2[j]
arr2[j] = m
let n = arr1[i]
arr1[i] = arr1[j]
arr1[j] = n
let l = troops[i]
troops[i] = troops[j]
troops[j] = l
}
}
}
}
//console.log(troops)
return troops.join(' ').trim()
}
//main(4,[100,100,120,130],[40,30,60,50])
//main(3,[90,110,90],[45,60,45])
// const rl = readline.createInterface({
// input:process.stdin,
// output:process.stdout
// })
// const lines = []
// rl.on('line',function(line){
// lines.push(line.split(' ').map(Number))
// if(lines.length === 3){
// var result = main(lines[0][0],lines[1],lines[2])
// console.log(result)
// rl.close()
// }
// })
// rl.on('close',function(){
// process.exit(0)
// })
乱序整数序列两数之和绝对值最小
const readline = require("readline");
function main(arr){
let arr1 =[]
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
arr1.push(Math.abs(arr[i]+arr[j]))
}
}
arr1.sort(function(a,b){return a-b})
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
if(Math.abs(arr[i]+arr[j])===arr1[0]){
return [arr[i],arr[j],arr1[0]]
}
}
}
}
// var a= main([-1,-3,7,5,11,15])
// console.log(a)
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
rl.on('line',function(line){
let lines = line.split(' ').map(Number)
//console.log(lines)
var result = main(lines)
console.log(result.join(' ').trim())
rl.close()
})
rl.on('close',function(){
process.exit(0)
})
黑板上色
const readline = require('readline')
function main(num, arr) {
arr.sort(function (a, b) {
return a - b
})
let sum = 0
for (let j = 0; j < 100; j++) {
let arr1 = []
for (let i = 0; i < num; i++) {
if (arr[i] % arr[0] === 0) {
arr1.push(arr[i])
}
}
arr = arr.filter(function (item1) {
return !arr1.some((item2) => item1 === item2)
})
sum++
if(arr.length===0){
break;
}
}
// console.log(sum)
return sum
}
//main(3,[2,4,6])
//main(4, [2, 3, 4, 9])
//main(8,[2,3,4,5,6,7,8,9])
//2,4,6,8
//3,9
//5
//7
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines = []
rl.on('line',function(line){
lines.push(line.split(' ').map(Number))
if(lines.length===2){
var result = main(lines[0][0],lines[1])
console.log(result)
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
最低位排序
const readline = require('readline')
function main(arr){
let newArr =[]
let newArrGw = []
for(let i=0;i<arr.length;i++){
newArr.push(Math.abs(arr[i]))
}
//console.log(newArr)
for(let i=0;i<newArr.length;i++){
//console.log()
if(String(newArr[i]).length-1===0){
newArrGw.push(newArr[i]%10)
}else{
newArrGw.push(newArr[i]%(10**(String(newArr[i]).length-1)))
}
}
//console.log(newArrGw)
//插入排序
for(let i=0;i<arr.length;i++){
let temp =newArrGw[i];
let temp2 = arr[i]
let j = i-1
while(j>=0 && newArrGw[j]>temp){
newArrGw[j+1] = newArrGw[j]
arr[j+1] = arr[j]
j--
}
newArrGw[j+1]=temp
arr[j+1]=temp2
}
//console.log(arr)
return arr
}
//main([1,2,5,-21,22,11,55,-101,42,8,7,32])
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
rl.on('line',function(line){
let lines = line.split(',').map(Number)
var result = main(lines)
console.log(result.join(',').trim())
rl.close()
})
rl.on('close',function(){
process.exit(0)
})
去重求和
const readline = require('readline')
function main(arr,num){
let newArr = [...new Set(arr)]
//console.log(newArr)
newArr.sort(function(a,b){
return a-b
})
//console.log(newArr)
let sumMax=0
let sumMin=0
let arrs=[]
for(let i=0;i<num;i++){
sumMin = sumMin + newArr[i]
arrs.push(newArr[i])
}
newArr.sort(function(a,b){
return b-a
})
for(let i=0;i<num;i++){
sumMax = sumMax + newArr[i]
arrs.push(newArr[i])
}
if([...new Set(arrs)].length<4){
return -1
}else{
return sumMax+sumMin
}
}
// var a= main([3,2,3,4,2])
// console.log(a)
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines =[]
rl.on('line',function(line){
lines.push(line.split(' ').map(Number))
if(lines.length===3){
let arr = []
for(let i = 0;i<lines[0][0];i++){
arr.push(lines[1][i])
}
var result = main(arr,lines[2][0])
console.log(result)
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
英文输入法
const readline = require('readline')
function main(arr,str){
let newArr = arr.split(/[.,' ']/g)
//console.log(newArr)
let len = str.length
//console.log(len)
let arrs = []
for(let i =0;i<newArr.length;i++){
let res = ""
if(newArr[i].length>=len){
for(let j =0;j<len;j++){
res = res+newArr[i][j]
//console.log(res)
}
if(res===str){
arrs.push(newArr[i])
}
}
}
if(arrs.length>0){
let arrss = [...new Set(arrs)]
return arrss.join(' ')
}else{
return str
}
}
//main("The furthest distance in the world,Is not between life and death,But when I stand in front or you,Yet you don't know that I love you.",'f')
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines = []
rl.on('line',function(line){
lines.push(line)
//console.log(lines)
if(lines.length===2){
var result = main(lines[0],lines[1])
console.log(result)
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
矩阵最值
const readline = require('readline')
function px(arr){
//let m = 0
let sum = 0
let newArr = []
for(let i =0;i<arr.length;i++){
sum = parseInt(arr.join(''),2)
newArr.push(sum)
var a= arr.pop() //截取数组最后一位,返回最后一位元素,原数组发生变化
arr.unshift(a) //添加元素到数组最前面,原数组发生变化
}
newArr.sort(function(a,b){
return b-a
})
return newArr[0]
}
function main(num,arr){
let sum = 0
for(let i = 0;i<num;i++){
let res = px(arr[i])
sum = sum + res
}
//console.log(sum)
return sum
}
//px([1,0,0,0,1])
//main(5,[[1,0,0,0,1],[0,0,0,1,1],[0,1,0,1,0],[1,0,0,1,1],[1,0,1,0,1]])
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines = []
rl.on('line',function(line){
lines.push(line.split(',').map(Number))
//console.log(lines)
if(lines.length === lines[0][0]+1){
let arr = []
for(let i =1;i<lines[0][0]+1;i++){
arr.push(lines[i])
}
var result = main(lines[0][0],arr)
console.log(result)
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
整数分解
const readline = require('readline')
function main(num){
let sumArr = []
for(let i = 0;i<=num;i++){
let sum = 0
let arr = []
for(let j =i+1;j<=num;j++){
sum = sum + j
arr.push(j)
if(sum === num){
sumArr.push(arr)
break;
}
}
}
//console.log(sumArr)
let res = []
for(let i = sumArr.length-1;i>=0;i--){
let a = num+'='+sumArr[i].join('+')
//console.log(a)
res.push(a)
}
res.push('Result:'+sumArr.length)
//console.log(res)
return res
}
main(9)
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
rl.on('line',function(line){
var result = main(Number(line))
//console.log(result)
for(let i=0;i<result.length;i++){
console.log(result[i])
}
rl.close()
})
rl.on('close',function(){
process.exit(0)
})
篮球比赛
const readline = require('readline')
function arrSum(arr){
let sum =0
for(let i =0;i<arr.length;i++){
sum = sum+arr[i]
}
return sum
}
function main(arr){
let dif=[]
for(let j=0;j<10000;j++){
let arr1=[]
for(let i=0;i<arr.length;i++){
arr1.push(arr[i])
}
let newArr =[]
for(let i =0;i<5;i++){
let a = arr1.splice(Math.floor(Math.random()*arr1.length),1)
newArr.push(a[0])
// console.log(a)
}
let x = arrSum(arr1)
let y = arrSum(newArr)
//console.log(newArr)
//console.log(x,y)
dif.push(Math.abs(x-y))
}
dif.sort(function(a,b){
return a-b
})
console.log(dif[0])
}
main([10,9,8,7,6,5,4,3,2,1])
成绩排序
const readline = require('readline')
function main(num,arr){
let newArr = []
if(num==0){
// let newArr = []
for(let i=0;i<arr.length;i++){
arr[i][1]=Number(arr[i][1])
}
newArr.push(arr[0])
for(let i=1;i<arr.length;i++ ){
let A = arr[i]
for(let j=newArr.length-1;j>=0;j--){
let B = newArr[j]
if(arr[i][1]<=newArr[j][1]){
newArr.splice(j+1,0,A)
break;
}
if(j===0){
newArr.unshift(A)
}
}
}
}else if(num==1){
// let newArr = []
for(let i=0;i<arr.length;i++){
arr[i][1]=Number(arr[i][1])
}
newArr.push(arr[0])
for(let i=1;i<arr.length;i++ ){
let A = arr[i]
for(let j=newArr.length-1;j>=0;j--){
let B = newArr[j]
if(arr[i][1]>=newArr[j][1]){
newArr.splice(j+1,0,A)
break;
}
if(j===0){
newArr.unshift(A)
}
}
}
}
return newArr
}
//main(0,[['fang',90],['yang',50],['ning',70]])
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
})
const lines = []
rl.on('line',function(line){
lines.push(line.split(' '))
if(lines.length===2+Number(lines[0][0])){
let arr = []
for(let i =0;i<Number(lines[0][0]);i++){
arr.push(lines[i+2])
}
var result = main(lines[1][0],arr)
//console.log(result)
for(let j =0;j<Number(lines[0][0]);j++){
console.log(result[j].join(' ').trim())
}
rl.close()
}
})
rl.on('close',function(){
process.exit(0)
})
扑克牌大小
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
const card = ['3','4','5','6', '7','8','9', '10','J','Q', 'K','A','2', 'joker', 'JOKER']
void async function () {
while(line = await readline()){
let idx = 0
for(let i = 0; i < line.length; i++){
if(line[i] === "-"){
idx = i
}
}
let arr1 = line.slice(0,idx).split(' ')
let arr2 = line.slice(idx+1).split(' ')
// 手牌数量不相同的情况
if(arr1.length !== arr2.length){
if(hasBombORjokers(arr1) === false && hasBombORjokers(arr2) === false){ //arr1和arr2都没有炸弹和王炸
console.log("ERROR")
}else if(hasBombORjokers(arr1) === true && hasBombORjokers(arr2) === true){
let temp = arr1.length < arr2.length ? arr1.join(' ') : arr2.join(' ')
console.log(temp)
}else if(hasBombORjokers(arr1) === true){ // 只有arr1有炸弹或王炸
console.log(arr1.join(' '))
}else if(hasBombORjokers(arr2) === true){ // 只有arr2有炸弹或王炸
console.log(arr2.join(' '))
}
}else{
// 顺子相互比较的情况
if(arr1.length === 5){
compare(arr1[0],arr2[0],arr1,arr2)
}else{ // 个子,对子,三个,炸弹相互比较的情况
let n1 = arr1[0]
let n2 = arr2[0]
compare(n1,n2,arr1,arr2)
}
}
}
}()
// 判断是否含有炸弹或王炸
function hasBombORjokers(arr){
if((arr.indexOf("joker") >= 0 && arr.indexOf("JOKER") >= 0) || ((arr.every(val=>val===arr[0]) && arr.length === 4))){
return true
}
return false
}
// 比较手牌大小
function compare(num1,num2,arr1,arr2){
// 查找大小
let c1 = card.indexOf(num1)
let c2 = card.indexOf(num2)
if(c1 > c2){
console.log(arr1.join(' '))
}else{
console.log(arr2.join(' '))
}
}