Java/์•Œ๊ณ ๋ฆฌ์ฆ˜

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ตฌ๊ฐ„ ํ•ฉ

soowitty 2024. 1. 29. 22:26
๐Ÿ’ก ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์šฉ์„ Do it! ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ with JAVA ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

 

ํ•ฉ ๋ฐฐ์—ด

๊ตฌ๊ฐ„ ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ํ•ฉ ๋ฐฐ์—ด์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

 

๋ฐฐ์—ด A๊ฐ€ ์žˆ์„ ๋•Œ, ๋ฐฐ์—ด S๋ฅผ ํ•ฉ ๋ฐฐ์—ด์ด๋ผ๊ณ  ํ•˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

S[i] = A[0] + A[1] + A[2] ... + A[i-1] + A[i]

 

ํ•ฉ ๋ฐฐ์—ด ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

S[i] = S[i-1] + A[i]

 

๊ตฌ๊ฐ„ ํ•ฉ

๊ตฌ๊ฐ„ ํ•ฉ์€ ํ•ฉ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋” ์ค„์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ํ•œ ๋ชฉ์ ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

๋ฐฐ์—ด์—์„œ ํŠน์ • ๋ฒ”์œ„์˜ ๊ฐ’๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ํ•ฉ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜๋ฉด ๋น ๋ฅด๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

i์—์„œ j๊นŒ์ง€์˜ ๊ตฌ๊ฐ„ ํ•ฉ์„ ๊ตฌํ• ๋•Œ, ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

S[ j ] - S[ i-1 ]