Return to Snippet

Revision: 12694
at March 24, 2009 23:29 by soulmachine


Initial Code
/**
 * @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;

    bitmap = (int*)malloc(max_norm * sizeof(int));
    if(NULL == bitmap)
    {
        result = -1;
        goto malloc_failed;
    }

    for(norm = count; norm <= max_norm; ++norm)
    {
        int i = 0;
        memset(bitmap, 0, norm * sizeof(int));
        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;
}

Initial URL
http://www.yanjiuyanjiu.com

Initial Description
/**
 * @brief 给定n个整数,求一个最小的数,使得它们除以这个数的余数不重复
 * @author soulmachine
 * @param[in] numbers 整数数组
 * @param[in] count 整数个数
 * @param[in] max_norm 模的最大值
 * @return 成功返回大于0的最小模,失败返回0或-1
 * @note æ— 
 * @remarks æ— 
 */

Initial Title
给定n个整数,求一个最小的数,使得它们除以这个数的余数不重复

Initial Tags
c

Initial Language
C