C4-1 最大公约数
题目描述
求两个正整数a 和 b的最大公约数。
要求使用c++ class编写程序。
#include <iostream>
using namespace std;
int getGcd(int a, int b) {
if (a == b)
return a;
else if (a > b)
return getGcd(a - b, b);
else
return getGcd(b - a, a);
}
class Integer {
private:
int _num;
public:
//构造函数
Integer(int num);
//计算当前Integer和b之间的最大公约数
int gcd(Integer b);
};
Integer::Integer(int num) :_num(num) {};
int Integer::gcd(Integer b) {
return getGcd(_num, b._num);
}
int main() {
int a, b;
cin >> a >> b;
Integer A(a);
Integer B(b);
cout << A.gcd(B) << endl;
return 0;
}
C4-2 反转整数
题目描述
对于输入的一个正整数,输出其反转形式
要求使用c++ class编写程序。
#include <iostream>
using namespace std;
class Integer {
private:
int _num;
public:
//Integer类构造函数
Integer(int num);
//反转_num
int inversed();
};
Integer::Integer(int num) :_num(num) {};
int Integer::inversed() {
int x = 0;
while (_num > 0) {
x = x * 10 + _num % 10;
_num /= 10;
}
return x;
}
int main() {
int n;
cin >> n;
Integer integer(n);
cout << integer.inversed() << endl;
return 0;
}
C4-3 一元二次方程求解
题目描述
对于一元二次方程ax^2 + bx + c = 0,解可以分为很多情况。
若该方程有两个不相等实根,首先输出1,换行,然后从小到大输出两个实根,换行;
若该方程有两个相等实根,首先输出2,换行,然后输出这个这个实根,换行;
若该方程有一对共轭复根,输出3,换行;
若该方程有无解,输出4,换行;
若该方程有无穷个解,输出5,换行;
若该方程只有一个根,首先输出6,换行,然后输出这个跟,换行;
要求使用c++ class编写程序。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Equation {
private:
int _a, _b, _c;
public:
Equation(int a, int b, int c);
void solve();
};
Equation::Equation(int a, int b, int c) :_a(a), _b(b), _c(c) {}
void Equation::solve() {
double answer;
if (_a == 0 and _b == 0) {
if (_c == 0)
cout << 5 << endl;
else
cout << 4 << endl;
return;
}
else if (_a == 0) {
cout << 6 << endl;
answer = -_c / (double)_b;
cout << setiosflags(ios::fixed);
cout << setprecision(2) << answer << endl;
return;
}
double x1, x2, d;
d = _b * _b - 4 * _a * _c;
if (d < 0 and _a != 0)
cout << 3 << endl;
else if (d == 0 and _a != 0) {
cout << 2 << endl;
answer = -_b / 2.0 / (double)_a;
cout << setiosflags(ios::fixed);
cout << setprecision(2) << answer << endl;
}
else {
cout << 1 << endl;
x1 = (-_b + sqrt(d)) / 2.0 / (double)_a;
x2 = (-_b - sqrt(d)) / 2.0 / (double)_a;
cout << setiosflags(ios::fixed);
if (x2 < x1)
cout << setprecision(2) << x2 << " " << setprecision(2) << x1 << endl;
else
cout << setprecision(2) << x1 << " " << setprecision(2) << x2 << endl;
}
}
int main() {
int a, b, c;
cin >> a >> b >> c;
Equation tmp(a, b, c);
tmp.solve();
return 0;
}