由信仰之跃学到一些类的知识 特别是分数运算
/*
* @Author: error: git config user.name && git config user.email & please set dead value or install git
* @Date: 2022-08-05 15:15:22
* @LastEditors: error: git config user.name && git config user.email & please set dead value or install git
* @LastEditTime: 2022-08-05 18:30:35
* @FilePath: \C++\testC++\JumpWall.cpp
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
const int g=18;
class Assassin{
private:
int x1;//水平方向区间的开始
int x2;//区间结束
int y;//垂直方向
public:
Assassin(int x1,int x2,int y);
int X1Get();
void X1Set(int x1);
int X2Get();
void X2Set(int x2);
int YGet();
void YSet(int y);
~Assassin(){};
};
struct Fraction{
int up;//分子
int down;//分母
Fraction(){};
Fraction(int _up,int _down)
{
this->up = _up;
this->down = _down;
}
};
Fraction f1;
Fraction f2;
vector<Fraction> v1;//存放区间
//辗转相除法,递归,导致时间复杂度太大
int gcd(int a,int b)
{
if(b==0)
{
return a;
}
else
{
return gcd(b,a%b);
}
}
//分数的化简
Fraction reduction(Fraction result)
{
if(result.down < 0)
{
result.up *= (-1);
result.down *= (-1);
}
if(result.up == 0)
result.down = 1;
int _gcd = gcd(abs(result.up),abs(result.down));
result.up = result.up/_gcd;
result.down = result.down/_gcd;
return result;
}
Assassin::Assassin(int x1,int x2,int y)
{
this->x1=x1;
this->x2=x2;
this->y=y;
}
int Assassin::X1Get()
{
return x1;
}
void Assassin::X1Set(int x1)
{
this->x1=x1;
}
int Assassin::X2Get()
{
return x2;
}
void Assassin::X2Set(int x2)
{
this->x2=x2;
}
int Assassin::YGet()
{
return y;
}
void Assassin::YSet(int y)
{
this->y=y;
}
Fraction divide(Fraction f1 , Fraction f2)
{
Fraction f3;
if(f2.up == 0)
return f2;//分母为0
f3.up = f1.up*f2.down;
f3.down = f1.down*f2.up;
f3 = reduction(f3);
return f3;
}
void print(Fraction f1,Fraction f2)
{
if(f1.down==1 && f2.down==1)//如果分母是1,则当做整数输出
{
cout<<f1.up<<" "<<f2.up<<endl;
}
else if(f1.down==1 && f2.down>1)
{
cout<<f1.up<<" "<<f2.up<<"/"<<f2.down<<endl;
}
else if(f1.down>1 && f2.down==1)
{
cout<<f1.up<<"/"<<f1.down<<" "<<f2.up<<endl;
}
else
{
cout<<f1.up<<"/"<<f1.down<<" "<<f2.up<<"/"<<f2.down<<endl;
}
}
void CalV(int &d1,int &d2,int &h)
{
Assassin assassin(d1,d2,h);
Fraction t;//表示t
//根据y=1/2gt^2,求t,t可能为分数
//int t=int(sqrt(2*assassin.YGet()/g));
t.up=int(sqrt(assassin.YGet()));
t.down=sqrt(g/2);
t=reduction(t);//化简
//根据x=vt,求v,v可能为分数
//将f1(x1)看做一个分数
f1.up=assassin.X1Get();
f1.down=1;
//v=x/t
f1=divide(f1,t);
f1=reduction(f1);//约分
v1.push_back(f1);
//f2(x2)
f2.up=assassin.X2Get();
f2.down=1;
f2=divide(f2,t);
f2=reduction(f2);//约分
v1.push_back(f2);
}
int main()
{
int T;
cin>>T;
int h,d1,d2;
for(int i=0;i<T;++i)
{
cin>>h>>d1>>d2;
CalV(d1,d2,h);
}
//正向输出所有元素
vector<Fraction>::iterator it;
for(it=v1.begin();it!=v1.end();it+=2)
{
print(*it,*(it+1));
}
return 0;
}