vijos P1317 开心的金明 C语言 源码(动态规划)

vijos P1317 开心的金明 C语言 源码(动态规划)

//测试数据 #0: Accepted, time = 0 ms, mem = 476 KiB, score = 10

测试数据 #1: Accepted, time = 0 ms, mem = 476 KiB, score = 10

测试数据 #2: Accepted, time = 0 ms, mem = 472 KiB, score = 10

测试数据 #3: Accepted, time = 15 ms, mem = 476 KiB, score = 10

测试数据 #4: Accepted, time = 15 ms, mem = 476 KiB, score = 10

测试数据 #5: Accepted, time = 0 ms, mem = 472 KiB, score = 10

测试数据 #6: Accepted, time = 15 ms, mem = 472 KiB, score = 10

测试数据 #7: Accepted, time = 62 ms, mem = 472 KiB, score = 10

测试数据 #8: Accepted, time = 125 ms, mem = 476 KiB, score = 10

测试数据 #9: Accepted, time = 0 ms, mem = 476 KiB, score = 10//

   #include 
    int N = 0, m = 0;
    int v[25], p[25];

    int max(int a,int b)
    {
    return (a > b) ? a : b;
    }
    int f(int c, int d)
    {
        int w;
        if(d == 0)
        {
            if(c >= v[d])
            {
                w = v[d] * p[d];
            }
            else
            {
                w  = 0;
            }
        }
        else if(d!=0)
        {
            if(c >= v[d])
            {
                w = max(f(c - v[d], d - 1) + v[d] * p[d], f(c, d - 1));
            }
            else
            {
                w = f(c, d - 1);
            }

        }

        return w;
    }
    int main()
    {
        scanf("%d %d",&N, &m);
        int i = 0;
        for (i = 0; i < m; i++)
        {
            scanf("%d %d", &v[i], &p[i]);
        }
        printf("%d", f(N, m));
        return 0;
    }


Accepted
, time = 232 ms, mem = 476 KiB, score = 100//

你可能感兴趣的:(算法学习)