為什么這個編譯能通過,但是輸出之后又重復(fù)的?
#include <stdio.h>//函數(shù)內(nèi)定義的東西只能作用于這個函數(shù)?
//計算總分和平均分?
int getSumScore(int score[])
{
? ? int i;
? ? int average;//平均分?
? ? int sum=0;
? ? for(i=0;i<10;i++)
? ? {
? ? ? sum+=score[i];?
? ? }
? ? printf("總分為%d\n",sum);
? ? average=sum/10;
? ? printf("平均分為%d\n",average);
? ? return average;
}
//計算最高分
int maxScore(int score[])
{
? ? int max=-1;
? ? int j;
? ? for(j=0;j<10;j++)
? ? {
? ? ? ? if(score[j]>max)
? ? ? ? {
? ? ? ? ? ? max=score[j];
? ? ? ? ? ? printf("最高分為%d\n",max);
? ? ? ? }
? ? }
? ? return max;
}
//計算最低分
int minScore(int score[])
{
? ? int min=100;
? ? int k;
? ? for(k=0;k<10;k++)
? ? {
? ? ? ? if(score[k]<min)
? ? ? ? {
? ? ? ? ? ? min=score[k];
? ? ? ? ? ? printf("最低分為%d\n",min);
? ? ? ? }
? ? }
? ? return min;
}
//考試成績降序排序
void sort(int score[])
{
? ? int x,y,z;
? ? for(x=8; x>=0; x--)//一共10個數(shù),需要比較9次,0~8是9個數(shù)
? ? {
? ? ? ? for(y=0;y<=x;y++)
? ? ? ? {
? ? ? ? ? ? if(score[y]<score[y+1] ) ? ? //當(dāng)前面的數(shù)比后面的數(shù)大時
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int temp; ? ? ? ? ?//定義臨時變量temp
? ? ? ? ? ? ? ? temp=score[y]; ? ? ?//將前面的數(shù)賦值給temp
? ? ? ? ? ? ? ? score[y]=score[y+1]; //前后之?dāng)?shù)顛倒位置
? ? ? ? ? ? ? ? score[y+1]=temp; ?//將較大的數(shù)放在后面 ? ?
? ? ? ? ? ? } ? ? ? ? ? ? ? ??
? ? ? ? }
? ? }
? ? printf("---成績排名---\n");
? ? for(z=0;z<10;z++)
? ? {
? ? printf(" %d ",score[z]);
? ? }
??
}
int main()
{
? ? int score[10]={67,98,75,63,82,79,81,91,66,84};
? ? getSumScore(score);
? ? maxScore(score);
? ? minScore(score);
? ? sort(score);
? ? return 0;
}
2017-02-11
輸出兩個應(yīng)該是因為 printf 語句在 if 里面。
你先是假定 max=-1,每當(dāng)判定 score[j] 大于 max 成功時就會輸出一次,所以會輸出兩次。慕課網(wǎng)頁評定系統(tǒng)又不是很完善,所以輸出兩個也能通過。
Hope that Help ! ! !