-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreakInteger.cpp
More file actions
39 lines (38 loc) · 842 Bytes
/
BreakInteger.cpp
File metadata and controls
39 lines (38 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include "math.h"
using namespace std;
class Solution {
public:
int integerBreak2(int n){
if(n==1) return 1;
if(n == 2) return 2;
if(n==3) return 3;
if(n==4) return 4;
int result=1;
while(n-3>=2){
n-=3;
result *=3;
}
result *=n;
return result;
}
int integerBreak(int n) {
if(n==1) return 1;
int max =0;
for(int i=2;i<=n;i++){
int average = n /i;
int remain = n - average*i;
int result =1;
result= pow(average, i-remain);
result *= pow(average+1,remain);
if(result > max){
max = result ;
}
}
return max;
}
};
int main(){
Solution s ;
cout<<s.integerBreak2(7);
}