C++在不知道数组大小的情况下数组中的最大/最小元素
我被分配了一项任务,这没什么特别的,但我确实在这里碰壁了......
在获得算术平均值后,我需要比较它们并输出最高和最低的。
x 是学号,vid[] 是算术平均值。
例如:
学生编号 x 具有算术平均值 vid[i]
,任务要求我输出哪个学生的平均值最高,哪个学生的平均值最低。
最糟糕的是我不能使用像 max() 和 min() 这样的东西,因为我不知道总共有多少学生。另外,它们都是具有相同变量名 vid[] 的数组。
任何帮助将不胜感激=)
int main()
{
int mokSK=0, p1[25], p2[25], p3[25], x[25], vid[25], iv=0;
ifstream inFile("inFile.in");
ofstream outFile("outFile.out");
inFile >> mokSK;
for(int i=0;i<mokSK;i++)
{
inFile >> x[i] >> p1[i] >> p2[i] >> p3[i];
vid[i]=(p1[i]+p2[i]+p3[i])/3;
outFile<< x[i] <<" " << vid[i] << endl;
}
return 0;
}
I was given a task, it's nothing special but I did hit the wall here...
After getting arithmetical means I need to compare them and output the highest and the lowest ones.
The x is a student number, the vid[] is the arithmetical mean.
For example:
Student number x has arithmetical mean vid[i]
and the task wants me to output which student has the highest and which one has the lowest means.
The worst part that I can't use stuff like max() and min() because I don't know how many students are there in total. Plus they are all arrays which have the same variable name vid[].
Any help would be appreciated =)
int main()
{
int mokSK=0, p1[25], p2[25], p3[25], x[25], vid[25], iv=0;
ifstream inFile("inFile.in");
ofstream outFile("outFile.out");
inFile >> mokSK;
for(int i=0;i<mokSK;i++)
{
inFile >> x[i] >> p1[i] >> p2[i] >> p3[i];
vid[i]=(p1[i]+p2[i]+p3[i])/3;
outFile<< x[i] <<" " << vid[i] << endl;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望 O(1) 访问最高和最低评分的学生;从阅读开始,在每次阅读过程中更新学生的最高和最低评分。
更清楚地说:从执行一开始就跟踪最低和最高评分的学生,并根据需要在每个学生数据阅读过程中更新最高和最低评分的学生。
If you want O(1) to access max and min graded students; from the beginning of reading, update your max and min graded student in each read pass.
to be more clear: keep track of min and max graded student from the very beginning of the execution and update max and min graded students in each student data reading pass if needed.