Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
前N个自然数的3到9次方打表,也就700多个int,再2分查找感觉会效率高一些可惜测试数据不给力,体现不出来。 #include <stdio.h> #include <math.h> #define UB 1000000 int mp[7][110]; // mp[X][0]作为此行有效数据个数 int bfind(int*p, int n) { int l, r, m; l = 1; r = p[0]; while (l <= r) { m = (l + r) / 2; if (p[m] == n) return m; else if (n > p[m]) { l = m + 1; if (l > p[0]) return m; if(n < p[l]) { if(p[l] + p[m] > n*2) return m; else return l; } } else { r = m -1; if(0==r) return m; if(n > p[r]) { if(n*2 > p[m] + p[r]) return m; else return r; } } } return 0; } int main() { int i,j,k,a,b,n; int *p = mp[0],*q; // 先构造3次方的初始行 for (i = 1; i < 101; i++) p[i] = i * i * i; p[0] = 100; // 表的后续行由之前行构造 for (j = 1; j < 7; j++) { p = mp[j]; q = mp[j - 1]; for (i = 1; i < 33; i++) { k = q[i] * i; // 只计算到大于100w就够了,p[0]记录数组里数据个数 if (k > UB){ p[0] = i ; p[i]=k;break; } p[i] = k; } } while (1){ scanf("%d %d", &b, &n); if (0 == b && 0 == n)break; if (1 == n || 1 == b)a = b; // 2次用开方计算,2次以上再查表 else if (2 == n) { double r = sqrt((double)b); i = r; k = i * i; a = k + 2 * i + 1; if (2*b < a + k) a = i; else a = i + 1; } else { p = mp[n - 3]; a = bfind(p, b); } printf("%d\n", a); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator