OpenJudge

求问该题在自己编译器按样例输入得到样例输出,但提交显示答案错误

  • TheGreatYan
    TheGreatYan 18.1.28 回复

    #include <stdio.h>
    int main()
    {
    int n, W;
    int i, j, k,sumw = 0, count[100], w[100];
    float v_perw[100], V = 0, Vmax = 0, t, v[100];
    scanf("%d %d", &n, &W);
    for (i = 0; i < n; i++)
    {
    scanf("%f %d", &v[i], &w[i]);
    v_perw[i] = v[i] / w[i];
    count[i] = i;
    }
    for (j = 0; j < n; j++)
    for (i = 0; i < n - j; i++)
    if (v_perw[i] < v_perw[i + 1])
    {
    t = v_perw[i];
    v_perw[i] = v_perw[i + 1];
    v_perw[i + 1] = t;
    k = count[i];
    count[i] = count[i + 1];
    count[i + 1] = k;
    }
    for (k = 0; sumw <= W; k++)
    {
    sumw = sumw + w[count[k]];
    }
    sumw = sumw - w[count[k - 1]];
    for (i = 0; i < k - 1; i++)
    V = V + v[count[i]];
    Vmax = V + v_perw[i] * (W - sumw);
    printf("%.1f\n", Vmax);
    return 0;
    }

  • TheGreatYan
    TheGreatYan 18.1.28 回复

    #include <stdio.h>
    int main()
    {
    int n, W;
    int i, j, k,sumw = 0, count[100], w[100];
    float v_perw[100], V = 0, Vmax = 0, t, v[100];
    scanf("%d %d", &n, &W);
    for (i = 0; i < n; i++)
    {
    scanf("%f %d", &v[i], &w[i]);
    v_perw[i] = v[i] / w[i];
    count[i] = i;
    }
    if(n==1)
    {
    if(w[0]<=W)
    Vmax=v[0];
    else
    Vmax=v_perw[0]*W;
    }
    else
    {
    for (j = 0; j < n-1; j++)
    for (i = 0; i < n -1- j; i++)
    if (v_perw[i] < v_perw[i + 1])
    {
    t = v_perw[i];
    v_perw[i] = v_perw[i + 1];
    v_perw[i + 1] = t;
    k = count[i];
    count[i] = count[i + 1];
    count[i + 1] = k;
    }
    for (k = 0; sumw <= W; k++)
    {
    sumw = sumw + w[count[k]];
    }
    sumw = sumw - w[count[k - 1]];
    for (i = 0; i < k - 1; i++)
    V = V + v[count[i]];
    Vmax = V + v_perw[i] * (W - sumw);
    }
    printf("%.1f\n", Vmax);
    return 0;
    }
    这个还是错的,求助

想要评论吗?

注册OpenJudge账号,如果您已经注册,请先登入