0

ここで非常に厄介な問題に遭遇します。この課題での私の目標は、ボリュームで最もアクティブな 5 人のバイヤーの名前を保持する配列を返すことです。stockSymbols はこれらの名前を保持していますが、購入された合計は正常に計算され、totalBought 配列に配置されます。次に、上位 5 つの最大のものを見つけ出し、それらの名前を totalSold に入れてみました。

私は C++ の初心者なので、このやり方が間違っていることは承知しています。また、コンパイルも行われず、いたるところにメモリ エラーがスローされます (if(biggest[0] < totalBought[i])) 行またはその周辺から始まります)。

この作業を成功させるためのより良い方法を知っている人がいれば、私はあなたの意見に感謝します. ただし、これを計算する最も効率的な方法 (速度の点で) を見つけたいと思っています。

どうもありがとう。

string* Analyser::topFiveBuyers()
{
    // Your code
string* totalSold;
totalSold = new string[5];

    string stockSymbols[] = {"W Buffet", "P Lynch", "G Soros", "J Neff", "Hargreaves Lansdown",
        "Sippdeal", "Saga", "Halifax", "iWeb", "Alliance Trust", "Clubfinance", "Lloyds TSB", "Saxo" };

    int totalBought[13];

    for(int i = 0; i < nTransactions; i++)
    {
        for(int j = 0; j < 13; j++)
        {
            if(transArray[i].buyerName == stockSymbols[j])
            {
                totalBought[j] += transArray[i].numShares;
            }
        }
    }

    int biggest[] = {-1, -1, -1, -1, -1};

    for(int i = 0; i < 13; i++)
    {
        if(biggest[0] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = biggest[2];
            biggest[2] = biggest[1];
            biggest[1] = biggest[0];
            biggest[0] = totalBought[i];
            totalSold[0] = stockSymbols[i];
        }
        else if(biggest[1] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = biggest[2];
            biggest[2] = biggest[1];
            biggest[1] = totalBought[i];
            totalSold[1] = stockSymbols[i];
        }
        else if(biggest[2] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = biggest[2];
            biggest[2] = totalBought[i];
            totalSold[2] = stockSymbols[i];
        }
        else if(biggest[3] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = totalBought[i];
            totalSold[3] = stockSymbols[i];
        }
        else if(biggest[4] < totalBought[i])
        {
            biggest[4] = totalBought[i];
            totalSold[4] = stockSymbols[i];
        }
    }

    return totalSold;
}
4

1 に答える 1

0

目的を達成するために、コンビネーション マップとマルチ マップを提案します。関数の結果を文字列のベクトルに変更する自由を取りました。正直なところ、次のようにする必要があります。

std::vector<string> Analyzer::topFiveBuyers()
{
    typedef map<string, int> xact_map;
    xact_map xacts;
    for (int i=0;i<nTransactions;++i)
        xacts[transArray[i].buyerName] += transArray[i].numShares;

    // enumerate the map, beginning to end, throwing all elements
    //  into a multimap keyed by transaction count rather than name.
    //  note this considers an element "less" (and therefore at the
    //  beginning of the sort order) if the share count is *greater*.

    typedef multimap<int, string, std::greater<int>> highest_map;
    highest_map highest;
    for (xact_map::const_iterator it=xacts.begin(); it != xacts.end(); ++it)
        highest.insert(highest_map::value_type(it->second, it->first));

    // now just peel off the top five elements from the multimap.
    vector<string> results;
    int n=0;
    for (highest_map::const_iterator it=highest.begin(); 
         it != highest.end() && n<5; ++it,++n)
    {
        results.push_back(it->second);
    }

    return results;
}
于 2013-03-21T10:42:37.480 に答える