当前位置: 首页 > news >正文

CSP-J 2023 T3 一元二次方程 解题报告

CSP-J 2023 T3 一元二次方程 解题报告

Link

前言

今年 C S P CSP CSP的原题, 回家 1 h 1h 1h内写 A C AC AC, 但是考场上没有写出来 , 原因是脑子太不好了, 竟然调了两个小时没有调出来. 一等奖悬那…

正题

看完题目,第一眼就是大模拟, 并且 C C F CCF CCF绝对不会让你好受,所以出了一个如此刁钻的题目, 并且要考虑到非常多的情况, 代码非常长…

最重要的一点: Δ \Delta Δ

Δ \Delta Δ是此题中最重要的分情况讨论的地方. 根据初三 22 22 22章的所学知识 ,可知分三种情况:

1. Δ < 0 \Delta < 0 Δ<0

不用说了
直接输出NO

2. Δ = 0 \Delta = 0 Δ=0

同样的, 只有一种情况, 答案为 − b 2 a - \dfrac{b}{2a} 2ab,但是, 需要严谨的判断.

if(delta == 0) {if(b == 0) {cout << 0;}else if(a * b > 0) {a = abs(a);b = abs(b);cout << "-";if(b % (2 * a) == 0) {cout << b / 2 / a;}else {cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);}}else {a = abs(a);b = abs(b);if(b % (2 * a) == 0) {cout << b / 2 / a;}else {cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);}}
}

3. Δ > 0 \Delta > 0 Δ>0

地狱.
我是分两种情况的, 一种是 a > 0 a > 0 a>0, 一种是 a < 0 a < 0 a<0. 这样可以分辨出是 + Δ + \sqrt{\Delta } +Δ 还是 − Δ -\sqrt{\Delta } Δ

如若 a < 0 a < 0 a<0, 则可知答案为:
b + Δ − 2 a \dfrac{b + \sqrt{\Delta}}{-2a} 2ab+Δ

如若 a > 0 a > 0 a>0, 则可知答案为:
Δ − b 2 a \dfrac{\sqrt{\Delta} - b}{2a} 2aΔ b

  • 在这里有一个技巧, 就是不论怎样, 输出时, Δ \sqrt{\Delta} Δ 永远是正的(符号为+)

可以分两种情况:
1.第一种: 不需要写sqrt, 也就是 Δ \Delta Δ完全平方数时,

比较好处理, 首先需要判断 b + Δ b + \sqrt{\Delta} b+Δ 是否为 0 0 0. 如果是, 则直接输出 0 0 0; 否则输出最简分数.

其中, 一定要记住如果 ( b + Δ ) % ( 2 ∗ a ) = 0 (b + \sqrt{\Delta}) \% (2 * a) = 0 (b+Δ )%(2a)=0, 就直接输出一个整数.还要注意判断正负号.

2.第二种: 需要写sqrt, 很难.

首先, 先输出前面的内容, 也就是 − b 2 a -\dfrac{b}{2a} 2ab, 判断同上.

然后, 输出+, 代表符号.

接着, 找出三个变量, 也就是: x y Δ x 2 \dfrac{x}{y} \sqrt{\dfrac{\Delta}{x^2}} yxx2Δ 中的 x , y 和 Δ x 2 x, y和\dfrac{\Delta}{x^2} x,yx2Δ.其中, Δ x 2 \sqrt{\dfrac{\Delta}{x^2}} x2Δ 为最简平方根数.

接下来是 4 4 4种情况:

x = y x = y x=y, 只有 Δ x 2 \sqrt{\dfrac{\Delta}{x^2}} x2Δ ;

x % y = 0 x \% y = 0 x%y=0, 只有 x y Δ x 2 \dfrac{x}{y}\sqrt{\dfrac{\Delta}{x^2}} yxx2Δ

y % x = 0 y \% x = 0 y%x=0, 只有 Δ x 2 y \dfrac{\sqrt{\dfrac{\Delta}{x^2}}}{y} yx2Δ

