設計一個程式輸入學生的國語成績當輸入 999 時表示輸入完畢在全班成績輸入完畢後輸出這個班國語平均成績然後再依序顯示介於 0~59、60~69、70~79、80~89、90~99 與 100 分的分別有多少學生此外還必須判斷輸入的成績是否介於0~100 之間如果不正確要求重新輸入?
回答 (3)
#include<iostream>
using namespace std;
int main(void){
const int end = 999, top=100;
unsigned lvl[top/10+1] = {0}, grade;
for(; cout << "give me a grade: "
&& cin >> grade
&& end != grade
;)
if(grade <= top) ++lvl[grade/10];
else cout << "input out of range, please ";
for(int i = 5; i > 0; --i) lvl[0] += lvl[i];
cout << "student count at 0~59 basket = " << lvl[0] << endl;
for(unsigned i = 6; i < sizeof(lvl)/sizeof(lvl[0])-1; ++i)
cout << "student count at " << (i*10) << '~' << (i*10+9) << " basket = " << lvl[i] << endl;
cout << "student count at 100 = " << lvl[top/10] << endl;
return 0;
}
#include<iostream>
using namespace std;
int main(void) {
int step[6] = {100,90,80,70,60,0};
int amount[6] ;
for(int i=0;i<6;i++) amount[i] = 0;
while(1){
float itemp;
scanf(" %f",&itemp);
if( itemp == 999) break;
if( itemp <0 || itemp >100) {printf("超出範圍!!\n");continue;}
for(int i=0;i<6;i++) if(itemp >= step[i]) { amount[i]++; break; } }
printf("%d\t分有%d人\n",step[0],amount[0]);
for(int i=1;i<6;i++) printf("%d~%d\t分有%d人\n",step[i],step[i-1]-1,amount[i]);
system("pause"); return 0; }
收錄日期: 2021-05-04 02:21:00
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20170610151424AARkAVL
檢視 Wayback Machine 備份