728x90
SMALL

using System;

public class Solution {
    public string solution(string s) {
        string answer = "";
            // 입력된 문자열을 공백을 기준으로 잘라서 저장
            string[] temp = s.Split(" "); // {try} {hello} {world}
            // for문을 돌려서 문자열을 하나씩 호출함.
            for (int i = 0; i < temp.Length; i++)
            {
            // 호출된 문자열에서 차례로 문자 하나씩을 꺼냄
                for (int j = 0; j < temp[i].Length; j++)
                {
                    // 짝수는 대문자로, 홀수는 소문자로 바꿈
                    if (j % 2 == 0)
                    {
                        answer += Char.ToUpper(temp[i][j]);
                    }
                    else
                    {
                        answer += Char.ToLower(temp[i][j]);
                    }
                }
                // 띄어쓰기가 전부 생략되어있으므로 문자열이 하나 끝날때마다 띄어쓰기를 넣어줌.
                // 단, 마지막에는 띄어쓰기를 추가하면 안되므로 temp.Lenght-1로 설정해준다.
                if (i != temp.Length-1)
                {
                    answer += " ";
                }
            }
        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

public class Solution {
    public string solution(string phone_number) {
        string answer = "";
        
        for(int i = 0;i<phone_number.Length;i++)
        {
            if(i < phone_number.Length-4) /// 길이-4 전까지는 다 *로 채워넣기
            {
                answer += "*";    
            }
            else
            {
                answer += phone_number[i];
            }
        }
        
        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

public class Solution {
    public string solution(int num) {
        string answer = "Even";
        
        if(num%2 != 0)
        {
            answer = "Odd";
        }

        return answer;
    }
}

 

//음의 정수가 나올 경우를 생각해서 짜야된다.

728x90

설정

트랙백

댓글

728x90
SMALL

using System.Linq;
using System.Collections.Generic;
using System;

public class Solution {
    public long solution(long n) {
        long answer = 0;
        char[] temp = n.ToString().ToCharArray();
        
        // 배열을 오름차순으로 정렬함
        Array.Sort(temp);
        
        // 배열을 뒤집는다 = 내림차순으로 바뀜
        Array.Reverse(temp);
        
        // temp배열을 문자열로 만든뒤에 long형태로 변환한다.
        // long이므로 ToInt32가 아니라 64다. 헷갈린다면 그냥 long.Parse를 쓰자.
        answer = Convert.ToInt64(new string(temp));
        
        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

public class Solution {
    public long solution(long n) {
        long answer = -1;
        
        for(long i = 0;i*i<=n;i++) // 루트 n까지 for문을 돌려서 
        {
            if(n == i*i)
            {
                answer = (i+1)*(i+1);
            }
        }

        return answer;
    }
}

 

///

 

using System;

public class Solution {
    public long solution(long n) {
        long answer = 0;

        var sqrt = Math.Sqrt(n);

        if (sqrt % 1.0 != 0)
            return -1;

        answer = (long)sqrt;

        answer += 1;
        answer *= answer;

        return answer;
    }
}
728x90

설정

트랙백

댓글

728x90
SMALL

using System.Collections.Generic;

public class Solution {
    public int[] solution(long n) {
        string a = n.ToString();
        int[] answer = new int[a.Length];
        
        for(int i=0;i<a.Length;i++)
        {
            answer[i] = (int)(n%10);
            n /= 10;
        }
        return answer;
    }
}

 

/// String으로 만들어놓고 해야되는 줄은 몰랐다.

 

using System;

public class Solution {
    public long[] solution(long n) {
        int size = n.ToString().Length;
        long[] answer = new long[size];

        for(int i = 0; i < size; i++)
        {
            answer[i] = n % 10;
            n /= 10;
        }
        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

using System;

public class Solution {
    public int solution(int n) {
        int answer = 0;
        
        while(n >= 10)
        {
            answer += n%10;
            n /= 10;
        }
        
        answer += n%10;
        return answer;
    }
}

 

파이썬

 

def solution(n):
    return sum(int(i) for i in str(n))

 

 

자바

 

 

class Solution {
    public int solution(int n) {
        int answer = 0;

        while(n>0)
        {
            answer+=n%10;
            n/=10;
        }

        return answer;
    }
}
728x90

설정

트랙백

댓글

728x90
SMALL

public class Solution {
    public int solution(int n) {
        int answer = 0;
        
        for(int i=1;i<=n;i++)
        {
            if(n%i == 0)
            {
                answer += i;
            }
        }
        
        return answer;
    }
}

728x90

설정

트랙백

댓글