我正在開發一個程式,它允許用戶為足球比賽輸入一些姓名和整數,即輸入球員的姓名、球衣號碼和得分,然后在最后列印出來。
我將如何找到得分最高的球員的名字并列印出來?
這是我下面的不完整代碼:
void showHighest(Player p[], int size)
{
int high = 0;
for (int counter = 0; counter < size; counter )
{
if (high < p[counter].points)
{
high = p[counter].points;
}
if (p[counter].points )
{
}
}
cout << "The player with the most points was: " << p[high].name << "with " << high << "amount of points." << endl;
}
uj5u.com熱心網友回復:
您需要跟蹤陣列中最高玩家的索引,而不僅僅是最高點。在您的cout陳述中,您使用最高點就好像它是一個index,而事實并非如此。
試試這個:
void showHighest(Player p[], int size)
{
int highest_points = 0;
int highest_index = -1;
for (int counter = 0; counter < size; counter)
{
if (highest_points < p[counter].points)
{
highest_index = counter;
highest_points = p[counter].points;
}
}
if (highest_index != -1)
cout << "The player with the most points was: " << p[highest_index].name << " with " << highest_points << " amount of points." << endl;
}
或者,您可以將跟蹤變數初始化為第一個玩家的資訊,并在第二個玩家處開始回圈,例如:
void showHighest(Player p[], int size)
{
if (size < 1) return;
int highest_points = p[0].points;
int highest_index = 0;
for (int counter = 1; counter < size; counter)
{
if (highest_points < p[counter].points)
{
highest_index = counter;
highest_points = p[counter].points;
}
}
cout << "The player with the most points was: " << p[highest_index].name << " with " << highest_points << " amount of points." << endl;
}
或者,使用指標跟蹤Player最高的points,例如:
void showHighest(Player p[], int size)
{
if (size < 1) return;
Player* highest_player = &p[0];
for (int counter = 1; counter < size; counter)
{
if (highest_player->points < p[counter].points)
{
highest_player = &p[counter];
}
}
cout << "The player with the most points was: " << highest_player->name << " with " << highest_player->points << " amount of points." << endl;
}
或者,您可以使用標準std::max_element()演算法Player在points不使用手動回圈的情況下找到最高的,例如:
#include <algorithm>
void showHighest(Player p[], int size)
{
if (size < 1) return;
auto p_end = p size;
auto it = std::max_element(p, p_end,
[](const Player &a, const Player &b){
return a.points < b.points;
}
);
cout << "The player with the most points was: " << it->name << " with " << it->points << " amount of points." << endl;
}
uj5u.com熱心網友回復:
一個好的解決方案是將“高”整數的更改設定為玩家物件。然后,您將持有玩家物件,而不是僅持有最高分。
void showHighest(Player p[], int size)
{
Player* high;
for (int counter = 0; counter < size; counter )
{
if (high.points < p[counter].points)
{
high = p[counter];
}
}
cout << "The player with the most points was: " << high.name << "with " << high.points << "amount of points." << endl;
}
顯然,還有許多其他方法如上所述。另一種不太有效的方法是“記住”最高分值(就像你做的那樣),然后檢查哪個玩家有這個分數:
void showHighest(Player p[], int size)
{
int high = 0;
for (int counter = 0; counter < size; counter )
{
if (high < p[counter].points)
{
high = p[counter].points;
}
}
string name = "";
for (int counter = 0; counter < size; counter )
{
if(player[counter].points == high)
{
name = p[counter].name;
}
}
cout << "The player with the most points was: " << name << "with" << high << "amount of points." << endl;
}
你可以找到很多其他的方法。由于使用了您在 C 中的 OOP 優勢,我會推薦第一個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372504.html
標籤:C
下一篇:雙向鏈表冒泡排序
