Array_점수 계산
✅ 0이면 0점, 1이면 1점으로 계산하되, 연속된 1이면 두 번째 1은 2점, 3번째 1은 3점 등으로 가산점을 줍니다.
예를 들어, 1 0 1 1 1 0 0 1 1 0 이면
가산점은, 1 0 1 2 3 0 0 1 2 0 이라서 최종 점수는 10점이 됩니다.
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
34
35
36
37
class Main {
public int solution(int n, int[] intArr){
int answer= 0;
int score=1;
for(int i=0; i<n; i++){
if(intArr[i] == 1){
answer +=score;
score++;
}else if(intArr[i] == 0){
score= 1;
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int[] intArr = new int[n];
for(int i=0; i<n; i++){
intArr[i]= sc.nextInt();
}
System.out.println(T.solution(n, intArr));
}
}
//⭐️input:
//10
//⭐️output:
//1 0 1 1 1 0 0 1 1 0
🔵 ThingsILearned
✔️
🟢
🟢
This post is licensed under CC BY 4.0 by the author.