【问题标题】:C code gives wrong answer but java code gives correct answer on spojC 代码给出错误答案,但 java 代码在 spoj 上给出正确答案
【发布时间】:2017-03-12 15:15:07
【问题描述】:

我正在解决关于 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 代码使用了添加换行符的 println,但您的 C 代码在输出 printf 中没有“\n”。
  • @aragaer 谢谢,我是傻瓜,请您发表您的答案...:)

标签: java c insertion-sort


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您的代码,经过简化,无需任何用户输入:

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

    【讨论】:

    • 用 spoj 网站上给出的两个示例再次测试,您的代码完美运行。
    • 使用示例它可以工作,但是当我提交时,它不起作用,上面的图片就是证明..
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多