简体   繁体   English

C代码给出错误答案,但Java代码给出正确的spoj答案

[英]C code gives wrong answer but java code gives correct answer on spoj

I am solving following problem on SPOJ.It is simple insertion sort algorithm. 我正在解决SPOJ上的以下问题,这是简单的插入排序算法。 My java code works but C code is giving wrong answer. 我的Java代码有效,但C代码给出了错误的答案。 what wrong i am doing? 我在做什么错?

Please help and thanks lot......:) 请帮助并非常感谢...... :)

link of problem statement 问题陈述的链接

java code 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 code 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解决方案被接受

Your code, simplified, without any user input: 您的代码经过简化,无需任何用户输入:

In C: 在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;
}

In Java: 在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 Execution trace: C执行跟踪:

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

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

Java Execution trace: Java执行跟踪:

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

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

Where is the differences? 差异在哪里?

EDIT 编辑

Your code isn't ANSI C: 您的代码不是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++) {
       ^

The code itself is correct, but your output is wrong. 代码本身是正确的,但是您的输出是错误的。 Expected output format is a number followed by newline. 预期的输出格式是一个数字,后跟换行符。 Your Java code uses println which automatically inserts newline. 您的Java代码使用println ,它会自动插入换行符。 Your C code lacks \\n . 您的C代码缺少\\n printf("%d\\n", count); is what you should use. 是您应该使用的。

Your solution seems to be correct! 您的解决方案似乎是正确的!

When you upload the solution, select the correct version of C compiler. 上传解决方案时,请选择正确的C编译器版本。 You are using C99 or next, i think.. so try to use it! 我认为您正在使用C99更高版本 ,所以请尝试使用它! C99 C99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM