1.三元一次方程
需要注意的地方:
只有系数不为0时才输出;系数为1时不输出系数;系数为-1时仅输出负号;若出现大于0的系数,当其不是第一次出现时,需要额外输出‘+’。
因此采用标记变量flag来判断该系数是否为第一个输出。
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
bool flag = false;
if (a != 0)
{
if (a == 1) cout << 'x';
else
{
if (a > 0) cout << a << 'x';
else if (a == -1) cout << "-x";
else cout << a << 'x';
}
flag = true;
}
if (b != 0)
{
if (b == 1)
{
if (flag) cout << '+';
cout << 'y';
}
else
{
if (b > 0)
{
if (flag) cout << '+';
cout << b << 'y';
}
else if (b == -1) cout << "-y";
else cout << b << 'y';
}
flag = true;
}
if (c != 0)
{
if (c == 1)
{
if (flag) cout << '+';
cout << 'z';
}
else
{
if (c > 0)
{
if (flag) cout << '+';
cout << c << 'z';
}
else if (c == -1) cout << "-z";
else cout << c << 'z';
}
flag = true;
}
cout << '=' << d;
return 0;
}
2.四则运算的最大值
计 算 除 法 时 记 得 将 a 转 化 为 d o u b l e 类 型 。 计算除法时记得将a转化为 double类型。 计算除法时记得将a转化为double类型。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
double res1, res2, res3, res4;
res1 = a + b;
res2 = a - b;
res3 = a * b;
res4 = a * 1.0 / b;
cout << max(res1, max(res2, max(res3, res4)));
return 0;
}
3.声音的频率
我对于该题的做法是依次读入 5 5 5个数,若符合条件,则放入 v e c t o r vector vector数组中。若最后数组为空,则直接输出 − 1 -1 −1;否则进行排序后再输出最大最小值。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> a;
int main()
{
int m;
for (int i = 0; i < 5; i++)
{
cin >> m;
if (m >= 20 && m <= 20000)
{
a.push_back(m);
}
}
if (a.empty()) cout << -1;
else
{
sort(a.begin(), a.end());
cout << a[0] << ' ' << a[a.size() - 1];
}
return 0;
}
4.厦大百年校庆
注意点:
1.闰年的判断:能被4整除的大多是闰年,但能被
100
100
100整除而不能被
400
400
400整除的年份不是闰年,能被
3200
3200
3200整除的也不是闰年。
2.天数的计算:
分情况讨论:
①输入年份
<
2021
<2021
<2021,先计算该月份从这天开始到月底的天数,再计算从下一月份开始到该年年底的天数(记得先判断是否为闰年:如是,则需要更改
2
2
2月份的天数为
29
29
29天),接着进行针对年份循环到
2020
2020
2020年:若为闰年,则计数器
c
n
t
cnt
cnt 加上
366
366
366,否则加上
365
365
365。由计算可得
2021
2021
2021年从
1.1
1.1
1.1到
4.6
4.6
4.6的总天数为
96
96
96,因此最后加上
96
96
96即得到最终答案。
②输入年份
=
2021
=2021
=2021:若月份
<
=
4
<=4
<=4,则需要先计算该月份从这天开始到月底的天数,再计算从下一月份开始到3月底的天数,最后加上6即可,若月份
=
4
=4
=4直接用
6
6
6减去天数即可。
#include<iostream>
using namespace std;
//判断是否为闰年
bool is_leapyear(int m)
{
if (m % 4 == 0)
{
if (m % 100 == 0 && m % 400 != 0 || m % 3200 == 0) return false;
return true;
}
return false;
}
int main()
{
int month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int a, b, c, cnt = 0;//表示a年b月c日
cin >> a >> b >> c;
if (a == 2021)
{
if (b == 4) cnt += 6 - c;
else
{
cnt += month[b] - c;
for (int j = b + 1; j < 4; j++) cnt += month[j];
cnt += 6;
}
}
//a < 2021
else
{
if (is_leapyear(a)) month[2] = 29;
cnt += month[b] - c;
for (int i = b + 1; i <= 12; i++) cnt += month[i];
for (int i = a + 1; i < 2021; i++)
{
//三目运算符:如果判断为真则执行冒号前半句,为假则执行后半句
is_leapyear(i) ? cnt += 366 : cnt += 365;
}
//2021年从1.1到4.6的总天数为96
cnt += 96;
}
cout << cnt;
return 0;
}
5.求面积
有三种情况会导致直线与坐标轴无法形成三角形:
①直线与x轴平行;即:a = 0
②直线与y轴平行;即:b = 0
③直线正好过原点。 即:c = 0
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b >> c;
if (a == 0 || b == 0 || c == 0) cout << -1;
else cout << 0.5 * abs(c / a) * abs(c / b);
return 0;
}
6.最近的坐标点
定义一个Point类的结构体,把输入的三个点的横纵坐标保存到Point类的数组中,计算每一个点距离原点的距离并保存。可以重载运算符,也可以先定义一个最小值,后续通过比较不断更新答案。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point
{
double x, y, dist;
void get_dist(double x, double y)
{
dist = sqrt(x * x + y * y);
}
bool operator < (const Point& p)const
{
return p.dist < dist;
}
}p[3];
int main()
{
for (int i = 0; i < 3; i++)
{
cin >> p[i].x >> p[i].y;
p[i].get_dist(p[i].x, p[i].y);
}
double min = 2e9;
int res = 1;
for (int i = 0; i < 3; i++)
{
if (p[i].dist < min)
{
min = p[i].dist;
res = i;
}
}
cout << '(' << p[res].x << ',' << p[res].y << ')';
return 0;
}
运算符重载:
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point
{
double x, y, dist;
void get_dist(double x, double y)
{
dist = sqrt(x * x + y * y);
}
bool operator < (const Point& p)const
{
return p.dist < dist;
}
}p[3];
int main()
{
for (int i = 0; i < 3; i++)
{
cin >> p[i].x >> p[i].y;
p[i].get_dist(p[i].x, p[i].y);
}
sort(p, p + 3);
cout << '(' << p[2].x << ',' << p[2].y << ')';
return 0;
}