본문 바로가기
TIL

12.06 (TIL-코딩문제)

by 오랑이귀엽다 2023. 12. 6.

x만큼 간격이 있는 n개의 숫자

 

나의 풀이

더보기

public class Solution {
    public long[] solution(int x, int n) {
        long[] answer = new long[n];
        for(int i=0; i<n;i++)
        {
            if(i==0)
                answer[i] =x;
            else
                answer[i] =x+answer[i-1];
        }
        return answer;
    }
}

 

다른사람 풀이

더보기
public class Solution {
    public long[] solution(int x, int n) {
        long[] answer = new long[n];
        for (int i = 0; i < n; i++)
        {
            answer[i] = (long)x * (i + 1);
        }
        return answer;
    }
}

 

자연수 뒤집어 배열로 만들기

 

나의 풀이

더보기

public class Solution {
    public int[] solution(long n) {
        string s = n.ToString();
        int[] answer = new int[s.Length];
        
        for(int i = 0; i < s.Length; i++)
        {
            answer[s.Length -i-1] = int.Parse(s[i].ToString());
        }
        return answer;
    }
}

 

다른사람 풀이

더보기
using System.Linq;

public class Solution {
    public int[] solution(long n) {
        string nString = new string(n.ToString().ToCharArray().Reverse().ToArray());
        int[] answer = new int[nString.Length];

        for (int i = 0; i < nString.Length; i++)
        {
            answer[i] = int.Parse(nString[i].ToString());
        }

        return answer;
    }
}

 

문자열을 정수로 바꾸기

 

나의 풀이

더보기

public class Solution {
    public int solution(string s) {
        int answer = int.Parse(s);
        return answer;
    }
}

 

다른사람 풀이

더보기
using System;
public class Solution {
    public int solution(string s) {
        int answer = 0;
        answer = Convert.ToInt32(s);
        return answer;
    }
}

 

 

public class Solution {
    public int solution(string s) {
        int answer = 0;
        int flag =1;
        int index = 0;
        int length = s.Length;
    if(s[0] == '+')
    {
        flag = 1;
        length -= 1;
        index =1;
    }
    else if(s[0] =='-')
    {
        flag = -1;
        length -= 1;
        index =1;
    }
    string x = s.Substring(index,length);
   int.TryParse(x,out answer);        
    return answer * flag;
    }
}

'TIL' 카테고리의 다른 글

12.08 (TIL-코딩문제)  (1) 2023.12.08
12.07 (TIL-Unity)  (0) 2023.12.07
12.05 (TIL-Unity)  (1) 2023.12.05
12.04 (TIL-코딩문제)  (1) 2023.12.04
12.01 (TIL-Unity)  (0) 2023.12.01