프로그래머스 C# 옷가게 할인 받기(3항 연산자, 파이썬, 자바)

2023. 6. 17. 21:35C# 알고리즘 코딩(프로그래머스)

using System;

public class Solution {
    public int solution(int price) {
        int answer = 0;
        if(price < 300000 && price >= 100000)
        {
            answer = price * 95/100;
        }
        else if(price >= 300000 && price < 500000)
        {
            answer = price * 9/10;
        }
        else if(price >= 500000 && price <= 1000000)
        {
            answer = price * 4/5;
        }
        else
        {
            answer = price;
        }
        return answer;
    }
}

 

 

/////

 

 

using System;

public class Solution {
    public int solution(int price) {
        int answer = 0;
        
        answer = (price < 300000 && price >= 100000) ? price * 95/100 : (price >= 300000 && price < 500000) ? answer = price * 90/100 : price >= 500000 ? answer = price * 4/5 : price;

        return answer;
    }
}

 

 

/////

 

 

using System;

public class Solution {
    public int solution(float price) {
        float  answer = 0;
        answer = price >= 500000 ? price * 0.8f : price >= 300000 ? price * 0.9f : price >= 100000 ? price * 0.95f : price;   
        return (int)answer;
    }
}

 

 

float도 생각해보기는 해야할 듯하다.

 

 

파이썬

 

 

//////

 

 

def solution(price):
    if price>=500000:
        price = price *0.8
    elif price>=300000:
        price = price *0.9
    elif price>=100000:
        price = price * 0.95
    return int(price)

 

 

자바

 

 

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

        if(price>=500000) return (int)(price*0.8);
        if(price>=300000) return (int)(price*0.9);
        if(price>=100000) return (int)(price*0.95);

        return price;
    }
}

 

 

class Solution {
    public int solution(int price) {
        int answer = 0;
        
        if(100000 <= price && price < 300000)
        {
            answer = (int) (price * 0.95);
        }
        else if(300000 <= price && price < 500000)
        {
            answer = (int) (price * 0.9);
        }
        else if(500000 <= price){
            answer = (int) (price * 0.8);
        }
        else
        {
            answer = price;
        }
        
        return answer;
    }
}