C 代码给出了错误的答案,但是 java 代码给出了关于 spoj 的正确答案

C code gives wrong answer but java code gives correct answer on spoj

我正在解决 SPOJ.It 上的以下问题是简单的插入排序算法。我的 java 代码有效,但 C 代码给出了错误的答案。 我做错了什么?

请大家帮忙,非常感谢......:)

link of problem statement

java代码

public class Main {

   public static void main(String[] args) throws NumberFormatException, IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int t = Integer.parseInt(br.readLine());
      while (t > 0) {
         int n = Integer.parseInt(br.readLine());
         String str = br.readLine();
         String arr[] = str.split(" ");
         int inputArr[] = new int[n];
         for (int i = 0; i < n; i++) {
            inputArr[i] = Integer.parseInt(arr[i]);
         }
         int key = 0;
         int count = 0;
         for( int i = 1; i < n; i++ ) {
            key = inputArr[i];
            int j = i - 1;
            while (j >= 0 && inputArr[j] > key) {
               inputArr[j + 1] = inputArr[j];
               j = j - 1;
               count++;
            }
            inputArr[j + 1] = key;
         }
         System.out.println(count);
         t--;
      }
   }
}

C代码

#include<stdio.h>
int main() {
   int t=0;
   scanf("%d",&t);
   while( t > 0 ) {
      int n=0;
      scanf("%d",&n);
      int arr[n];
      int key=0;
      for(int i=0; i<n; i++) {
         scanf("%d",&arr[i]);
      }
      int count=0;
      int j=0;
      for(int i=1; i<n; i++) {
         key = arr[i];
         j   = i - 1;
         while(j>=0&&arr[j]>key) {
            arr[j+1]=arr[j];
            count++;
            j = j-1;
         }
         arr[j+1]=key;
      }
      printf("%d",count);
      t--;
   }
   return 0;
}

您的解决方案似乎是正确的!

上传解决方案时,select C 编译器的正确版本。 您正在使用 C99 或下一个,我认为..所以尝试使用它! C99

您的代码,经过简化,无需任何用户输入:

在 C:

#include <stdio.h>

int insertion_sort() {
   int arr[] = { 1, 1, 1, 2, 2 };
   /*int arr[] = { 2, 1, 3, 1, 2 };*/
   int n     = sizeof(arr)/sizeof(arr[0]);
   int count = 0;
   for(int i = 1; i < n; i++ ) {
      int key = arr[i];
      int j   = i - 1;
      while(( j >= 0 ) && ( arr[j] > key )) {
         arr[j+1] = arr[j];
         count++;
         j = j-1;
      }
      arr[j+1]=key;
   }
   for( int i = 0; i < n; i++ ) {
      printf( "%d,",arr[i]);
   }
   printf( "\n" );
   printf( "count: %d\n",count );
   return 0;
}

在Java中:

public class InsertionSort {

   public static void main( String[] args ) throws Exception {
      //final int inputArr[] = { 1, 1, 1, 2, 2 };
      final int inputArr[] = { 2, 1, 3, 1, 2 };
      final int n = inputArr.length;
      int count = 0;
      for( int i = 1; i < n; i++ ) {
         final int key = inputArr[i];
         int j   = i - 1;
         while(( j >= 0 ) && ( inputArr[j] > key )) {
            inputArr[j + 1] = inputArr[j];
            j = j - 1;
            count++;
         }
         inputArr[j + 1] = key;
      }
      System.out.println( Arrays.toString( inputArr ));
      System.out.println( "count: " + count );
   }
}

C 执行跟踪:

1,1,1,2,2,
count: 0

2,1,3,1,2,
count: 4

Java 执行轨迹:

[1, 1, 1, 2, 2]
count: 0

[2, 1, 3, 1, 2]
count: 4

区别在哪里?

编辑

您的代码不是 ANSI C:

gcc -ansi -c insertion_sort.c 
insertion_sort.c: In function ‘insertion_sort’:
insertion_sort.c:34:7: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
       for(int i=0; i<n; i++) {
       ^
insertion_sort.c:34:7: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
insertion_sort.c:39:15: error: redefinition of ‘i’
       for(int i=1; i<n; i++) {
               ^
insertion_sort.c:34:15: note: previous definition of ‘i’ was here
       for(int i=0; i<n; i++) {
               ^
insertion_sort.c:39:7: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
       for(int i=1; i<n; i++) {
       ^

代码本身是正确的,但你的输出是错误的。预期的输出格式是一个数字后跟换行符。您的 Java 代码使用 println 自动插入换行符。您的 C 代码缺少 \nprintf("%d\n", count); 是你应该使用的。