Skip to content

Commit 58e424c

Browse files
committed
add: ๋ฐฑ์ค€/a^b
1 parent bf4ed56 commit 58e424c

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

โ€Žboj/10827(BigInt).jsโ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// a^b
2+
// https://www.acmicpc.net/problem/10827
3+
4+
const fs = require('fs');
5+
const stdin = (process.platform === 'linux'
6+
? fs.readFileSync('/dev/stdin').toString().trim()
7+
: `0.1 1`
8+
).split('\n');
9+
10+
const input = (() => {
11+
let line = 0;
12+
return () => stdin[line++];
13+
})();
14+
15+
const [a, b] = input()
16+
.split(' ')
17+
.map((each) => Number(each));
18+
19+
const aArr = a.toString().split('');
20+
const indexOfDot = aArr.indexOf('.');
21+
22+
const dotPosition = (
23+
BigInt(10 ** aArr.slice(indexOfDot + 1, aArr.length).length) ** BigInt(b)
24+
).toString();
25+
26+
const converToInt =
27+
aArr.slice(0, indexOfDot).join('') +
28+
aArr.slice(indexOfDot + 1, aArr.length).join('');
29+
30+
const calc = (BigInt(converToInt) ** BigInt(b)).toString();
31+
32+
const index = calc.length - dotPosition.length + 1;
33+
34+
if (index >= 0) {
35+
return console.log(
36+
calc.slice(0, index) + '.' + calc.slice(index, calc.length)
37+
);
38+
}
39+
40+
return console.log('0.' + '0'.repeat(-index) + calc);
41+
42+
/*
43+
44+
## How
45+
์กฐํ•ฉ์— BigInt๋ฅผ ์จ์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ ‘ํ•˜๊ณ  BigInt ๊ด€๋ จ ๋ฌธ์ œ๋ฅผ ํ•œ ๋ฒˆ ๋” ํ’€์–ด๋ณด๊ณ  ์‹ถ์–ด์„œ ์„ ์ •ํ–ˆ๋‹ค.
46+
47+
์†Œ์ˆ˜์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ ๊ทธ๋Œ€๋กœ ์—ฐ์„  ํ—ค ๋ฒ„๋ฆฌ๋ฉด ์˜ค์ฐจ๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ `.`์˜ ์œ„์น˜๋ฅผ ๊ธฐ์–ตํ•˜๊ณ . ์„ ์—†์• ๋ฒ„๋ฆฐ ๋’ค ์ •์ˆ˜ํ™”ํ•œ ์ˆ˜๋ฅผ BigInt๋กœ ์—ฐ์‚ฐํ•ด์„œ. ์ด ์žˆ์–ด์•ผ ํ•  ์œ„์น˜์— ์ถ”๊ฐ€ํ•˜๋„๋ก ํ•˜๋ฉด ๋œ๋‹ค.
48+
49+
`.`์˜ ์œ„์น˜๋Š” ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋จผ์ € "10 ^ (`.` ์ดํ›„์˜ ์ˆซ์ž ๊ฐœ์ˆ˜)"์— "b ๋งŒํผ ๊ฑฐ๋“ญ์ œ๊ณฑํ•œ ์ˆ˜์˜ ๊ธธ์ด"๋ฅผ ๊ตฌํ•œ๋‹ค. ์ „์ฒด ์—ฐ์‚ฐ ๊ฒฐ๊ณผ ๊ธธ์ด์—์„œ ํ•ด๋‹น ๊ธธ์ด๋ฅผ ๋บ€ ๋’ค + 1 ํ•˜๋ฉด ์—ฐ์‚ฐ ํ›„ `.`์ด ์žˆ์–ด์•ผ ํ•  ์œ„์น˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
50+
51+
์ด๋ ‡๊ฒŒ ์ ์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•˜๋ฉด ์Œ์ˆ˜์ธ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธฐ๋Š”๋ฐ "0. + (0 * ๊ธธ์ด) + ๊ณ„์‚ฐํ•œ ์ˆ˜" ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.
52+
53+
## Retrospective
54+
55+
*/

0 commit comments

Comments
ย (0)