1

我的程序从输入文件中获取值并将它们传递给一个名为 gpa 的数组。我创建了一个函数,它在间隙数组中找到最小值并显示它,但如果有一个重复值,我还需要显示一个重复值。我该怎么做。

先感谢您

#include <iostream>
#include <fstream>

using namespace std;

void lowestGpa(string names[], double gpa[], int SIZE){
    int count;
    double lowest = gpa[0];
    string name;

    for (count = 0; count < SIZE; count++)
    {
        if (gpa[count] <= lowest)
        {
            lowest = gpa[count];
            name = names[count];
        }
    }

    cout << name << " " << lowest;
}

int main()
{
    ifstream infile;
    infile.open("GPA.txt");

    int const SIZE = 15;
    string names[SIZE];
    double gpa[SIZE];

    while (infile)
    {
        for(int i = 0; i < SIZE; i++)
        {
            infile >> names[i] >> gpa[i];
        }
    }

    lowestGpa(names, gpa, SIZE);

    infile.close();

    return 0;
}
4

5 回答 5

1
//Add a loop to cout the lowest's name 
for (count = 0; count < SIZE; count++)
{

    if (gpa[count] == lowest )

    {
        cout << names[count]<< " " << lowest;
    }

}

但是这个方法并不完美,2个循环。

于 2013-10-30T03:05:17.647 回答
1

最简单(但不一定是计算速度最快)的方法可能是对数据进行排序。然后您可以找到最低的,因为它将位于数组中的位置 0,并且您可以通过迭代列表并查找 score[n] == score[n + 1] 的实例来检查重复项。

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    const size_t size = 7;
    int array[size] = { 70, 20, 60, 40, 10, 30, 40 };
    // note: the second argument is just past the last element,
    // it's where the array has *ended*
    std::sort(&array[0], &array[size]);

    for (size_t i = 0; i < size; ++i)
        std::cout << array[i] << " ";
    std::cout << endl;

    // lowest:
    std::cout << "Lowest: " << array[0] << std::endl;

    // duplicate check
    // skip 1 and use backwards-looking checks, this saves us having
    // to do math in the loop condition.
    for (size_t i = 1; i < size; ++i) {
        if (array[i] == array[i - 1])
            std::cout << (i - 1) << " and " << i <<
                " contain " << array[i] << std::endl;
    }
    return 0;
}

现场演示:http: //ideone.com/3WKrHR

于 2013-10-30T03:29:43.650 回答
1

无需对整个数组进行排序,也无需循环两次。只需使用一个额外的向量来存储 GPA 最低的名称。

void lowestGpa(string names[], double gpa[], int SIZE){
    double lowest = gpa[0];
    vector<int> lowids(1, 0);

    for (int count = 0; count < SIZE; count++)
    {
        if (gpa[count] < lowest)
        {
            lowest = gpa[count];
            lowids.clear();
            lowids.push_back(count);
        }
        else if (gpa[count] == lowest)
        {
            lowids.push_back(count);
        }
    }

    for (vector<int>::const_iterator i = lowids.begin(); i != lowids.end(); ++i)        
        cout << names[*i] << " " << lowest << endl;
}
于 2013-10-30T03:39:22.550 回答
0

您可能希望找到最低 GPA 的所有重复项。

#include <algorithm>
#include <numeric_limits>

void lowestGpa(string names[], double gpa[], int size){
    double lowest = numeric_limits<double>::infinity(); //or 1. / 0
    for(int i = 0; i < size; i++) {
        lowest = min(lowest, gpa[i]);
    }

    for(int i = 0; i < size; i++) {
        if(gpa[i] == lowest) {
            cout << names[i] << " " << gpa[i];
        }
    }
}

这运行在O(n),

此外,这不会在sizeis时崩溃0

于 2013-10-30T03:13:43.760 回答
0

您可以使用std::set, std::set_difference,std::sort来查找最低重复元素:

#include <vector>
#include <set>
#include <iostream>

int main() {
    int gpas[] = {100,30,97,67,89,100,90,67,100,67,30,1,1};//sample values

    std::vector<int> vec(gpas, gpas + sizeof(gpas)/sizeof(int));//this constructor is only valid for stack allocated arrays
    std::set<int> cpy(vec.begin(), vec.end());//create a set with the unique gpas in a set
    std::vector<int> duplicates;

    std::set<int>::const_iterator it;

    std::sort(vec.begin(), vec.end());//sort the set as set_difference requires that the container is sorted

    //find the duplicates using set_difference. This will find all duplicates
    //(which will be repeated n - 1 times in `duplicates` for n > 1)
    std::set_difference(vec.begin(), vec.end(), cpy.begin(), cpy.end(), std::back_inserter(duplicates));

    //this creates a set from all the duplicates found
    std::set<int> unique_duplicates(duplicates.begin(), duplicates.end());

    //output the duplicates
    for (it = unique_duplicates.begin(); it != unique_duplicates.end(); ++it) {
        std::cout << *it << std::endl;
    }

    //if you want the lowest value of the duplicates:
    //you can use the min_element from <algorithm>:
    //std::min_element(unique_duplicates.begin(), unique_duplicates.end());

    //However, as the vector `vec` was already sorted, this ensures that
    //all the other results obtained from the sorted vector will ALREADY
    //be sorted. Hence, if size is > 0, then we can simply look at the
    //first element alone.
    if (unique_duplicates.size() > 0) {
        it = unique_duplicates.begin();
        std::cout << "The LOWEST duplicate element is: " << *it << std::endl;
    }

    return 0;
}

从发布的示例数据中,输出:

1
30
67
100

作为重复元素

并且,1作为最低重复元素。

参考:

http://www.cplusplus.com/reference/algorithm/set_difference/

于 2013-10-30T04:34:42.450 回答