计算机三网考试题,会的人进~~~

发布时间:2024-11-01 08:59 发布:上海旅游网

问题描述:

题目见图
这是答案;
int i,j,t;
for(i=0;i<MAX;i++)
{
if(a[i]/1000%2==0&&a[i]/100%2==0&&a[i]/10%2==0&&a[i]%2==0)
b[cnt++]=a[i];
}
for(i=0;i<cnt-1;i++)
for(j=i+1;j<cnt;j++)
if(b[i]<b[j])
t=a[i];
a[i]=a[j];
a[j]=t;
上面是我写的程序片段,就是考的内容,整个程序是:
#include <stdio.h>
#define MAX 200
int a[MAX], b[MAX], cnt = 0;
void jsVal()
{int i,j,t;
for(i=0;i<MAX;i++)
{
if(a[i]/1000%2==0&&a[i]/100%2==0&&a[i]/10%2==0&&a[i]%2==0)
b[cnt++]=a[i];
}
for(i=0;i<cnt-1;i++)
for(j=i+1;j<cnt;j++)
if(b[i]<b[j])
t=a[i];
a[i]=a[j];
a[j]=t;

}
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();
}

选出来的数字貌似是对的但就是顺序不对,各位高手快帮帮我吧~~~20号左右就考试了~~~~

问题解答:

if(b[i]<b[j])
t=a[i];
a[i]=a[j];
a[j]=t;
应改为
if(b[i]<b[j])
{t=a[i];
a[i]=a[j];
a[j]=t;
}
否则只执行t=a[i]不排序。

void jsVal()
{
int iQian;
int iBai;
int iShi;
int iGe;
int iCtrI;
int iCtrJ;
int iCtrK;
int iTemp;

for(iCtrI=0;iCtrI<200;iCtrI++)
{
iQian=a[iCtrI]/1000;
iBai=a[iCtrI]/100%10;
iShi=a[iCtrI]/10%10;
iGe=a[iCtrI]%10;
if( iQian%2==0 && iShi%2==0 && iBai%2==0 && iGe%2==0 )
{
b[cnt]=a[iCtrI];
cntN++;
}
}
for(iCtrJ=0;iCtrJ<cnt-1;iCtrJ++)
{
for(iCtrK=iCtrJ+1;iCtrK<cnt;iCtrK++)
{
if( b[iCtrJ]<b[iCtrK] )
{
iTemp=b[iCtrJ];
b[iCtrJ]=b[iCtrK];
b[iCtrK]=iTemp;
}
}
}
}

热点新闻