g********r 发帖数: 89 | 1 Returns the next integer a in the arithmetic sequence of integers where
* a = m^n, m > 1 n > 1, and m and n are both integers
* Thus, the first few outputs will be 4, 8, 9, 16, 25, 27, 32, 36, etc | s*******g 发帖数: 170 | 2 我的一个解法:
unsigned long calc(unsigned m, unsigned n) {
if(m < 2 || m < 2) return 0;
unsigned long ret = m;
for(unsigned i = 1; i < n; ++i)
ret *= m;
return ret;
}
unsigned findNext2(unsigned val) {
unsigned long ret = INT_MAX;
unsigned smallest = 0;
unsigned sum = 4;
while(smallest <= val) {
unsigned n = 2;
for(unsigned i = n; i <= sum-2; ++i) {
unsigned long result = calc(sum-i, i);
if(i == 2) smallest = result;
if(result > val) ret = min(ret, result);
if(result > val) break;
}
for(unsigned i = n; i <= sum-2; ++i) {
unsigned long result = calc(i, sum-i);
if(result > val) ret = min(ret, result);
if(result > val) break;
}
++sum;
}
return ret;
}
【在 g********r 的大作中提到】 : Returns the next integer a in the arithmetic sequence of integers where : * a = m^n, m > 1 n > 1, and m and n are both integers : * Thus, the first few outputs will be 4, 8, 9, 16, 25, 27, 32, 36, etc
| r****7 发帖数: 2282 | 3 应该用binary search来解吧
【在 s*******g 的大作中提到】 : 我的一个解法: : unsigned long calc(unsigned m, unsigned n) { : if(m < 2 || m < 2) return 0; : unsigned long ret = m; : for(unsigned i = 1; i < n; ++i) : ret *= m; : return ret; : } : unsigned findNext2(unsigned val) { : unsigned long ret = INT_MAX;
| z***b 发帖数: 127 | |
|