题解 | 游游的数值距离
游游的数值距离
https://www.nowcoder.com/practice/a3688c3615d144e4ba79357034850e3c
#include <bits/stdc++.h> using namespace std; #define int long long int f[] = {0, 1, 0, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200}; //打表阶乘以及初始化 double min_x = 0, min_y = 1e9; int minx = 1e9; bool Process(double x, int i, int j)//找最小数 { if (x == floor(x)) { if (j < minx) { minx = j; if (x < min_y) { min_x = i; min_y = x; return true; } } } return false; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); double n; cin >> n; if(n == 2)//特判2,除了2没有别的数会输出 1 1 { cout << 1 << " " << 1 << endl; return 0; } //小学生解方程法好了 //假如|x! * y - y - n| = ?,当除了y都是已知的话直接解方程求y就可以了 for (int i = 3; i <= 10; i++) { for (int j = 0; j <= 10; j++)//枚举答案的可能 { double x = 0; double x2 = 0; x = double(j + n) / double(f[i] - 1); x2 = double(-j + n) / double(f[i] - 1); if (x == 2 || x2 == 2) continue; Process(x, i, j); Process(x2, i, j); } } cout.precision(0); cout << fixed << min_x << " " << fixed << min_y << endl; return 0; } //先暴力打表看看数据规律即可,自己造数据参悟 // #include <bits/stdc++.h> // using namespace std; // #define int long long // int factorial(int n) // { // if(n==1) return 1; // else return n*factorial(n-1); // } // signed main() // { // ios::sync_with_stdio(false); // cin.tie(0);cout.tie(0); // int n;cin >> n; // int minx = 1e9; // int min_x,min_y; // for(int x=1;x<n;x++) // { // for(int y=1;y<n;y++) // { // if(x == 2 || y == 2)continue; // int tmp = abs(factorial(x)*y - y -n); // if(tmp < minx) // { // minx = tmp; // min_x = x; // min_y = y; // } // } // } // cout << min_x << " " << min_y << endl; // return 0; // }