Browse Source

阶乘之和,贪心算法

webturing 4 years ago
parent
commit
91756da493
1 changed files with 30 additions and 0 deletions
  1. 30 0
      G.cpp

+ 30 - 0
G.cpp

@@ -0,0 +1,30 @@
+#include <iostream>
+
+using namespace std;
+using ll=long long;
+
+int main() {
+    int a[10] = {1};
+    for (int i = 1; i <= 9; i++) {
+        a[i] = i * a[i - 1];
+    }
+
+    int t;
+    cin >> t;
+    while (t--) {
+        int x;
+        cin >> x;
+        for (int i = 9; i >= 1; i--) {
+            if (x >= a[i]) {
+                x -= a[i];
+            }
+        }
+        if (x == 0) {
+            cout << "Yes" << endl;
+
+        } else {
+            cout << "No" << endl;
+        }
+    }
+    return 0;
+}