정수가 담긴 리스트 num_list가 주어질 때, 모든 원소들의 곱이 모든 원소들의 합의 제곱보다 작으면 1을 크면 0을 return하도록 solution 함수를 완성해주세요
<생각과정>
num_list의 곱, num_list의 합의 제곱 구해주기 -> 배열 선언하고 할당해주는 방법 찾기
class Solution {
public int solution(int[] num_list) {
int answer = 0;
for(int i=0; i<=num_list.length; i++){
int a *= [i];
int b +=[i];
int c= b*b;
if(a < c){
answer=1;
}else{
answer=0;
}
}
return answer;
}
}

역시나 오류
변수 선언 먼저 해주고, num_list[i] 써주기
class Solution {
public int solution(int[] num_list) {
int answer = 0;
int product =1;
int sum=0;
for(int i=0; i<num_list.length; i++){
product *= num_list[i];
sum += num_list[i];
int c= sum*sum;
if(product < c){
answer=1;
}else{
answer=0;
}
}
return answer;
}
}

product는 곱셈값이기에 0으로 해주면 0나와서 1로 설정해주기~!
++아까 안됐던코드 발전시키기
class Solution {
public int solution(int[] num_list) {
int answer = 0;
int a =1;
int b=0;
for(int i=0; i<num_list.length; i++){
a *= num_list[i];
b += num_list[i];
int c= a*a;
if(a < c){
answer=1;
}else{
answer=0;
}
}
return answer;
}
}

a=830
c=529
a>c인데 왜 a<c 결과인 1이 나왔을까
당연하지... c= a*a라 해놨네...
class Solution {
public int solution(int[] num_list) {
int answer = 0;
int a =1;
int b=0;
for(int i=0; i<num_list.length; i++){
a *= num_list[i];
b += num_list[i];
int c= b*b;
if(a < c){
answer=1;
}else{
answer=0;
}
}
return answer;
}
}

결국 변수의 문제 였다
'코딩테스트 > 프로그래머스_코딩 기초 트레이닝' 카테고리의 다른 글
[프로그래머스] 카운트 업 (1) | 2023.05.30 |
---|---|
[프로그래머스] n의 배수 (0) | 2023.05.30 |
[프로그래머스] 조건에 맞게 수열 변환하기 3 (2) | 2023.05.30 |
[프로그래머스] 첫 번째로 나오는 음수 (2) | 2023.05.29 |
[프로그래머스] 옷가게 할인 받기 (7) | 2023.05.26 |