【leetcode】强整数
int cmp(const void* a, const void* b) { return *(int*)a - *(int*)b; } int* powerfulIntegers(int x, int y, int bound, int* returnSize){ if (bound<2) { *returnSize = 0; return NULL; } int* arr = (int*)calloc(bound,sizeof(int)); int i,j,xMax=1,yMax=1,n=0,m=1; if (x !=1) { for (i=1; i<=bound && pow(x,i)<=bound; i++); //找出x对应次方的最大范围 xMax = i; } if (y != 1) { for (i=1; i<=bound && pow(y,i)<=bound; i++); //找出y对应次方的最大范围 yMax=i; } for (i=0; i<xMax; i++) { for (j=0; j<yMax && pow(x,i) + pow(y,j) <= bound; j++) { arr[n++] = pow(x,i) + pow(y,j); } } qsort(arr,n,sizeof(int),cmp); for (i=1; i<n; i++) { if (arr[i] != arr[i-1]) arr[m++] = arr[i]; //去掉重复的数 } *returnSize = m; return arr; }