求助,给的测试案例都可以过,但还是有一个WA

AT_code_thanks_festival_2015_c 集合写真

axpzdqds @ 2024-10-31 12:04:48

#include<stdio.h>

#include<iostream>

const int N = 50;

int main()

{

  int n;

  scanf("%d", &n);

  int q[N];

  for (int i = 1; i <=n; i++)scanf("%d", &q[i]);

   int x;

   scanf("%d", &x);

   int l = 1, r = n;

    while (l < r)
    {
        int mid = l + r >> 1;

        if (q[mid] > x)r = mid;

        else l = mid + 1;
    }
    if (r == n)r += 1;

    printf("%d\n", r);

    return 0;
}

by c22j33c43 @ 2024-10-31 12:23:04

反例

4

1 5 9 19

10

你的输出 5


by c22j33c43 @ 2024-10-31 12:31:14

你n的特判不对,这样就忽略了排在n的情况


by c22j33c43 @ 2024-10-31 12:36:54

AC代码(还有数组开小了,会RE,我开大了点)

#include<stdio.h>

#include<iostream>

const int N = 53;

int main()

{

  int n;

  scanf("%d", &n);

  int q[N];

  for (int i = 1; i <=n; i++)scanf("%d", &q[i]);

   int x;

   scanf("%d", &x);

   int l = 1, r = n;

    while (l < r)
    {
        int mid = l + r >> 1;

        if (q[mid] > x)r = mid;

        else l = mid + 1;
    }
    if (x > q[n]) r += 1;

    printf("%d\n", r);

    return 0;
}

|