当前位置:网站首页>【暑期每日一题】洛谷 P2026 求一次函数解析式
【暑期每日一题】洛谷 P2026 求一次函数解析式
2022-07-01 04:47:00 【AC_Dragon】
题目链接:P2026 求一次函数解析式 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目背景
做数学寒假作业的怨念……
题目描述
给定两个整点的坐标,求它们所在直线的函数解析式(一次函数)。
输入格式
输入共两行。
第一行有两个整数x1,y1。表示第一个整点的坐标为(x1,y1)
第二行有两个整数x2,y2。表示第二个整点的坐标为(x2,y2)
输出格式
输出共一行,即这个函数解析式。
样例 #1
样例输入 #1
3 6
2 4样例输出 #1
y=2x样例 #2
样例输入 #2
3 5
2 7样例输出 #2
y=-2x+11样例 #3
样例输入 #3
3 -1
5 -2样例输出 #3
y=-1/2*x+1/2提示
二乘X表示为2x
二分之一乘X表示为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; // 斜率分子,分母
k_fz=y2-y1;
k_fm=x2-x1;
// 斜率为整数(截距一定也为整数)
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;
}
// 斜率为分数
if(k_fz*1.0/k_fm>0) // 斜率为正数
{
k_fz=abs(y2-y1);
k_fm=abs(x2-x1);
// 斜率化成最简分数
k_fz=k_fz/__gcd(k_fz,k_fm);
k_fm=k_fm/__gcd(k_fz,k_fm);
int b_fz,b_fm; // 截距分子,分母
b_fz=y1*k_fm-k_fz*x1;
b_fm=k_fm;
if(b_fz%b_fm==0) // 截距为整数
{
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 // 斜率为负数
{
k_fz=abs(y2-y1);
k_fm=abs(x2-x1);
// 斜率化成最简分数
k_fz=k_fz/__gcd(k_fz,k_fm);
k_fm=k_fm/__gcd(k_fz,k_fm);
int b_fz,b_fm; // 截距分子,分母
b_fz=y1*k_fm+k_fz*x1; // 注意此时为 "+" 号 (因为斜率为负数)
b_fm=k_fm;
if(b_fz%b_fm==0) // 截距为整数
{
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;
}边栏推荐
猜你喜欢

2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers

2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination

LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target

VR线上展览所具备应用及特色

Software testing needs more and more talents. Why do you still not want to take this path?

2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis

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

分布式数据库数据一致性的原理、与技术实现方案

Neural network convolution layer

STM32 光敏电阻传感器&两路AD采集
随机推荐
Basic skeleton of neural network nn Use of moudle
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
Simple implementation of slf4j
Introduction to JVM stack and heap
Summary of acl2021 information extraction related papers
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
【硬十宝典】——1.【基础知识】电源的分类
神经网络-最大池化的使用
Basic exercise of test questions hexadecimal to decimal
Common interview questions ①
The design points of voice dialogue system and the importance of multi round dialogue
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
Codeworks round 449 (Div. 1) C. Kodori tree template
Leecode record 1351 negative numbers in statistical ordered matrix
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
LeetCode_28(实现 strStr())
About the transmission pipeline of stage in spark
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
分布式事务-解决方案
Solve the problem that the external chain file of Qiankun sub application cannot be obtained