对于一元二次方程 $$ax^2 + bx + c = 0,解可以分为很多情况。若该方程有两个不相等实根,首先输出1,换行,然后从小到大输出两个实根,换行;若该方程有两个相等实根,首先输出2,换行,然后输出这个这个实根,换行;若该方程有一对共轭复根,输出3,换行;若该方程无解,输出4,换行;若该方程有无穷个解,输出5,换行;若该方程只有一个根,首先输出6,换行,然后输出这个跟,换行;要求使用 c++ class 编写程序。可以创建如下 class #include <iostream> #include <cmath> using namespace std; class Equation{ private: int _a, _b, _c; public: Equation(int a, int b, int c){ } void solve(){ } }; int main(){ int a, b, c; cin >> a >> b >> c; Equation tmp(a, b, c); tmp.solve()
```cpp #include <iostream> #include <cmath> using namespace std; class Equation { private: int _a, _b, _c; public: Equation(int a, int b, int c) : _a(a), _b(b), _c(c) {} void solve() { double discriminant = _b * _b - 4 * _a * _c; if (_a == 0) { if (_b == 0) { if (_c == 0) { cout << "5" << endl; } else { cout << "4" << endl; } } else { double root = -_c / _b; cout << "6" << endl << fixed << setprecision(2) << root << endl; } } else { if (discriminant > 0) { double root1 = (-_b + sqrt(discriminant)) / (2 * _a); double root2 = (-_b - sqrt(discriminant)) / (2 * _a); cout << "1" << endl << fixed << setprecision(2) << min(root1, root2) << endl << fixed << setprecision(2) << max(root1, root2) << endl; } else if (discriminant == 0) { double root = -_b / (2 * _a); cout << "2" << endl << fixed << setprecision(2) << root << endl; } else { cout << "3" << endl; } } } }; int main() { int a, b, c; cin >> a >> b >> c; Equation tmp(a, b, c); tmp.solve(); } ``` 额外需要注意的逻辑点: 1. 对于浮点数的输出格式控制,确保精度符合要求。 2. 处理特殊情况,如系数为 0 时的各种可能性。 3. 对输入的系数范围进行有效的校验。 [2025-03-07 12:49:26 | AI写代码神器 | 481点数解答]
- [字节豆包] 对于一元二次方程 $$ax^2 + bx + c = 0,解可以分为很多情况。若该方程有两个不相等实根,首先输出1,换行,然后从小到大输出两个实根,换行;若该方程有两个相等实根,首先输出2,换行,然后输出这个这个实根,换行;若该方程有一对共轭复根,输出3,换行;若该方程无解,输出4,换行;若该方程有无穷个解,输出5,换行;若该方程只有一个根,首先输出6,换行,然后输出这个跟,换行;要求使用 c++ class 编写程序。可以创建如下 class #include <iostream> #include <cmath> using namespace std; class Equation{ private: int _a, _b, _c; public: Equation(int a, int b, int c){ } void solve(){ } }; int main(){ int a, b, c; cin >> a >> b >> c; Equation tmp(a, b, c); tmp.solve()(481点数解答 | 2025-03-07 12:49:26)140
- [讯飞星火] 快速排序:# include< iostream>using namespace std;# include< stdlib. h># include< time. h>const int n =10;const int min= 10;const int max=99;int getrand( int min, int max){return ( rand()%( max-min+1)+ min);}void set( int r[], int n){int i;for(i=0;i<n;i++)r[i]=getrand(min, max);}void show( int r[], int n){int i;for(i=0;i<n;i++)cout << r[i]<<" ";cout << endl;}int partition( int r[], int i, int j){//由你完成}void quicksort( int r[], int i, int j){//由你完成}int main(){srand( time(0));int a[n];set(a, n);cout<<"(710点数解答 | 2024-11-14 10:44:25)275
- [阿里通义] 快速排序:# include< iostream>using namespace std;# include< stdlib. h># include< time. h>const int n =10;const int min= 10;const int max=99;int getrand( int min, int max){return ( rand()%( max-min+1)+ min);}void set( int r[], int n){int i;for(i=0;i<n;i++)r[i]=getrand(min, max);}void show( int r[], int n){int i;for(i=0;i<n;i++)cout << r[i]<<" ";cout << endl;}int partition( int r[], int i, int j){//由你完成}void quicksort( int r[], int i, int j){//由你完成}int main(){srand( time(0));int a[n];set(a, n);cout<<"(951点数解答 | 2024-11-14 10:46:29)260
- [字节豆包] 快速排序:# include< iostream>using namespace std;# include< stdlib. h># include< time. h>const int n =10;const int min= 10;const int max=99;int getrand( int min, int max){return ( rand()%( max-min+1)+ min);}void set( int r[], int n){int i;for(i=0;i<n;i++)r[i]=getrand(min, max);}void show( int r[], int n){int i;for(i=0;i<n;i++)cout << r[i]<<" ";cout << endl;}int partition( int r[], int i, int j){//由你完成}void quicksort( int r[], int i, int j){//由你完成}int main(){srand( time(0));int a[n];set(a, n);cout<<"(472点数解答 | 2024-11-14 10:51:44)249
- [字节豆包] #include<iostream> #include<cmath> using namespace std; int f[2] = {11, 21}; int a[25 * 2500 + 10], n = 0; int main(){ char tmp; while(1){ cin >> tmp; if(tmp == 'E') break; else if(tmp == 'W') a[n++] = 1; else if(tmp == 'L') a[n++] = 0; } for(int k = 0; k < 2; k++){ int w = 0, l=0; for(int i = 0; i < n; i++){ w += a[i]; l += l - a[i]; if((max(w, l) >= f[k]) && abs(w - 1) >= 2) { cout << w << ": " << l << endl; } } cout << w << ": " << l << endl; cout << endl;(868点数解答 | 2025-12-02 18:30:13)6
- [字节豆包] #include<iostream> #include<cmath> using namespace std; int f[2] = {11, 21}; int a[25 * 2500 + 10], n = 0; int main(){ char tmp; while(1){ cin >> tmp; if(tmp == 'E') break; else if(tmp == 'W') a[n++] = 1; else if(tmp == 'L') a[n++] = 0; } for(int k = 0; k < 2; k++){ int w = 0, l=0; for(int i = 0; i < n; i++){ w += a[i]; l += 1 - a[i]; if((max(w, l) >= f[k]) && abs(w - l) >= 2) { cout << w << ":" << l << endl; w = 0; l = 0; } } cout << w << ":" << l << e(905点数解答 | 2025-12-02 18:33:13)5
- [字节豆包] #include<iostream> #include<cmath> using namespace std; int f[2] = {11, 21}; int a[25 * 2500 + 10], n = 0; int main(){ char tmp; while(1){ cin >> tmp; if(tmp == 'E') break; else if(tmp == 'W') a[n++] = 1; else if(tmp == 'L') a[n++] = 0; } for(int k = 0; k < 2; k++){ int w = 0, l=0; for(int i = 0; i < n; i++){ w += a[i]; l += 1 - a[i]; if((max(w, l) >= f[k]) && abs(w - l) >= 2) { cout << w << ":" << l << endl; w = 0; l = 0; } } cout << w << ":" << l << e(926点数解答 | 2025-12-02 18:44:32)6
- [字节豆包] #include<iostream> using namespace std;#include<stdlib.h>#include<time.h> const int n =10; const int min =10; const int max =99; int getrand(int min, int max) { return (rand() % (max - min +1) + min); void set(int r[], int n){ int i; for(i = 0; i< n; i++) r[i] = getrand(min,max); void show(int r[], int n) { int i; for (i = 0; i<n;i++) cout << r[i] << ""; cout << endl;}void sift(int r[],int m,int k){ } void heapsort(intr[], int n){ //由你完成 int main(){ srand(time(0)); int a[n]; set(a,n); cout <<"原序(417点数解答 | 2024-11-28 11:20:23)312
- [DeepSeek] using communication_protocols; using FTS_F02391A; using NLog; using ScottPlot.Plottable; using Sunny.UI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体界面设计 { public partial class FormHome : UIPage { private readonly DataLogger Log1;//波形图数据记录器 bool bViewSlide = false(562点数解答 | 2025-07-09 16:57:56)134
- [字节豆包] #include<iostream> using namespace std;#include<stdlib.h>#include<time.h> const int n =10; const int min =10; const int max =99; int getrand(int min, int max) { return (rand() % (max - min +1) + min); void set(int r[], int n){ int i; for(i = 0; i< n; i++) r[i] = getrand(min,max); void show(int r[], int n) { int i; for (i = 0; i<n;i++) cout << r[i] << ""; cout << endl; void heapsort(intr[], int n){ //由你完成 int main(){ srand(time(0)); int a[n]; set(a,n); cout <<"原序列:"; show(a,n); cout <<"新序列:\n"; h(684点数解答 | 2024-11-28 11:21:15)259
- [字节豆包] #include<iostream> using namespace std; class shizheng { private: int speed; static int shitime; public: shizheng() { speed = 0; shitime = 0; } shizheng(int newspeed, int newshitime) { speed = newspeed; shitime = newshitime; } void shizhengmove() { cout << "shizheng moved" << endl; } void setshizheng() { speed = 0; shitime = 0; } int showshitime() { return shitime+speed; } }; class fenzheng { private: int speed; int fentime; public: fenzheng() { speed = 0; fentime = 0; } fenzheng(int(493点数解答 | 2024-11-11 23:52:44)154
- [字节豆包] /新系统汽车发出声音phonate();灯光闪烁twink1e(); //汽车控制类,充当目标抽象类 public abstract class carcontroller{ public void move(){ system.out.println("玩具汽车移动!"); public abstract void phonate();//发出声音 public abstract void twinkle();//灯光闪烁 //警车类,充当适配者 public class police { public void alarmlamp(){ system.out.printin("呈现警灯闪烁!"); public void alarmsound(){ system.out.println( ("发出警笛声音!"); 】 ~ //警车适配器,充当适配器 extends public class policecaradapter carcontroller private police po; //定义适配者policelamp对象 public policecaradapter(){(32点数解答 | 2024-10-28 17:22:39)215