其他情况, 输出 x × Δ x 2 y \dfrac{x \times \sqrt{\dfrac{\Delta}{x^2}}}{y} yx×x2Δ

完结撒花!!

C o d e : Code: Code:

  • 心脏病患者请勿观看
#include <bits/stdc++.h>
using namespace std;int T, M;
int a, b, c;int pd(int x) {for(int i = sqrt(x) + 1; i >= 1; --i) {if(x % (i * i) == 0) {return i;}}
}int main() {cin >> T >> M;while(T--) {cin >> a >> b >> c;int delta;delta = b * b - 4 * a * c;if(delta < 0) {cout << "NO";}else if(delta == 0) {if(b == 0) {cout << 0;}else if(a * b > 0) {a = abs(a);b = abs(b);cout << "-";if(b % (2 * a) == 0) {cout << b / 2 / a;}else {cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);}}else {a = abs(a);b = abs(b);if(b % (2 * a) == 0) {cout << b / 2 / a;}else {cout << b / __gcd(2 * a, b) << "/" << (2 * a) / __gcd(2 * a, b);}}}else {if(a < 0) {int mother = - 2 * a;int x = pd(delta);int y = delta / x / x;if(b == 0) {mother = abs(mother);if(y == 1) {if(x == mother) {cout << "1";}else if(x % mother == 0) {cout << x / mother;}else {cout << x / __gcd(x, mother) << "/" << mother / __gcd(x, mother);}}else {if(x == mother) {cout << "sqrt(" << y << ")";}else if(mother % x == 0) {cout << "sqrt(" << y << ")";cout << "/" << mother / x;}else if(x % mother == 0) {cout << x / mother << "*sqrt(" << y << ")";}else {cout << x / __gcd(x, mother) << "*sqrt(" << y << ")" << "/" << mother / __gcd(x, mother);}}}else if(y == 1) { // 不需要sqrt// 说明可以合并为同一个式子int son = - b - x;if(son == 0) {cout << 0;}else if(son * mother < 0) { // 如果分子分母同号.son = abs(son);mother = abs(mother);if(son % mother == 0) {cout << son / mother;}else {cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);}}else { // 如果分子分母异号.son = abs(son);mother = abs(mother);cout << "-";if(son % mother == 0) {cout << son / mother;}else {cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);}}}else { // 需要sqrt.// 1. 先输出前面的if(b > 0) { // 不需要负号b = abs(b);mother = abs(mother);if(b % mother == 0) {cout << b / mother;}else {cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);}}else { // 需要负号b = abs(b);mother = abs(mother);cout << "-";if(b % mother == 0) {cout << b / mother;}else {cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);}}// 2. 输出sqrt部分(不管怎样都是+)cout << "+";if(x == 1) { // 不需要输出前缀.cout << "sqrt(" << y << ")";cout << "/" << - 2 * a;}else {if(x == mother) {cout << "sqrt(" << y << ")";}else if(x % mother == 0) {cout << x / mother << "*sqrt(" << y << ")";}else if(mother % x == 0) {cout << "sqrt(" << y << ")";cout << "/" << mother / x;}else {cout << x / __gcd(x, mother);cout << "*sqrt(" << y << ")";cout << "/" << mother / __gcd(x, mother);}}}}else {int mother = 2 * a;int x = pd(delta);int y = delta / x / x;if(b == 0) {mother = abs(mother);if(y == 1) {if(x == mother) {cout << "1";}else if(x % mother == 0) {cout << x / mother;}else {cout << x / __gcd(x, mother) << "/" << mother / __gcd(x, mother);}}else {if(x == mother) {cout << "sqrt(" << y << ")";}else if(mother % x == 0) {cout << "sqrt(" << y << ")";cout << "/" << mother / x;}else if(x % mother == 0) {cout << x / mother << "*sqrt(" << y << ")";}else {cout << x / __gcd(x, mother) << "*sqrt(" << y << ")" << "/" << mother / __gcd(x, mother);}}}else if(y == 1) { // 不需要sqrt// 说明可以合并为同一个式子int son = - b + x;if(son == 0) {cout << 0;}else if(son * mother > 0) { // 如果分子分母同号.son = abs(son);mother = abs(mother);if(son % mother == 0) {cout << son / mother;}else {cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);}}else { // 如果分子分母异号.son = abs(son);mother = abs(mother);cout << "-";if(son % mother == 0) {cout << son / mother;}else {cout << son / __gcd(son, mother) << "/" << mother / __gcd(son, mother);}}}else { // 需要sqrt.// 1. 先输出前面的if(b * mother < 0) { // 不需要负号b = abs(b);mother = abs(mother);if(b % mother == 0) {cout << b / mother;}else {cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);}}else { // 需要负号b = abs(b);mother = abs(mother);cout << "-";if(b % mother == 0) {cout << b / mother;}else {cout << b / __gcd(b, mother) << "/" << mother / __gcd(b, mother);}}// 2. 输出sqrt部分(不管怎样都是+)cout << "+";if(x == 1) { // 不需要输出前缀.cout << "sqrt(" << y << ")";cout << "/" << 2 * a;}else {mother = 2 * a;if(x == mother) {cout << "sqrt(" << y << ")";}else if(x % mother == 0) {cout << x / mother << "*sqrt(" << y << ")";}else if(mother % x == 0) {cout << "sqrt(" << y << ")";cout << "/" << mother / x;}else {cout << x / __gcd(x, mother);cout << "*sqrt(" << y << ")";cout << "/" << mother / __gcd(x, mother);}}}}}cout << endl;}return 0;
}

相关文章:

CSP-J 2023 T3 一元二次方程 解题报告

CSP-J 2023 T3 一元二次方程 解题报告 Link 前言 今年 C S P CSP CSP的原题, 回家 1 h 1h 1h内写 A C AC AC, 但是考场上没有写出来 , 原因是脑子太不好了, 竟然调了两个小时没有调出来. 一等奖悬那… 正题 看完题目,第一眼就是大模拟, 并且 C C F CCF CCF绝对不会让你好受…...

中颖单片机SH367309全套量产PCM,专用动力电池保护板开发资料

方案总体介绍 整套方案硬件部分共2块板子&#xff0c;包括MCU主板&#xff0c;采用SH79F6441-32作为主处理器。MCU主板包括2个版本。PCM动力电池保护板采用SH367309。 软件方案采用Keil51建立的工程&#xff0c;带蓝牙的版本&#xff0c;支持5~16S电池。 硬件方案--MCU主板 MC…...

Android数据对象序列化原理与应用

序列化与反序列化 序列化是将对象转换为可以存储或传输的格式的过程。在计算机科学中&#xff0c;对象通常是指内存中的数据结构&#xff0c;如数组、列表、字典等。通过序列化&#xff0c;可以将这些对象转换为字节流或文本格式&#xff0c;以便在不同的系统之间进行传输或存…...

Linux cp命令:复制文件和目录

cp 命令&#xff0c;主要用来复制文件和目录&#xff0c;同时借助某些选项&#xff0c;还可以实现复制整个目录&#xff0c;以及比对两文件的新旧而予以升级等功能。 cp 命令的基本格式如下&#xff1a; [rootlocalhost ~]# cp [选项] 源文件 目标文件 选项&#xff1a; -a&…...

SpringBoot 接收不到 post 请求数据与接收 post 请求数据

文章归档&#xff1a;https://www.yuque.com/u27599042/coding_star/xwrknb7qyhqgdt10 SpringBoot 接收不到 post 请求数据 接收 post 请求数据&#xff0c;控制器方法参数需要使用 RequestParam 注解修饰 public BaseResponseResult<Object> getMailCode(RequestParam…...

vue3学习(十四)--- vue3中css新特性

文章目录 样式穿透:deep()scoped的原理 插槽选择器:slotted()全局选择器:global()动态绑定CSScss module 样式穿透:deep() 主要是用于修改很多vue常用的组件库&#xff08;element, vant, AntDesigin&#xff09;&#xff0c;虽然配好了样式但是还是需要更改其他的样式就需要用…...

Python爬虫基础之Requests详解

目录 1. 简介2. 安装3. 发送请求4. 处理响应5. IP代理6. Cookie登录参考文献 原文地址&#xff1a;https://program-park.top/2023/10/27/reptile_4/ 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由…...

C++求根节点到叶子节点数字之和

文章目录 题目链接题目描述解题思路代码复杂度分析 题目链接 LCR 049. 求根节点到叶节点数字之和 - 力扣&#xff08;LeetCode&#xff09; 题目描述 给定一个二叉树的根节点 root &#xff0c;树中每个节点都存放有一个 0 到 9 之间的数字。 每条从根节点到叶节点的路径都代表…...

C++搜索二叉树

本章主要是二叉树的进阶部分&#xff0c;学习搜索二叉树可以更好理解后面的map和set的特性。 1.二叉搜索树概念 二叉搜索树的递归定义为&#xff1a;非空左子树所有元素都小于根节点的值&#xff0c;非空右子树所有元素都大于根节点的值&#xff0c;而左右子树也是二叉搜索树…...

软件工程17-18期末试卷

2.敏捷开发提倡一个迭代80%以上的时间都在编程&#xff0c;几乎没有设计阶段。敏捷方法可以说是一种无计划性和纪律性的方法。错 敏捷开发是一种软件开发方法论&#xff0c;它强调快速响应变化、持续交付有价值的软件、紧密合作和适应性。虽然敏捷方法鼓励迭代开发和灵活性&…...

课题学习(九)----阅读《导向钻井工具姿态动态测量的自适应滤波方法》论文笔记

一、 引言 引言直接从原论文复制&#xff0c;大概看一下论文的关键点&#xff1a; 垂直导向钻井工具在近钻头振动和工具旋转的钻井工作状态下&#xff0c;工具姿态参数的动态测量精度不高。为此&#xff0c;通过理论分析和数值仿真&#xff0c;提出了转速补偿的算法以消除工具旋…...

阿里云服务器—ECS快速入门

这里对标阿里云的课程&#xff0c;一步步学习&#xff0c;链接在下面&#xff0c;学习完考试及格即可获取阿里云开发认证和领取证书&#xff0c;大家可以看看这个&#xff0c;这里我当作笔记&#xff0c;记一下提升印象&#xff01; 内容很长&#xff0c;请耐心看完&#xff0…...

Hive简介及核心概念

本专栏案例数据集链接: https://download.csdn.net/download/shangjg03/88478038 1.简介 Hive 是一个构建在 Hadoop 之上的数据仓库,它可以将结构化的数据文件映射成表,并提供类 SQL 查询功能,用于查询的 SQL 语句会被转化为 MapReduce 作业,然后提交到 Hadoop 上运行。 …...

CrossOver 23.6.0 虚拟机新功能介绍

CrossOver 23.6.0 Mac 此应用程序允许您运行为 Microsoft Windows 编写的程序&#xff0c;而无需实际安装操作系统。 CrossOver 23.6.0 Mac 包括一个 Windows 程序库&#xff0c;用于它可以运行的 Windows 程序。 您会发现非常流行的应用程序&#xff0c;例如 Microsoft Word…...

(免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐

摘 要 随着互联网趋势的到来&#xff0c;各行各业都在考虑利用互联网将自己推广出去&#xff0c;最好方式就是建立自己的互联网系统&#xff0c;并对其进行维护和管理。在现实运用中&#xff0c;应用软件的工作规则和开发步骤&#xff0c;采用Java技术建设农产品销售管理系统。…...

centos更改yum源

1、更改yum源 阿里云/etc/yum.repos.d/CentOS-Base.repo 金山云/etc/yum.repos.d/cloud.repo vi /etc/yum.repos.d/cloud.repo 替换为 [base] nameCentOS-$releasever - Base mirrorlisthttp://mirrorlist.centos.org/?release$releasever&arch$basearch&repoos&…...

React-快速搭建开发环境

1.安装 说明&#xff1a;react-excise-01是创建的文件名 npx create-react-app react-excise-01 2. 打开文件 说明:we suggest that you begin by typing:下面即是步骤。 cd react-excise-01 npm start 3.显示...

算法随想录算法训练营第四十六天| 583. 两个字符串的删除操作 72. 编辑距离

583. 两个字符串的删除操作 题目&#xff1a;给定两个单词 word1 和 word2 &#xff0c;返回使得 word1 和 word2 相同所需的最小步数。 每步 可以删除任意一个字符串中的一个字符。 思路&#xff1a;这题思路主要是求出 word1 字符串和 word2 字符串中的最长相同的子字符串&…...

vue源码分析(五)——vue render 函数的使用

文章目录 前言一、render函数1、render函数是什么&#xff1f; 二、render 源码分析1.执行initRender方法2.vm._c 和 vm.$createElement 调用 createElement 方法详解&#xff08;1&#xff09;区别&#xff08;2&#xff09;代码 3、原型上的_render方法&#xff08;1&#xf…...

Maven第三章:IDEA集成与常见问题

Maven第三章:IDEA集成与常见问题 前言 本章内容重点:了解如何将Maven集成到IDE(如IntelliJ IDEA或Eclipse)中,以及使用过程中遇到的常见的问题、如何解决,如何避免等,可以大大提高开发效率。 IEAD导入Maven项目 File ->Open 选择上一章创建的Maven项目 my-app查看po…...

Cursor实现用excel数据填充word模版的方法

cursor主页&#xff1a;https://www.cursor.com/ 任务目标&#xff1a;把excel格式的数据里的单元格&#xff0c;按照某一个固定模版填充到word中 文章目录 注意事项逐步生成程序1. 确定格式2. 调试程序 注意事项 直接给一个excel文件和最终呈现的word文件的示例&#xff0c;…...

树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频

使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...

Java 8 Stream API 入门到实践详解

一、告别 for 循环&#xff01; 传统痛点&#xff1a; Java 8 之前&#xff0c;集合操作离不开冗长的 for 循环和匿名类。例如&#xff0c;过滤列表中的偶数&#xff1a; List<Integer> list Arrays.asList(1, 2, 3, 4, 5); List<Integer> evens new ArrayList…...

华为OD机试-食堂供餐-二分法

import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...

PL0语法,分析器实现!

简介 PL/0 是一种简单的编程语言,通常用于教学编译原理。它的语法结构清晰,功能包括常量定义、变量声明、过程(子程序)定义以及基本的控制结构(如条件语句和循环语句)。 PL/0 语法规范 PL/0 是一种教学用的小型编程语言,由 Niklaus Wirth 设计,用于展示编译原理的核…...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

Axios请求超时重发机制

Axios 超时重新请求实现方案 在 Axios 中实现超时重新请求可以通过以下几种方式&#xff1a; 1. 使用拦截器实现自动重试 import axios from axios;// 创建axios实例 const instance axios.create();// 设置超时时间 instance.defaults.timeout 5000;// 最大重试次数 cons…...

CMake 从 GitHub 下载第三方库并使用

有时我们希望直接使用 GitHub 上的开源库,而不想手动下载、编译和安装。 可以利用 CMake 提供的 FetchContent 模块来实现自动下载、构建和链接第三方库。 FetchContent 命令官方文档✅ 示例代码 我们将以 fmt 这个流行的格式化库为例,演示如何: 使用 FetchContent 从 GitH…...

C++使用 new 来创建动态数组

问题&#xff1a; 不能使用变量定义数组大小 原因&#xff1a; 这是因为数组在内存中是连续存储的&#xff0c;编译器需要在编译阶段就确定数组的大小&#xff0c;以便正确地分配内存空间。如果允许使用变量来定义数组的大小&#xff0c;那么编译器就无法在编译时确定数组的大…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...