문제 설명
문제 풀이
- sqrt(수) 함수를 쓰기 위해서는 c에서는 <math.h> c++에서는 <cmath> include가 필요하다.
#include <string>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
int solution(int left, int right) {
int answer = 0;
int cnt = right-left+1;
for(int val=left; val<right+1; val++){
answer += val;
if(int(sqrt(val))*int(sqrt(val))==val){
answer -= 2*val;
}
}
return answer;
}
처음에는 약수의 개수가 홀수일때 빼고 짝수일때 더하는 구조로 하려다가
빼기만 하고싶어서 위와 같이 해봤는데 돼서 신남 룰루
어 리를 만족쓰
'코딩테스트 대비' 카테고리의 다른 글
[프로그래머스 c++] 같은 숫자는 싫어 (1) | 2022.08.22 |
---|---|
[프로그래머스 c++] 구명보트 (0) | 2022.08.22 |
[c++] 소수점 자릿수 출력하기 / [Softeer] 성적평균 문제 풀이 (0) | 2022.06.10 |
평범한 배낭 문제 (0) | 2022.05.12 |
[프로그래머스 Lv.1] 3진법 뒤집기 (0) | 2022.05.06 |