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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/122190.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JKPacket权威指南——学习建议

初学者 1&#xff0c;了解基本的swift语法&#xff0c;如果还没有从OC转swift的开发者建议尽早转swift了。 2&#xff0c;只需要下载源码并运行源码中demo&#xff0c;并阅读下具体使用的demo代码即可简单使用。 资深大佬 1&#xff0c;需要储备一定的rxswift相关知识&#…

ResNet(CVPR2016)

文章目录 AbstractIntroductionRelated WorkResidual RepresentationsShortcut Connections Deep Residual LearningResidual LearningIdentity Mapping by Shortcuts ExperimentConclusion 原文链接 Abstract 深层的神经网络更难训练&#xff0c;我们提出了一个残差学习框架&…

Day2-数组第二部分、双指针|LeetCode27、209、977、59|代码随想录

阅前声明&#xff1a;本人java基础不好&#xff0c;以后写的时候会加上一些java语法需要注意的地方&#xff0c;如果问题还请多多交流指正&#xff01; 数组第二部分主要是针对双指针思想展开的。 双指针思想 设置两个指针&#xff0c;fast指针用来指向新数组的内容&#xff…

怎么突破反爬虫机制

在当今的数字化时代&#xff0c;网络爬虫已经成为了收集信息和数据的重要工具。然而&#xff0c;许多网站和平台都配备了反爬虫机制&#xff0c;以防止恶意攻击和过度访问。对于普通用户来说&#xff0c;如何突破这些反爬虫机制呢&#xff1f;本文将为你提供一些实用的技巧和建…

Nginx的进程结构实例演示

可以参考《Ubuntu 20.04使用源码安装nginx 1.14.0》安装nginx 1.14.0。 nginx.conf文件中worker_processes 2;这条语句表明启动两个worker进程。 sudo /nginx/sbin/nginx -c /nginx/conf/nginx.conf开启nginx。 ps -ef | grep nginx看一下进程情况。 sudo /nginx/sbin/ng…

Git 拉取远程更新报错

报错内容如下&#xff1a; cannot lock ref refs/remotes/origin/bugfix/bug: refs/remotes/origin/bugfix 已存在&#xff0c;无法创建 refs/remotes/origin/bugfix/bug 来自 gitlab.zhangyue-inc.com:dejian_ios/iReaderDejian! [新分支] bugfix/bug -> ori…

Hive 视图和索引

本专栏案例数据集链接: https://download.csdn.net/download/shangjg03/88478038 1.视图 1.1 简介 Hive 中的视图和 RDBMS 中视图的概念一致,都是一组数据的逻辑表示,本质上就是一条 SELECT 语句的结果集。视图是纯粹的逻辑对象,没有关联的存储 (Hive 3.0.0 引入的物化视图…

防关联浏览器推荐:MuLogin指纹浏览器安全登录多平台账号

在现今的数字时代&#xff0c;我们的生活离不开互联网。我们使用在线平台进行银行交易、购物、社交媒体互动和其他各种活动。为了保护个人隐私和账号安全&#xff0c;我们需要寻找一种安全且方便的方式来管理我们的在线账号。MuLogin指纹浏览器正是为了满足这些需求而设计的一款…

【Bond随你温故Kubernetes之】壹图复盘service与内部通信

最近跟朋友聊到了k8s 我&#xff1a; “环境给了就只管用呗&#xff0c;副本自动管理地妥妥的&#xff0c;有啥可以复盘的&#xff1f;“ 朋友&#xff1a; “容器的通讯与服务暴露还是有点东西的” 我&#xff1a; “嗯&#xff5e;&#xff5e;&#xff08;抿嘴点…

matlab中字符串转换为数字(str2double函数)

str2double函数 将 str 中的文本转换为双精度值。str 包含表示实数或复数值的文本。str 可以是字符向量、字符向量元胞数组或字符串数组。如果 str 是字符向量或字符串标量&#xff0c;则 X 是数值标量。如果 str 是字符向量元胞数组或字符串数组&#xff0c;则 X 是与 str 具…

