洛谷:你以为你用二分查找和快速排序我就会让你过???

P1478 陶陶摘苹果(升级版)

himan @ 2019-12-06 20:05:31

#include <stdio.h>
int partition(int* a, int* b, int p, int r);
int quick_sort(int* a, int* b, int p, int r);
int two(int* a, int p, int r, int value);
int main() {
    int n = 0; 
    int s = 0;
    int a = 0;
    int b = 0;
    int height[5000];
    int strengh[5000];
    int num = 0;
    int* null = NULL;
    scanf("%d %d", &n, &s);
    scanf("%d %d",&a,&b);
    for (int i = 0; i < n; i++) {
        scanf("%d %d",&height[i],&strengh[i]);
    }
    printf("input\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", height[i]);
        printf("%d", strengh[i]);
        printf("\n");
    }
    int totalheight = a + b;
    int canreach = 0;
    quick_sort(height,strengh,0,n-1);//注意 这里的n是个数,要进行索引转化
    printf("this is height\n");
    for (int i = 0; i < n;i++) {
        printf("%d ",height[i]);
        printf("%d", strengh[i]);
        printf("\n");
    }

    canreach = two(height,0,n,totalheight);
    quick_sort(strengh,null, 0, canreach);
    printf("this is strengh\n");
    for (int i = 0; i < canreach; i++) {

        printf("%d", strengh[i]);
        printf("\n");
    }
    for (int i = 0; i < canreach;i++) {

        s -= strengh[i]; 
            if(s>=0){
            num++;
        }
    }
    printf("%d",num);
    printf("\n");
    printf("%d", s);

}
//要搜索摘取苹果的最大数量
/*对苹果进行排序,首先筛选出高度符合(先高度排序,然后用二分法查找),需要力气小的排在前面,排在前面的摘,减力气然后判断
是否还够摘下一个*/

int quick_sort(int *a,int *b,int p, int r) {
    int q = 0;

    if (p >= r) { return 0; }
    q = partition(a,b, p, r);
    quick_sort(a,b,p,q-1);
    quick_sort(a,b,q+1,r);

}

int partition(int* a, int* b, int p, int r) {
    int q = 0;
    q = p;
    int  temp = 0;
    int temp2 = 0;
    for (int u = p; u < r; u++) {
        if (a[u] >= a[r]) {
            if (b != NULL) {
                temp2 = b[u];
                b[u] = b[r];
                b[r] = temp2;
            }
            temp = a[u];

            a[u] = a[r];

            a[r] = temp;

        }
        q++;
    }
    temp = a[q];

    a[q] = a[r];

    a[r] = temp;

    if (b != NULL) {
        temp2 = b[q];
        b[q] = b[r];
        b[r] = temp2;
    }

    return q;

}

int two(int* a, int p, int r, int value) {
    int t = (p + r) / 2;
    if (p < r) {
        if (a[t] > value) {
            two(a, p, t - 1,value);
        }
        if (a[t] < value) {
            two(a, t + 1, r,value);
        }
        if (a[t] == value) {
            return t;
        }
    }
}

代码五分钟,修bug 5小时!


by Victorique_De_Blois @ 2019-12-06 20:07:17

多余的提示语句会判错。


by tiger0133 @ 2019-12-06 20:08:05

不要多余的提示语句,直接输入输出就行了


by himan @ 2019-12-06 20:08:31

@Victorique_De_Blois 提示我删了,还是不能过 :3


by himan @ 2019-12-06 20:09:07

@tiger0133 删除提示还是过不了呢:z


|