分类讨论的技巧

在高为 H 的天花板上有 n 个小球,体积不计,位置分别为 0,1,2,….n-1。在地面上有一个小车(长为 L,高为 K,距原点距离为 S1)。已知小球下落距离计算公式为 d=1/2*g*(t^2),其中 g=10,t 为下落时间。地面上的小车以速度 V 前进。

(小车在原点左侧)

小车与所有小球同时开始运动,当小球距小车的距离 <= 0.00001 时,即认为小球被小车接受(小球落到地面后不能被接受)。

  请你计算出小车能接受到多少个小球。

# include<iostream>
# include<math.h>
using namespace std;
int main()
{
    double H,S1,V,L,K,t;
    int n,count=0;
    cin>>H>>S1>>V>>L>>K>>n;
    t = sqrt(2*(H-K)/10.0);
    double s1 = V*t - S1;
    double s2 = s1 + L;
    cout<<s2<<endl;
    if(s2<-0.00001) //车尾车头 都在点的左侧 
    {
        cout<<count;
    }
    else if(s1<-0.00001 && s2>=-0.00001 && s2<=n-1+0.00001) //车尾在点的左侧 车头在点的中间 
    {
        while(s2>=-0.00001)
        {
            count++;
            s2 -= 1.0;
        }
        count<<count;
    }
    else if(s1<-0.00001 && s2>n-1+0.00001)     //车尾在点的左侧 车头在点的右侧 
    {
        cout<<n;
    }
    else if(s1>=-0.00001 && s2<=n-1+0.00001) // 车尾在点的中间 车头在点的中间 
    {
          while(s1-s2 <= 0.00002)
          {
               count++;
               s1 += 1;
          }
          cout<<count;
    }
    else if(s1-n+1<=0.00001 && s2-n+1>0.00001 && s1 >=-0.00001) //车尾在点的中间 车头在点的右侧 
    {
        while(s1-n+1<=0.00001)
        {
            count++;
            s1 += 1.0;
        }
        cout<<count;
    }
    else                                    //车尾在点的右侧  车头在点的右侧 
    {
        cout<<count;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wshyj/p/6349319.html