기본틀 :
using System;
public class Solution {
public int solution(int left, int right) {
int answer = 0;
return answer;
}
}
풀이 1) 반복문으로 left ~ rigth 까지 반복시키면서, 그 안에서 Math.Sqrt()만큼 반복문을 동작시키면서 약수인지 여부를 판별한다. 이때, 몫과 나눈 숫자가 같을 경우는 count++를 해주고 아닐 경우 count += 2를 해준다.
using System;
using static System.Math;
public class Solution {
public int solution(int left, int right)
{
int sum = 0;
for(int i = left; i <= right; i++)
{
int count = 0;
for(int j = 1; j <= (int)Math.Sqrt(i); j++)
{
count += i % j == 0 ? i / j == j ? 1 : 2 : 0;
}
sum += (count % 2 == 0) ? i : -1 * i;
}
return sum;
}
}
'문제 풀기 > C#' 카테고리의 다른 글
35. (중요)문자열 다루기 기본(원하는 타입.TryParse(~~~, out)) (0) | 2025.01.21 |
---|---|
34. 문자열 내림차순으로 배치하기(문자열.ToCharArray(), Array.Sort(), Array.Reverse()) (0) | 2025.01.20 |
32. 내적 (0) | 2025.01.20 |
31. 수박(string.Concat() 함수, Enumerable.Repeat 함수) (0) | 2025.01.17 |
30. 가운데 글자 가져오기(Substring(시작 위치, 개수)) (0) | 2025.01.17 |