Coding 수업정리/CS50

CS50-01-Practice Problems-half

kthdev 2023. 4. 24. 21:19

https://cs50.harvard.edu/x/2023/problems/1/half/

 

Half - CS50x 2023

Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

cs50.harvard.edu

 

제출한 답안

// Calculate your half of a restaurant bill
// Data types, operations, type casting, return value

#include <cs50.h>
#include <stdio.h>

float half(float bill, float tax, int tip);

int main(void)
{
    float bill_amount = get_float("Bill before tax and tip: ");
    float tax_percent = get_float("Sale Tax Percent: ");
    int tip_percent = get_int("Tip percent: ");

    printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
{

    float tax_each;
    tax_each = bill / 100 * tax;
    float tip_each;
    tip_each = (bill + tax_each) / 100 * tip;
    float result = ( bill + tax_each + tip_each ) / 2;
    return result;
}

 

익숙하지 않아서 시간은 걸렸지만, 나누는 사람 숫자도 둘로 정해져있는 예제여서 큰 어려움은 없었다.