除数博弈

题源:leetcode

链接:https://leetcode-cn.com/problems/divisor-game/

 

 

 

一开始就想到了数学方法,即return (n%2==0)

这里还有个动态规划的方法:

 1 class Solution {
 2 public:
 3     bool divisorGame(int n) {
 4         vector<int> f(n + 5, false);
 5 
 6         f[1] = false;
 7         f[2] = true;
 8         for (int i = 3; i <= n; ++i) {
 9             for (int j = 1; j < i; ++j) {
10                 if (i % j == 0 && !f[i - j]) {
11                     f[i] = true;
12                     break;
13                 }
14             }
15         }
16 
17         return f[n];
18     }
19 };

 

posted @ 2021-07-30 11:03  Danae丶  阅读(36)  评论(0编辑  收藏  举报