来自URI Online Judge的这个C ++代码有什么问题?

时间:2015-07-10 16:30:43

标签: c++

你可以帮我找一下这个C ++代码中的错误来解决URI Online Judge中的一个问题......这就是问题所在: https://www.urionlinejudge.com.br/judge/en/problems/view/1038 这是代码:

#include <iostream>
#include <cstdio>

using namespace std;

int Code,Qau;
double F;

int main () {
    cin >> Code >> Qau;
    if (Code==1) {
        F=4.00*Qau;
        cout <<"Total: R$ ";
        printf ("%.2lf\n",F);
    } else if (Code==2) {
        F=4.50*Qau;
        cout <<"Total: R$ ";
        printf ("%.2lf\n",F);
    } else if (Code==3) {
        F=5.00*Qau;
        cout <<"Total: R$ ";
        printf ("%.2lf\n",F);
    } else if (Code==4) {
        F=2.00*Qau;
        cout <<"Total: R$ ";
        printf ("%.2lf\n",F);
    } else if (Code==5) {
        F=1.50*Qau;
        cout <<"Total: R$ ";
        ("%.2lf",F); 
        cout <<endl;
    } 
return 0;
}

2 个答案:

答案 0 :(得分:0)

在第五个块中你忘了使用printf,只有表达式的大括号,但没有命令本身。

答案 1 :(得分:0)

您指定的问题可以通过此代码轻松解决

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    float val[] = { 4, 4.5, 5, 2, 1.5 };
    int X, Y;
    cin >> X >> Y;
    cout << "Total: R$ " << setprecision(2) << fixed << val[X - 1] * Y << endl;
    return 0;
}

或简化您的代码

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int Code, Qau;
    double F;
    cin >> Code >> Qau;
    if (Code == 1) F = 4.00;
    else if (Code == 2) F = 4.50;
    else if (Code == 3) F = 5.00;
    else if (Code == 4) F = 2.00;
    else if (Code == 5) F = 1.50;
    printf("Total: R$ %.2lf\n", F * Qau);
    return 0;
}