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;
该代码在平台上测试会超时 `#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;
}`