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

时间:2017-03-12 15:15:07

标签: java c insertion-sort

我正在解决SPOJ上的以下问题。这是一种简单的插入排序算法。我的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;
}

java solution accepted

3 个答案:

答案 0 :(得分:0)

您的代码,简化,没有任何用户输入:

在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++) {
       ^

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

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

上传解决方案时,请选择正确版本的C编译器。 你正在使用 C99 或者下一个,我想......所以尽量使用它! C99