#蒟蒻萌新求助

B2065 鸡尾酒疗法

LBlue @ 2023-04-22 16:20:14

只能输入一个怎么办

#include<iostream>
using namespace std;
int main()
{
    int n,Cocktail[3],other1,other2;
    double t1,t2;
    cin >> n;
    cin >> Cocktail[1] >> Cocktail[2];
    t1 = 1.0 * Cocktail[1] / Cocktail[2];
    for(int i = 2;i <= n;i++)
    {
        cin >> other1 >> other2;
        t2 = 1.0 * other1 / other2;
        if(t2 - t1 > 0.05)
            cout << "better" << endl;
        else if(t1 - t2 > 0.05)
            cout << "worse" << endl;
        else
            cout << "same" << endl;
        return 0;
    }
}

请问各位该怎么修改


by ud2_ @ 2023-04-22 16:30:07

输出完一个就执行 return 0 了。


by cflsfzh @ 2023-04-22 16:35:47

参考一下,如下修正————

1.将return 0;放在循环外。 2.改变t1,t2更值是俩除数位置。

#include<iostream>
using namespace std;
int main()
{
    int n,Cocktail[3],other1,other2;
    double t1,t2;
    cin >> n;
    cin >> Cocktail[1] >> Cocktail[2];
    t1 = 1.0 * Cocktail[2] / Cocktail[1];
    for(int i = 2;i <= n;i++)
    {
        cin >> other1 >> other2;
        t2 = 1.0 * other2 / other1;
        if(t2 - t1 > 0.05)
            cout << "better" << endl;
        else if(t1 - t2 > 0.05)
            cout << "worse" << endl;
        else
            cout << "same" << endl;
    }
    return 0;
}

by LBlue @ 2023-04-22 21:46:43

@cflsfzh 谢谢!知道了


by LBlue @ 2023-04-22 21:47:46

@ud2_ 知道了,谢谢!


|