2017-03-12 140 views
2

我正在解决以下SPOJ问题。它是简单的插入排序算法。我的java代码工作,但C代码给出了错误的答案。 我在做什么错?C代码给出了错误的答案,但java代码给出了正确的答案spoj

请帮助和感谢很多...... :)

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

+2

有什么不对?什么是对的?你的输入是什么?已经完成的输出是什么?你真正看到了什么? –

+0

在插入排序中执行交换操作的次数 –

+0

您的Java代码使用'println'来添加换行符,但是您的C代码在输出'printf'中没有'\ n'。 – aragaer

回答

0

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

-1

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

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

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); 
    } 
} 

Ç执行轨迹:

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

但它不接受在这里[链接] http://www.spoj.com/problems/CODESPTB/} –

+1

看到编辑,我很困惑。 –

+0

再次用spoj网站上给出的两个样本进行测试,您的代码完美工作。 – Aubin

相关问题