2017-03-12 4 views
2

다음의 SPOJ에 관한 문제를 해결하고 있습니다. 간단한 삽입 정렬 알고리즘입니다. 내 자바 코드가 작동하지만 C 코드가 잘못된 대답을주고있다. 내가 뭘 잘못하고 있니?C 코드가 틀린 답을 주지만, 자바 코드가 spoj에 정확한 답을줍니다.

도와주세요 감사 많은 ... :)

link of problem statement

자바 코드

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

무엇이 잘못 되었나요? 뭐가 맞습니까? 당신의 의견은 무엇이며 expected ouput은 무엇입니까? 너는 실제로 무엇을 보느냐? –

+0

스왑 조작이 삽입 정렬로 수행 된 횟수 –

+0

Java 코드는 개행을 추가하는'println'을 사용하지만 C 코드에는 출력 'printf'에'\ n '이 없습니다. – aragaer

답변

0

코드 자체는 정확하지만 출력이 잘못된 것입니다. 예상되는 출력 형식은 줄 바꿈 뒤에 오는 숫자입니다. Java 코드는 자동으로 개행을 삽입하는 println을 사용합니다. C 코드에 \n이 없습니다. printf("%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; 
} 

:

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

하지만 여기에 동의하지 않습니다. [link] http://www.spoj.com/problems/CODESPTB/} –

+1

편집보기 , 나는 혼란 스럽다. –

+0

spoj 사이트에 제공된 두 개의 샘플로 다시 테스트하면 코드가 완벽하게 작동한다. – Aubin