chenxie95 / SJTU_C-_Cource

上交2022小学期 程序设计思想 答疑论坛
1 stars 0 forks source link

小球碰撞问题平台超时 #8

Open SJTUerFan opened 2 years ago

SJTUerFan commented 2 years ago

该代码在平台上测试会超时 `#include

include

using namespace std; int main() { int N; bool firstFlag = 0; double L; double a[10] = {0}; int rightFlag[10] = { 0 }; int i; cin >> N >> L; for (i = 0; i < N; i++) { cin >> a[i]; if (i % 2 == 0) rightFlag[i] = 1; else rightFlag[i] = 0; } int first=0, last = 0; int t=0; double v = 0.5;

while (a[N / 2 - 1] != -1 || a[N / 2] != -1)
{
    for (i = 0; i < N; i++)
    {
        if (a[i] != -1)
        {
            if (rightFlag[i] == 0)
                a[i] = a[i] - v;
            else
                a[i] = a[i] + v;
        }

        if (a[i] < 1e-3 || L - a[i] < 1e-3)
        {
            a[i] = -1;
            if (firstFlag == 0)
            {
                firstFlag = 1;
                first = (t + 1) / 2;
            }
        }
    }

    for (i = 0; i < N - 1; i++)
    {
        if (abs(a[i+1] - a[i ])<1e-3 && a[i] != -1)
        {
            rightFlag[i] = 1 - rightFlag[i];
            rightFlag[i + 1] = 1 - rightFlag[i + 1];
        }
    }
   /* for (i = 0; i < N; i++)
    {
        if (a[i] < 1e-3 || L - a[i] < 1e-3)
        {
            a[i] = -1;
            if (firstFlag == 0)
            {
                firstFlag = 1;
                first = (t+1)/10;
            }
        }
    }*/
    t++;
}
last = t / 2;

cout << first << " " << last << endl;
return 0;

}`

SkyTbac commented 2 years ago

可以参考下#5的回答