C语言每日一题(21)删除排序数组中的重复项

力扣 26.删除排序数组中的重复项 题目描述 给你一个 非严格递增排列 的数组 nums &#xff0c;请你 原地 删除重复出现的元素&#xff0c;使每个元素 只出现一次 &#xff0c;返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。 考…

设计模式之创建型模式

创建型模式与对象的创建有关。 创建型模式抽象了对象实例化的过程&#xff0c;这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式&#xff0c;而不是使用 new 运算符直接实例化对象。创建型模式有以下 工厂模式&#xff08;Factory Method&#xff09; 意图&#xf…

CompletableFuture常见方法以及使用

常用场景&#xff1a; 1.并行执行多个任务&#xff1a;CompletableFuture 可以用于并行执行多个任务&#xff0c;从而提高性能 2.并行执行多个任务&#xff1a;CompletableFuture 可以用于并行执行多个任务&#xff0c;从而提高性能 3.任务依赖和组合&#xff1a;Completabl…

计算机网络基础二

课程目标 了解 OSI 七层模型分层结构 了解 TCP/IP 协议簇四层模型分层结构 能够说出 TCP/IP 协议簇中 运输层、网络层和数据链路 层常见的 相关协议 能够说出 TCP/IP 的三次握手四次断开过程 了解 Vmware 的三种网络模式 能够使用客户端工具连接虚拟机 掌握主机名、 DNS…

Android S从桌面点击图标启动APP流程 (六)

系列文章 Android S从桌面点击图标启动APP流程 (一)Android S从桌面点击图标启动APP流程 (二) Android S从桌面点击图标启动APP流程 (三) Android S从桌面点击图标启动APP流程 (四) Android S从桌面点击图标启动APP流程 (五) Android 12的源码链接&#xff1a; android 1…

【OpenCV实现图像的算数运算,性能测试和优化,改变颜色空间】

文章目录 OpenCV功能概要图像的算数运算性能测试和优化改变颜色空间对象追踪 OpenCV功能概要 OpenCV&#xff08;Open Source Computer Vision Library&#xff09;是一个开源的计算机视觉和机器学习库&#xff0c;提供了丰富的图像处理和计算机视觉算法。它支持多种编程语言&…

js中数组的相关方法

引言&#xff1a; 数组&#xff08;Array&#xff09;是有序的元素序列。 [1]若将有限个类型相同的变量的集合命名&#xff0c;那么这个名称为数组名。组成数组的各个变量称为数组的分量&#xff0c;也称为数组的元素&#xff0c;有时也称为下标变量 方法&#xff1a; push()…

数字化转型系列主题:战略咨询常用术语解释和样例说明

引言 做战略咨询的人经常提到一些术语 ”某企业的愿景&#xff0c;某企业的价值观&#xff0c;战略目标&#xff0c;举措&#xff0c;行动&#xff0c;挑战&#xff0c;...“, 这些术语对刚进入咨询行业的小白或其它行业的人&#xff0c;经常会分不清楚&#xff0c;弄的一头雾…

Java中的volatile关键字

volatile是什么&#xff1f; "volatile"是一个关键字&#xff0c;用于修饰变量。它的作用是告诉编译器该变量可能会在意料之外的时候被修改&#xff0c;因此编译器在对该变量进行优化时需要特别小心。 具体来说&#xff0c;当一个变量被声明为"volatile"…

dd命令用法学习,是一个功能强大的工具

dd 命令是一个功能强大的工具&#xff0c;它有许多参数可以用来控制其行为。以下是 dd 命令中常用的一些参数&#xff1a; - ifinputfile&#xff1a;指定输入文件的路径。 - ofoutputfile&#xff1a;指定输出文件的路径。 - bssize&#xff1a;设置每个块的大小。可以使用不同…