https://school.programmers.co.kr/learn/courses/30/lessons/86051
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
function solution(numbers)
{
var sum = 0;
for (i=0; i<10; i++)
{ // 1부터 9까지의 숫자 i가 있다.
if (numbers.includes(i) == false)
{ // i가 numbers에 없는 원소라면
sum +=i;
// sum에 i를 더해준다
}
}
return sum;
}
230916 C++
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int solution(vector<int> numbers) {
int answer = 0;
for (int i = 0; i <= 9; i++) {
bool found = false;
for (int num : numbers) {
if (num == i) {
found = true;
break;
}
}
if (!found) {
answer += i;
}
}
return answer;
}
전체에서 있는 숫자를 빼도 된다고 한다. 이렇게
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> numbers) {
int answer = 45;
for (int i = 0 ; i < numbers.size() ; i++)
answer -= numbers[i];
return answer;
}
'코딩테스트 > LV. 1' 카테고리의 다른 글
LV. 1 :: 가운데 글자 가져오기 (0) | 2023.01.24 |
---|---|
LV. 1 :: 수박수박수박수박수박수 (0) | 2023.01.24 |
LV. 1 :: 약수의 합 (0) | 2023.01.23 |
LV. 1 :: 문자열 내림차순으로 배치하기 (0) | 2023.01.23 |
LV. 1 :: 제일 작은 수 제거하기 (0) | 2023.01.23 |
댓글