请问一个排序题
(论坛 不能 附加图片了?) 我放在附件里面 题目[local]1[/local]
答案的 红色部分是这样的 为什么我写的那种 用起泡法排序运行结果却和这个不一样?请问我出错在什么地方了啊
for (i=0; i<cnt-1; i++)
for (j=i+1; j<cnt; j++)
if (b[i] > b[j])
{
k = b[i];
b[i] = b[j];
b[j] = k;
}
这个是我写的,主要是红色部分,
#include <stdio.h>
#define MAX 200
int a[MAX], b[MAX], cnt = 0;
void jsVal()
{
int i, j;
int a1, a2, a3, a4, k;
for (i=0; i<MAX; i++)
{
a1 = a[i]/1000;
a2 = a[i]%1000/100;
a3 = a[i]%100/10;
a4 = a[i]%10;
if ((a1 <= a2) && (a2 <= a3) && (a3 <= a4) && (a[i]%2 == 0))
{
b[cnt] = a[i];
cnt++;
}
}
for(j=0;j<cnt-1;j++)
for(i=0;i<cnt-j;i++)
if(b[i]>b[i+1])
{k=b[i];
b[i]=b[i+1];
b[i+1]=k;
}
}
void readDat()
{
int i;
FILE *fp;
fp = fopen("IN.DAT", "r");
for (i=0; i<MAX; i++)
fscanf(fp, "%d,", &a[i]);
fclose(fp);
}
void writeDat()
{
FILE *fp;
int i;
fp = fopen("OUT.DAT", "w");
fprintf(fp, "%d\n", cnt);
for (i=0; i<cnt; i++)
fprintf(fp, "%d\n", b[i]);
fclose(fp);
}
void main()
{
int i;
readDat();
jsVal();
printf("满足条件的数=%d\n", cnt);
for (i=0; i<cnt; i++)
printf("%d ", b[i]);
printf("\n");
writeDat();
}