문제 풀기/C#
33. 약수의 개수와 덧셈
kagan-draca
2025. 1. 20. 13:54
기본틀 :
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;
}
}