APCS程式檢測 -實作題1050305 第1題 成績指標

第一步:泡沫排序法
最簡單的排序法,考試也很常出,建議是背起來
//泡沫排序法
void BubbleSort(int* pArray, int nNum)
{
int nTemp;
for (int i = 0; i < (nNum - 1); i++)
{
for (int j = 0; j < (nNum - 1 - i); j++)
{
if (pArray[j] > pArray[j + 1])
{
nTemp = pArray[j];
pArray[j] = pArray[j + 1];
pArray[j + 1] = nTemp;
}
}
}
}
第二步:找出最高不及格分數
//找出最高不及格分數的索引值
int GetUnluckyIndex(int* pGrade, int nStudenNum, int nPassGrade)
{
int nIndex = -1;
bool bFind = false;
for (int i = 0; i < nStudenNum; i++)
{
if (pGrade[i] >= nPassGrade)
{
nIndex = i - 1;
bFind = true;
break;
}
}
if (bFind == false) //表示全部都不及格,所以最左邊的就是最高的不及格分數
nIndex = nStudenNum - 1;
if (nIndex < 0) //表示全部都及格
nIndex = -1;
return nIndex;
}
第三步:找出最低及格分數
//找出最低及格分數的索引值
int GetLuckyIndex(int* pGrade, int nStudenNum, int nPassGrade)
{
int nIndex = -1;
bool bFind = false;
for (int i = (nStudenNum - 1); i >= 0; i--)
{
if (pGrade[i] < nPassGrade)
{
nIndex = i + 1;
bFind = true;
break;
}
}
if (bFind == false) //表示全部都及格,所以最右邊的就是最低的及格分數
nIndex = 0;
if (nIndex >= nStudenNum) //表示全部不及格
nIndex = -1;
return nIndex;
}
完整程式碼
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void BubbleSort(int* pArray, int nNum);
int GetUnluckyIndex(int* pGrade, int nStudenNum, int nPassGrade);
int GetLuckyIndex(int* pGrade, int nStudenNum, int nPassGrade);
int main()
{
const int PASS_GRADE = 60;
const int MAX_NUM = 20; //題目有寫測資是 1~20 的整數
//宣告陣列並初始化
int nGrade[MAX_NUM];
for (int i = 0; i < MAX_NUM; i++)
nGrade[i] = -1;
//處理輸入資料
//學生人數
string strStudentNum;
getline(cin, strStudentNum);
int nStudentNum = stoi(strStudentNum);
//學生分數
string strGrade;
getline(cin, strGrade);
stringstream delim(strGrade);
string pch;
for (int i = 0; i < nStudentNum; i++)
{
getline(delim, pch, ' ');
nGrade[i] = stoi(pch);
}
//泡沫排序法
BubbleSort(nGrade, nStudentNum);
//印出排序後的成績
for (int i = 0; i < nStudentNum; i++)
{
if (i == nStudentNum - 1)
cout << nGrade[i] << endl;
else
cout << nGrade[i] << " ";
}
//印出最高的不及格分數
int nUnluckyIndex = GetUnluckyIndex(nGrade, nStudentNum, PASS_GRADE);
if (nUnluckyIndex == -1)
cout << "best case" << endl;
else
cout << nGrade[nUnluckyIndex] << endl;
//印出找出最低的及格分數
int nluckyIndex = GetLuckyIndex(nGrade, nStudentNum, PASS_GRADE);
if (nluckyIndex == -1)
cout << "worst case" << endl;
else
cout << nGrade[nluckyIndex] << endl;
system("pause");
return 0;
}
//泡沫排序法
void BubbleSort(int* pArray, int nNum)
{
int nTemp;
for (int i = 0; i < (nNum - 1); i++)
{
for (int j = 0; j < (nNum - 1 - i); j++)
{
if (pArray[j] > pArray[j + 1])
{
nTemp = pArray[j];
pArray[j] = pArray[j + 1];
pArray[j + 1] = nTemp;
}
}
}
}
//找出最高不及格分數的索引值
int GetUnluckyIndex(int* pGrade, int nStudenNum, int nPassGrade)
{
int nIndex = -1;
bool bFind = false;
for (int i = 0; i < nStudenNum; i++)
{
if (pGrade[i] >= nPassGrade)
{
nIndex = i - 1;
bFind = true;
break;
}
}
if (bFind == false) //表示全部都不及格,所以最左邊的就是最高的不及格分數
nIndex = nStudenNum - 1;
if (nIndex < 0) //表示全部都及格
nIndex = -1;
return nIndex;
}
//找出最低及格分數的索引值
int GetLuckyIndex(int* pGrade, int nStudenNum, int nPassGrade)
{
int nIndex = -1;
bool bFind = false;
for (int i = (nStudenNum - 1); i >= 0; i--)
{
if (pGrade[i] < nPassGrade)
{
nIndex = i + 1;
bFind = true;
break;
}
}
if (bFind == false) //表示全部都及格,所以最右邊的就是最低的及格分數
nIndex = 0;
if (nIndex >= nStudenNum) //表示全部不及格
nIndex = -1;
return nIndex;
}

