알고리즘

C# 알고리즘 /부족한 금액 계산하기 / 문자열 다루기(기본) / 행렬의 덧셈

게발인개발자 2023. 12. 4. 22:47

부족한 금액 계산하기

using System;

class Solution
{
    public long solution(int price, int money, int count)
    {
        long sum = 0;
for (int i = 1; i <= count; i++)
{
    sum += price * i;
}

if( money >= sum)
{
    return 0;
}
else
{
    return sum - money;
    }
    }
}

 

문자열 다루기

public class Solution {
    public bool solution(string s) {
     bool answer = false;

 if( s.Length == 4 || s.Length == 6 ) 
 {
     answer = int.TryParse(s, out int num);
 }
 return answer;
    }
}

 

 

행렬의 덧셈

public class Solution {
    public int[,] solution(int[,] arr1, int[,] arr2) {
       int[,] answer = new int[arr1.GetLength(0),arr1.GetLength(1)];

for( int i =0; i < arr1.GetLength(0); i++ )
{
    for( int j = 0; j < arr1.GetLength(1); j++)
    {
        answer[i,j] = arr1[i, j] + arr2[i,j];
    }
}
return answer;
    }
}