@Dmaxiya
2020-08-24T15:42:35.000000Z
字数 1544
阅读 1348
博文
在一般涉及到 double 类型变量计算的时候,精度损失是必然的。double 的有效数字只有 16 位,在 16 位之后的数字就乱七八糟了,再加上乘除、cmath 等头文件的计算,精度又要损失几位,用 cfloat 头文件里的 DBL_EPSILON 作为精度误差可能不太管事了。
所以我们可以自己设置一个精度常量 eps,然后写几个 inline 的比较函数,这里以 为例,要用的时候,用到哪个打哪个,最后再拿一题小练一下。
const double eps = 1e-6;inline bool zero(const double &x) {return fabs(x) < eps;}inline bool equal(const double &x, const double &y) {return fabs(x - y) < eps;}inline bool smaller(const double &x, const double &y) {return !equal(x, y) && x < y;}inline bool larger(const double &x, const double &y) {return !equal(x, y) && x > y;}
给一个数 ,将 表示为 2 个整数 的平方和 ,如果有多种表示,按照 的递增序输出。其中 。
从 0 到 跑,计算 的值,如果为整数,按大小放到 set<pair<int,int>> 里面,再依次输出。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst double eps = 1e-6;inline bool equal(const double &x, const double &y) {return fabs(x - y) < eps;}int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);int N;double sq;set<pair<int, int> > ans;cin >> N;sq = sqrt(N);for(int i = 0; i < sq; ++i) {double j = sqrt(N - i * i);if(equal(j, floor(j))) {int Min = min(i, (int)j);int Max = max(i, (int)j);ans.insert(make_pair(Min, Max));}}if(ans.size() == 0) {cout << "No Solution" << endl;} else {set<pair<int, int> >::iterator it;for(it = ans.begin(); it != ans.end(); ++it) {cout << it->first << " " << it->second << endl;}}return 0;}
