当前位置:网站首页>[daily question in summer] Luogu p2026 find the analytic formula of primary function
[daily question in summer] Luogu p2026 find the analytic formula of primary function
2022-07-01 04:47:00 【AC_ Dragon】
Background
Complaints about doing math winter vacation homework ……
Title Description
Given the coordinates of two integral points , Find the analytic function of the line where they are located ( Once function ).
Input format
There are two lines of input .
The first line has two integers x1,y1. The coordinates of the first integral point are (x1,y1)
The second line has two integers x2,y2. The coordinate of the second integral point is (x2,y2)
Output format
The output is one line , That is, the analytic expression of this function .
Examples #1
The sample input #1
3 6
2 4Sample output #1
y=2xExamples #2
The sample input #2
3 5
2 7Sample output #2
y=-2x+11Examples #3
The sample input #3
3 -1
5 -2Sample output #3
y=-1/2*x+1/2Tips
Mahayana and Hinayana X Expressed as 2x
Half multiply X Expressed as 1/2\*x
AC code:
#include<iostream>
#include<algorithm>
using namespace std;
// y=k*x+b;
// k=(y2-y1)/(x2-x1)
// b=y-k*x;
int main()
{
int x1,y1;
cin>>x1>>y1;
int x2,y2;
cin>>x2>>y2;
int k_fz,k_fm; // Slope molecule , The denominator
k_fz=y2-y1;
k_fm=x2-x1;
// The slope is an integer ( Intercept must also be an integer )
if(k_fz%k_fm==0)
{
int b=y1-(k_fz/k_fm)*x1;
if(b>0)
cout<<"y="<<k_fz/k_fm<<"x"<<"+"<<b<<endl;
else if(b<0)
cout<<"y="<<k_fz/k_fm<<"x"<<b<<endl;
else
cout<<"y="<<k_fz/k_fm<<"x"<<endl;
return 0;
}
// The slope is a fraction
if(k_fz*1.0/k_fm>0) // The slope is positive
{
k_fz=abs(y2-y1);
k_fm=abs(x2-x1);
// The slope is reduced to the simplest fraction
k_fz=k_fz/__gcd(k_fz,k_fm);
k_fm=k_fm/__gcd(k_fz,k_fm);
int b_fz,b_fm; // Intercept molecule , The denominator
b_fz=y1*k_fm-k_fz*x1;
b_fm=k_fm;
if(b_fz%b_fm==0) // Intercept is an integer
{
if(b_fz*1.0/b_fm>0)
cout<<"y="<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<"+"<<b_fz/b_fm<<endl;
else if(b_fz*1.0/b_fm<0)
cout<<"y="<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<b_fz/b_fm<<endl;
else
cout<<"y="<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<endl;
return 0;
}
if(b_fz*1.0/b_fm>0)
cout<<"y="<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<"+"<<abs(b_fz)/__gcd(abs(b_fz),abs(b_fm))<<"/"<<abs(b_fm)/__gcd(abs(b_fz),abs(b_fm))<<endl;
else if(b_fz*1.0/b_fm<0)
cout<<"y="<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<"-"<<abs(b_fz)/__gcd(abs(b_fz),abs(b_fm))<<"/"<<abs(b_fm)/__gcd(abs(b_fz),abs(b_fm))<<endl;
else
cout<<"y="<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<endl;
}
else // The slope is negative
{
k_fz=abs(y2-y1);
k_fm=abs(x2-x1);
// The slope is reduced to the simplest fraction
k_fz=k_fz/__gcd(k_fz,k_fm);
k_fm=k_fm/__gcd(k_fz,k_fm);
int b_fz,b_fm; // Intercept molecule , The denominator
b_fz=y1*k_fm+k_fz*x1; // Note that this is "+" Number ( Because the slope is negative )
b_fm=k_fm;
if(b_fz%b_fm==0) // Intercept is an integer
{
if(b_fz*1.0/b_fm>0)
cout<<"y="<<"-"<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<"+"<<b_fz/b_fm<<endl;
else if(b_fz*1.0/b_fm<0)
cout<<"y="<<"-"<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<b_fz/b_fm<<endl;
else
cout<<"y="<<"-"<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<endl;
return 0;
}
if(b_fz*1.0/b_fm>0)
cout<<"y="<<"-"<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<"+"<<abs(b_fz)/__gcd(abs(b_fz),abs(b_fm))<<"/"<<abs(b_fm)/__gcd(abs(b_fz),abs(b_fm))<<endl;
else if(b_fz*1.0/b_fm<0)
cout<<"y="<<"-"<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<"-"<<abs(b_fz)/__gcd(abs(b_fz),abs(b_fm))<<"/"<<abs(b_fm)/__gcd(abs(b_fz),abs(b_fm))<<endl;
else
cout<<"y="<<"-"<<abs(k_fz)<<"/"<<abs(k_fm)<<"*x"<<endl;
}
return 0;
}边栏推荐
猜你喜欢

Cmake selecting compilers and setting compiler options

The longest increasing subsequence and its optimal solution, total animal weight problem

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

Common methods in transforms
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding

【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点

Pytorch(二) —— 激活函数、损失函数及其梯度

Use and modification of prior network model

神经网络-使用Sequential搭建神经网络

手动实现一个简单的栈
随机推荐
Pytoch (IV) -- visual tool visdom
VR线上展览所具备应用及特色
Codeworks round 449 (Div. 1) C. Kodori tree template
Basic usage, principle and details of session
Introduction to JVM stack and heap
分布式全局唯一ID解决方案详解
Solve the problem that the external chain file of Qiankun sub application cannot be obtained
字符输入流与字符输出流
Software testing needs more and more talents. Why do you still not want to take this path?
Shell之Unix运维常用命令
VIM简易使用教程
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
This sideline workload is small, 10-15k, free unlimited massage
Leecode record 1351 negative numbers in statistical ordered matrix
打印流与System.setout();
Matters behind the construction of paint testing laboratory
CF1638E. Colorful operations Kodori tree + differential tree array
神经网络-卷积层
缓冲流与转换流
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)