C++在不知道数组大小的情况下数组中的最大/最小元素

发布于 2024-12-15 03:54:17 字数 775 浏览 0 评论 0原文

我被分配了一项任务,这没什么特别的,但我确实在这里碰壁了......

在获得算术平均值后,我需要比较它们并输出最高和最低的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

快乐很简单 2024-12-22 03:54:17

如果您希望 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.

如梦 2024-12-22 03:54:17
int main()
{
  int mokSK=0, p1[25], p2[25], p3[25],x[25],vid[25],iv=0;
  int minmean = INT_MAX; int minstud= 0;// initialize minmean and max mean with first mean
  int maxmean = 0; int maxstud= 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;
    if(vid[i]>maxmean){maxmean = vid[i]; maxstud = i;}
    if(vid[i]<minmean){minmean = vid[i]; minstud = i;}
    // not handled if multple students have maxmean or minmean
    outFile<< x[i] <<" " << vid[i] << endl; 
  }

 outFile << "Max mean: " << maxmean << ", student id: " << maxstud << endl;
 outFile << "Min mean: " << minmean << ", student id: " << minstud << endl;
 return 0;
}
int main()
{
  int mokSK=0, p1[25], p2[25], p3[25],x[25],vid[25],iv=0;
  int minmean = INT_MAX; int minstud= 0;// initialize minmean and max mean with first mean
  int maxmean = 0; int maxstud= 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;
    if(vid[i]>maxmean){maxmean = vid[i]; maxstud = i;}
    if(vid[i]<minmean){minmean = vid[i]; minstud = i;}
    // not handled if multple students have maxmean or minmean
    outFile<< x[i] <<" " << vid[i] << endl; 
  }

 outFile << "Max mean: " << maxmean << ", student id: " << maxstud << endl;
 outFile << "Min mean: " << minmean << ", student id: " << minstud << endl;
 return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文