2017-03-12 8 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コードには\nがありません。 printf("%d\n", count);を使用する必要があります。

-1

あなたの解決策は正しいようです!

ソリューションをアップロードするときは、正しいバージョンのCコンパイラを選択してください。 C99を使用している、または次に、私は思う..それを使用してみてください! C99

0

ユーザ入力なしであなたのコード、簡素化、:Cにおいて

:Javaでは

#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 

ここでは違いはありますか?

EDIT

あなたのコードは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サイトで与えられた2つのサンプルで再びテストされた、あなたのコードは完全に動作します。 – Aubin

関連する問題