leetcode650 2 Keys Keyboard

思路:

把给定的数分解质因子之后,对于每一个质因子x,都需要x次操作(一次copy all操作和x-1次paste),所以答案就是对分解后的所有质因子求和。

实现:

 1 class Solution
 2 {
 3 public:
 4     int minSteps(int n)
 5     {
 6         int sum = 0;
 7         for (int i = 2; i <= n; i++)
 8         {
 9             while (n % i == 0)
10             {
11                 sum += i;
12                 n /= i;
13             }
14         }
15         return sum;
16     }
17 };
原文地址:https://www.cnblogs.com/wangyiming/p/7444539.html