xccjk / x-blog

学习笔记
18 stars 2 forks source link

PAT (Basic Level) Practice - JavaScript #75

Closed xccjk closed 1 year ago

xccjk commented 2 years ago

1019 数字黑洞

注意点:

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    fn(String(line));
})

function fn(line) {
    var num = new Array(4 - line.length).fill('0').join('') + line;
    var min = num.split('').sort().join('');
    var max = num.split('').sort().reverse().join('');
    if (max === min) {
        console.log(`${max} - ${min} = 0000`);
    } else {
        var diff = +max - +min;
        var diffStr = String(diff).length === 4 ? diff : 
            new Array(4 - String(diff).length).fill('0').join('') + String(diff)
        console.log(`${max} - ${min} = ${diffStr}`)
        if (diff !== 6174) {
            fn(String(diff))
        }
    }
}
xccjk commented 2 years ago

1004 成绩排名

注意点:

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    fn(String(line));
})

var data = [];
var num = 0;
var count = 0;

function fn(line) {
    if (line.indexOf(' ') !== -1) {
        const [a, b, c] = line.split(' ');
        data.push({ 'label': `${a} ${b}`, value: c });
        count++
        if (count === num) {
            var sortData = data.sort((a, b) => { return b.value - a.value });
            sortData.map((item, index) => {
                if (index === 0) {
                    console.log(item.label)
                }
                if (index === sortData.length - 1) {
                    console.log(item.label)
                }
            })
        };
    } else {
        num = +line
    }
}
xccjk commented 2 years ago

1006 换个格式输出整数

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    fn(String(line));
})

function fn(num) {
    var arr = num.split('');
    var len = arr.length;
    let str = ''
    arr.forEach((m, n) => {
        if (len - n === 3) {
            str += new Array(+m).fill('B').join('')
        } else if (len - n === 2) {
            str += new Array(+m).fill('S').join('')
        } else {
            str += new Array(+m).fill().map((_, index) => String(index + 1)).join('')
        }
    })
    console.log(str)
}
xccjk commented 2 years ago

1009 说反话

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    const res = fn(line);
    console.log(res)
})

function fn(str) {
    return str.split(' ').reverse().join(' ')
}
xccjk commented 2 years ago

1011 A+B 和 C

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    if (line.length > 2) {
        const res = fn(line);
        console.log(res)
    }
})

var num = 0
function fn(str) {
    if (str.indexOf(' ') !== -1) {
        num++
        const [a, b, c] = str.split(' ');
        if (Number(a) + Number(b) > Number(c)) {
            return `Case #${num}: true`
        } else {
            return `Case #${num}: false`
        }
    }
}
xccjk commented 2 years ago

1012 数字分类

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    const res = fn(line);
    console.log(res)
})

function fn(str) {
    const array = str.split(' ')
    var arr = array.slice(1).map(l => +l);
    var arr1 = arr.filter(l => l%2 === 0 && l%5 === 0);
    var arr2 = arr.filter(l => l%5 === 1);
    var arr3 = arr.filter(l => l%5 === 2);
    var arr4 = arr.filter(l => l%5 === 3);
    var arr5 = arr.filter(l => l%5 === 4);
    var a1 = arr1.length ? arr1.reduce((a, b) => a +=b) : 'N';
    var a2 = arr2.length ? 0 : 'N';
    arr2.forEach((a, b) => {
        if (!(b%2)) {
            a2 = a2 + a
        } else {
            a2 = a2 - a
        }
    })
    var a3 = arr3.length || 'N';
    var a4 = arr4.length ? (arr4.reduce((a, b) => a +=b)/arr4.length).toFixed(1) : 'N';
    var a5 = arr5.sort(function(a, b){return b - a})[0] || 'N';
    return `${a1} ${a2} ${a3} ${a4} ${a5}`
}
xccjk commented 2 years ago

1016 部分A+B

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    fn(line);
})

function fn(line) {
    var arr = line.split(' ');
    var num = 0;
    arr.forEach((val, index) => {
        if (index%2 === 0) {
            m = val.split('').filter(l => l === arr[index + 1]).join('')
            num += Number(m)
        }
    })
    console.log(num)
}
xccjk commented 2 years ago

1001 害死人不偿命的(3n+1)猜想

var readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    var n = parseInt(line)
    var ret = fn(n)
    console.log(ret)
})

var num = 0
function fn(n) {
    if (n === 1) {
        return num
    } else {
        var m = n%2 ? 3 * n + 1 : n;
        num++;
        return fn(m/2);
    }
}
xccjk commented 2 years ago

1002 写出这个数

注意点:

var readline = require('readline')

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    fn(String(line))
})

var enum_list = ["ling","yi","er","san","si","wu","liu","qi","ba","jiu"]

function fn(line) {
    var num = line.split('').map(l => +l).reduce((a, b) => a+=b);
    var str = String(num).split('').map(val => {
        return enum_list[val]
    }).join(' ');
    console.log(str);
}
xccjk commented 2 years ago

1023 组个最小数

注意点:

var readline = require('readline')

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    fn(line);
})

var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

function fn(line) {
    var arr0 = [];
    var arr1 = [];
    line.split(' ').map((l, i) => {
        if (+l !== 0) {
            if (arr[i] === 0) {
                arr0 = new Array(+l).fill(arr[i])
            } else {
                arr1 = arr1.concat(new Array(+l).fill(arr[i]))
            }
        }
    });
    var str = arr1.map((l, i) => {
        if (arr1.length > 1) {
            if (i === 1) {
                return `${arr0.join('')}${l}`
            }
            return l;
        } else {
            return `${l}${arr0.join('')}`
        }
    }).join('');
    console.log(str);
}
xccjk commented 2 years ago

1026 程序运行时间

注意点:

var readline = require('readline')

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})

rl.on('line', function(line) {
    fn(line);
})

function complement(str) {
   if (String(str).length !== 2) {
       return `0${str}`
   }
   return str;
}

function fn(line) {
    var [a, b] = line.split(' ');
    var t = (+b - +a) / 100;
    var time = Math.floor(t/3600);
    var minute = Math.floor((t - time * 3600) / 60);
    var second = Math.round((t - time * 3600 - minute * 60))
    console.log(`${complement(time)}:${complement(minute)}:${complement(second)}`)
}