/ Published in: C
/**
* @brief 给定n个整数,求一个最å°çš„数,使得它们除以这个数的余数ä¸é‡å¤
* @author soulmachine
* @param[in] numbers 整数数组
* @param[in] count 整数个数
* @param[in] max_norm 模的最大值
* @return æˆåŠŸè¿”回大于0的最å°æ¨¡ï¼Œå¤±è´¥è¿”回0或-1
* @note æ—
* @remarks æ—
*/
* @brief 给定n个整数,求一个最å°çš„数,使得它们除以这个数的余数ä¸é‡å¤
* @author soulmachine
* @param[in] numbers 整数数组
* @param[in] count 整数个数
* @param[in] max_norm 模的最大值
* @return æˆåŠŸè¿”回大于0的最å°æ¨¡ï¼Œå¤±è´¥è¿”回0或-1
* @note æ—
* @remarks æ—
*/
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * @brief 给定n个整数,求一个最å°çš„数,使得它们除以这个数的余数ä¸é‡å¤ * @author soulmachine * @param[in] numbers 整数数组 * @param[in] count 整数个数 * @param[in] max_norm 模的最大值 * @return æˆåŠŸè¿”回大于0的最å°æ¨¡ï¼Œå¤±è´¥è¿”回0或-1 * @note æ— * @remarks æ— */ int decide_norm(int *numbers, int count, int max_norm) { int result = 0; int *bitmap = NULL; /* å˜æ”¾ç¬¬ä¸€æ¬¡å‘½ä¸çš„æ•° */ int norm = 0; if(NULL == bitmap) { result = -1; goto malloc_failed; } for(norm = count; norm <= max_norm; ++norm) { int i = 0; for(i = 0;i < count; i++) { int remainder = numbers[i] % norm; if(0 == bitmap[remainder]) { bitmap[remainder] = numbers[i]; } else { debug_printf("conflicted: %d %% %d = %d %% %d = %d\n", bitmap[remainder], norm, numbers[i], norm, remainder); break; } } if(count == i) /* 这个模下,å„ä¸ªæ•°å€¼æ²¡æœ‰å†²çª */ { result = norm; break; } } malloc_failed: return result; }
URL: http://www.yanjiuyanjiu.com