본문 바로가기
개발공간

C# Divisible Sum Pairs [HackerRank]

by -0o0- 2021. 9. 9.

C# Divisible Sum Pairs [HackerRank]

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{
    public static int divisibleSumPairs(int n, int k, List<int> ar)
    {
        //카운트 변수
        int count = 0;
        
        //이중 반복으로 전체적으로 더하기
        for(int i=0; i<ar.Count-1; i++)
        {
            for(int j=i+1; j<ar.Count; j++)
            {
                //한 쌍의 더한 수 / k 의 나머지가 0이면 카운트 더하기
                if((ar[i] + ar[j])%k==0) count++;
            }
        }
        return count;
    }

}

class Solution
{
    public static void Main(string[] args)
    {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');

        int n = Convert.ToInt32(firstMultipleInput[0]);

        int k = Convert.ToInt32(firstMultipleInput[1]);

        List<int> ar = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arTemp => Convert.ToInt32(arTemp)).ToList();

        int result = Result.divisibleSumPairs(n, k, ar);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}

'개발공간' 카테고리의 다른 글

C# Bill Division [HackerRank]  (0) 2021.09.10
C# Migratory Birds [HackerRank]  (0) 2021.09.09
C# Subarray Division [HackerRank]  (0) 2021.09.09
C# Breaking the Records [HackerRank]  (0) 2021.09.09
C# Number Line Jumps [HackerRank]  (0) 2021.09.09

댓글