The answer in ALDS1_1_A of aizu online judge is correct, but it becomes WA.

Asked 1 years ago, Updated 1 years ago, 342 views

I wrote the following in C language.
As long as I run it on hand, it's right, but it's going to be WA...
Is there anything wrong?
There is no difference when I try to differentiate the results by dropping the answers from others and redirecting them to a file locally.

#include<stdio.h>

void trace(intr[], intN){
    for(inti=0;i<N;i++){
      printf("%d", r[i]);
      if(i!=N-1){
        printf("";
      }
    }
    printf("\n");
}
int main() {
    
  intr[100];

  int N;
  scanf("%d", & N);
    for(inti=0;i<N;i++){
      scanf("%d", &r[i]);
    }

    trace(r,N);
    int j, i, v; 
    for(i=1;i<N;i++){
      v=r[i];
      j = i-1;

      while(v>=0&r[j]>v){
        r[j+1] = r[j];
        j--;
      }
      r[j+1] = v;
      trace(r,N);
    }
    
    return 0;
}

c

2022-09-30 22:02

1 Answers

while(v>=0&r[j]>v){
  r[j+1] = r[j];
  j--;
}

There are no restrictions on the j value.Therefore, it appears to be accessing beyond the r range.Conversely, the value of v does not change, so it should not be meaningful to evaluate v>=0 per loop.


2022-09-30 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.