Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
root-devvoo
Java-Data_Structure
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
Java-Data_Structure
/
Chapter1
/
src
/
Code12_2.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
33 lines (31 loc) · 1.26 KB
main
Breadcrumbs
Java-Data_Structure
/
Chapter1
/
src
/
Code12_2.java
Copy path
Top
File metadata and controls
Code
Blame
33 lines (31 loc) · 1.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import
java
.
util
.
Scanner
;
/* n개의 정수를 입력받아 배열에 저장한다.
* 이들 중에서 0개 이상의 연속된 정수들을 더하여 얻을 수 있는
* 최대값을 구하여 출력하는 프로그램을 작성하라.
*
* (Code12_1 코드보다 간결하고 효율적인 코드)
*/
public
class
Code12_2
{
public
static
void
main
(
String
[]
args
) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
// n이라는 숫자 지정
int
n
=
sc
.
nextInt
();
int
[]
data
=
new
int
[
n
];
// n개의 정수를 입력받아 배열에 저장
for
(
int
i
=
0
;
i
<
n
;
i
++)
data
[
i
] =
sc
.
nextInt
();
sc
.
close
();
// Code12_1 코드보다 간결하고 효율적인 코드
int
max
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++) {
int
sum
=
0
;
// sum의 값은 i가 바뀔 때...즉, 구간의 출발점이 변경될 때 sum은 0으로 리셋하고,
for
(
int
j
=
i
;
j
<
n
;
j
++) {
// j가 바뀌는 동안에는 새로 sum을 구하지 않고,
// 그 이전 루프에서 구해놨던 sum에 현재의 data[j]를 더해주기만 하면, 항상 i에서 j까지의 합이 구해진다.
sum
+=
data
[
j
];
// (그냥 sum에다가 data[j]를 추가해주는 것만으로 i에서 j까지의 합을 구할 수 있다.)
if
(
sum
>
max
)
max
=
sum
;
}
}
System
.
out
.
println
(
max
);
}
}
You can’t perform that action at this time.