Node.jsバックアップアルゴリズム5


1. 10951 A+B - 4

const readline = require('readline');

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

let input = [];

rl.on('line', function (line) {
  input.push(line);
})
  .on('close', function () {
    let i = 0, result = "";
    
    const numbers = input.map(function(line) {
        return line.split(" ");
    });
    
    while( i < numbers.length ) {
        if( i !== 0 ) {
            result += "\n";
        }
        
        const sum = numbers[i].reduce(function(acc, cur) {
            return Number(acc) + Number(cur);
        }, 0);
        
        result += `${sum}`;
        i++;
    }
    
   console.log(result);
    
   process.exit();
});

2. 1110プラスサイクル

const fs = require("fs");
let num = Number(fs.readFileSync('/dev/stdin'));

let count = 0, result = null, newNum = num;

while(true) {
    count++;
    
    // 숫자가 10보다 작을 경우 ex) 8 => 88 = 8 * 11
    if( newNum < 10 ) {
         result = newNum * 11;
    } else {
        const sumOfNum = Math.floor(newNum / 10) + (newNum % 10);
        result = ((newNum % 10) * 10) + (sumOfNum % 10);    
    }

    if( num === result ) break;
    
    newNum = result;
}

console.log(count);