튜토리얼 좀 도와주세요ㅠ(CONVERT 문제)

  • algdpdl
    algdpdl

    단위 변환 문제인데, 무엇이 문제인지 잘 모르겠습니다.
    한 가지 신경쓰이는 점은 0을 인풋으로 주었을때 답이 예제에서처럼 0.0000으로 나오지 않고, 그냥 0이라고 나옵니다.

    #include <iostream>
    
    using namespace std;
    
    double kg2lb(double kg){
            return 2.2046*kg;
    }
    double lb2kg(double lb){
            return 0.4536*lb;
    }
    double l2g(double l){
            return 0.2642*l;
    }
    double g2l(double g){
            return 3.7854*g;
    }
    
    int main(int argc, char** argv){
            int cases;
            int index = 1;
            cin >> cases;
            while(cases--){
                    double operand;
                    double result;
                    cin >> operand;
                    string str;
                    cin >> str;
                    string unit;
                    if(str.compare("kg")==0) {
                            result = kg2lb(operand);
                            unit = "lb";
                    }
                    else if(str.compare("lb")==0) {
                            result = lb2kg(operand);
                            unit = "kg";
                    }
                    else if(str.compare("l")==0) {
                            result = l2g(operand);
                            unit = "g";
                    }
                    else {
                            result = g2l(operand);
                            unit = "l";
                    }
    
                    cout << index++ << " " << result << " " << unit << endl;
            }
    }
    

    8년 전
2개의 댓글이 있습니다.
  • jwl1993
    jwl1993

    fix와 precision을 사용하시면 될거같습니다.


    8년 전 link
  • algdpdl
    algdpdl

    감사합니다. 덕분에 풀었네요 ^^


    8년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.