AcWing 1. 腾讯课堂的物理实验
原题链接
简单
作者:
WA-TLE
,
2022-04-26 19:47:09
,
所有人可见
,
阅读 189
这个题让我怀疑我是个智障
首先一读题,这应该是 if —— else 吧,然后就在那分类讨论
这是我讨论的代码,算是接近成功吧,计蒜客 20 个样例,过了 19 个
今上午 计组课上看了看题解。。。。。。。
两个小球相撞后,速度大小不变,方向相反,相当于,两个小球交换一下。。。
仅此而已
#include <iostream>
#include <cmath>
using namespace std;
int l,t,k,s1,s2;
bool g1,g2;
int main()
{
cin>>l>>t>>k;
int a = t/l;
int b = t%l;
if(a)
{
if(a%2)
{
s1 = l-b;
g1 = true;
}
else
{
s1 = b;
g1 = false;
}
}
else
{
g1 = false;
s1 = b;
}
for(int i=1;i<=k-t;i++)
{
bool g = true;
// cout<<"!!!"<<' '<<s1<<' '<<s2<<endl;
if(s1 + s2 == l-1)
{
g = false;
g1 = !g1;
g2 = !g2;
i++;
}
if(s1 +s2 == l)
{
g1 = !g1;
g2 = !g2;
}
if(!s1&&g)
{
g1 = false;
}
if(!s2&&g)
{
g2 = false;
}
if(g1) s1--;
if(!g1) s1++;
if(g2) s2--;
if(!g2) s2++;
// cout<<s1<<' '<<s2<<endl;
}
s2 = l-s2;
int d = abs(s1-s2);
cout<<d<<endl;
return 0;
}
这个是正解
#include <iostream>
#include <cmath>
using namespace std;
int l,t,k;
int main()
{
cin>>l>>t>>k; // 写题前,要多想
int d = k%l; // 先看 在那个位置
int dd = k/l; // 是正向,还是反向
if(dd%2)
{
d = l-d;
}
int y = (k-t)%l; // 同上
int yy = (k-t)/l;
if(yy%2) y = l-y;
int tt = abs(d-(l-y)); // 绝对值即可
cout<<tt<<endl;
return 0;
}