标签:qsort border 数值 table sizeof rgba 如何 -- turn
nums | 2 | 3 | 5 | 25 | 30 | 50 |
dp | 1 | 1 | 1 | 2 | 2 | 3 |
int cmp(const void *a, const void *b) { return *(int*)a - *(int*)b; } int* largestDivisibleSubset(int* nums, int numsSize, int* returnSize){ qsort(nums, numsSize, sizeof(nums[0]), cmp); //排序 int i, j, dp[1005], max = -999999; int t, x;//x 记录nums[i]的数值;用于判断是否整除for(i = 0; i < numsSize; i++){ dp[i] = 1; for(j = i - 1; j >= 0; j--){ if(nums[i] % nums[j] == 0){ dp[i] = fmax(dp[i], dp[j] + 1); } } if(max < dp[i]){ max = dp[i]; x = nums[i]; } } int *res = malloc(sizeof(int) * max); t = max; if(max == 1){//小优化; res[0] = nums[0]; *returnSize = max; return res; } for(i = numsSize - 1; i >= 0; i--){ if(dp[i] == t && x % nums[i] == 0){ res[--t] = nums[i]; x = nums[i]; } } *returnSize = max; return res; }
标签:qsort border 数值 table sizeof rgba 如何 -- turn
原文地址:https://www.cnblogs.com/rongrongrong/p/14695468.html