4.歌星大獎賽
在歌星大獎賽中,有10個評委為參賽的選手打分,分數為1~100分。選手最后得分為:去掉一個最高分和一個最低分后其余8個分數的平均值。請編寫一個程序實現。
*問題分析與算法實現
這個問題的算法十分簡單,但是要注意在程序中判斷最大、最小值的變量是如何賦值的。
*程序說明與注釋
#include void main()
{
int integer,i,max,min,sum;
max=-32768; /*先假設當前的最大值max為C語言整型數的最小值*/
min=32767; /*先假設當前的最小值min為C語言整型數的最大值*/
sum=0; /*將求累加和變量的初值置為0*/
for(i=1;i<=10;i++)
{
printf("Input number %d=",i);
scanf("%d",&integer); /*輸入評委的評分*/
sum+=integer; /*計算總分*/
if(integer>max)max=integer; /*通過比較篩選出其中的最高分*/
if(integer printf("Canceled max score:%d\nCanceled min score:%d\n",max,min);
printf("Average score:%d\n",(sum-max-min)/8); /*輸出結果*/
}
*運行結果
Input number1=90
Input number2=91
Input number3=93
Input number4=94
Input number5=90
Input number6=99
Input number7=97
Input number8=92
Input number9=91
Input number10=95
Canceled max score:99
Canceled min score:90
Average score:92
*思考題
題目條件不變,但考慮同時對評委評分進行裁判,即在10個評委中找出最公平(即評分最接返平均分)和最不公平(即與平均分的差距最大)的評委,程序應該怎樣實現?
求最大數
問555555的約數中最大的三位數是多少?
*問題分析與算法設計
根據約數的定義,對于一個整數N,除去1和它自身外,凡能整除N的數即為N的約數。因此,最簡單的方法是用2到N-1之間的所有數去除N,即可求出N的全部約數。本題只要求取約數中最大的三位數,則其取值范圍可限制在100到999之間。
*程序說明與注釋
#include void main()
{
long i;
int j;
printf("Please input number:");
scanf("%ld",&i);
for(j=999;j>=100;j--)
if(i%j==0)
{
printf("The max factor with 3 digits in %ld is:%d,\n",i,j);
break;
}
}
*運行結果
輸入:555555
輸出:The max factor with 3 digits in 555555 is:777
·2009年計算機等考一級WPS上機考試指導(六) (2009-9-17 17:26:38)
·2009年計算機等考一級WPS上機考試指導(五) (2009-9-17 17:18:25)
·2009年計算機等考一級WPS上機考試指導(四) (2009-9-17 17:14:45)
·2009年計算機等考一級WPS上機考試指導(三) (2009-9-17 17:09:59)
·2009年計算機等考一級WPS上機考試指導(二) (2009-9-17 17:05:19)