본문 바로가기
개발공간

C# Subarray Division [HackerRank]

by -0o0- 2021. 9. 9.

C# Subarray Division [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 birthday(List<int> s, int d, int m)
    {
        //더한 수 와 d 가 맞을 때의 카운트 변수
        int count = 0;
        
        for(int i=0; i<s.Count; i++)
        {
            int tmp = 0;
            //i 번째부터 i+m번째 까지 반복
            for(int j=i; j<i+m; j++)
            {
                if(j==s.Count) break;
                tmp += s[j];
            }
            //더한 tmp 가 d와 같다면 카운트 더하기
            if(d==tmp) count++;
        }
        return count;
    }

}

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

        int n = Convert.ToInt32(Console.ReadLine().Trim());

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

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

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

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

        int result = Result.birthday(s, d, m);

        textWriter.WriteLine(result);

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

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

C# Migratory Birds [HackerRank]  (0) 2021.09.09
C# Divisible Sum Pairs [HackerRank]  (0) 2021.09.09
C# Breaking the Records [HackerRank]  (0) 2021.09.09
C# Number Line Jumps [HackerRank]  (0) 2021.09.09
C# Apple and Orange [HackerRank]  (0) 2021.09.09

댓글