1.已知數(shù)據(jù)文件IN.dat中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請編寫函數(shù)primecount(),其功能是:如果四位數(shù)各個位上的數(shù)字均是奇數(shù),則統(tǒng)計出滿足此條件的個數(shù)count并把這些四位數(shù)按從大到小的順序存入數(shù)組b中。最后main()函數(shù)調(diào)用寫函數(shù)writeDat(),把結(jié)果count以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.dat文件中。
參考【答案】
void primecount()
{ int i, thou, hun, ten, data, j;
for (i=0; i《200; i++)
{ thou = a[i]/1000;
hun = a[i]00/100;
ten = a[i]0/10;
data = a[i];
if (thou%2!=0 && hun%2!=0 && ten%2!=0 && data%2!=0)
{ b[count] = a[i];
count++;
}
}
for (i=0; i
for (j=i+1; j
if (b[i] 《 b[j])
{ data = b[i];
b[i] = b[j];
b[j] = data;
}
}
參考【答案】
void primecount()
{ int i, thou, hun, ten, data, j;
for (i=0; i《200; i++)
{ thou = a[i]/1000;
hun = a[i]00/100;
ten = a[i]0/10;
data = a[i];
if (thou%2!=0 && hun%2!=0 && ten%2!=0 && data%2!=0)
{ b[count] = a[i];
count++;
}
}
for (i=0; i
for (j=i+1; j
if (b[i] 《 b[j])
{ data = b[i];
b[i] = b[j];
b[j] = data;
}
}