为什么这段代码打出来是空的?与书上运行结果不一样?

/* Note:Your choice is C IDE */

#include "stdio.h"

#define N 30

void main()

{

 int i,j,temp,a[N];

 printf("Please input class scores:\n");

for(i=0;i<N;i++)

scanf("%d",&a[i]);

 for(j=i+1;j<N-1;j++)

  for(j=i+1;j<N;j++)

   if(a[i]<a[j])

   {

   temp=a[i];

   a[i]=a[j];

   a[j]=temp;

   }

   printf("class scores other:");

   for(i=0;i<N;i++)

   {

   if(i%6==0)

    printf("\n");

     printf("%3d",a[i]);

   }   

}

https://img3.mukewang.com/5beb758200013b3f10801440.jpg

https://img3.mukewang.com/5beb75830001741410801440.jpg


noe12138
浏览 1680回答 1
1回答

Cfans丶夏

亲,是for(i = 0; i < N - 1; i++)     for(j = i + 1; j < N; j++)         if(a[i] < a[j])         {             temp = a[i];             a[j] = a[i];             a[i] = temp;         }普通的排序算法。代码重要的是理解,是懂得如何与计算机沟通,相对排序经典的算法还有冒泡算法for(i = 0; i < N - 1; i++)     for(j = 0; j < N - 1 - i; j++)         if(a[j] < a[j + 1])         {             temp = a[j];             a[j] = a[j + 1];             a[j + 1] = a[j];         }还有如果运用的是C语言自带的库的话,尽量用#include <stdio.h> 虽然没有严格要求,但是这还是有区别的。
打开App,查看更多内容
随时随地看视频慕课网APP