通过引用传递而不修改值cpp

问题描述 投票:0回答:1

在采访中,我通过引用传递一个向量,但它没有修改。请帮忙

这是代码 `

bool c(int n, int ind, int sum, int count, vector<int> & A, vector<int> & ans){
    if(ind>=n){
        return false;
    }
    if(count == ans.size()){
        return sum == 0;
    }
    if(A[ind]<=sum){
        ans.push_back(A[ind]);
        if(c(n, ind+1, sum-A[ind], count+1, A, ans)) {
            // for(int l = 0; l < ans.size();l++) cout << ans[l] << " ";
            A.erase(A.begin() + ind);
            // cout << " \n ";
            // for(int l = 0; l < A.size();l++) cout << A[l] << " ";
            return true;
        }
        ans.pop_back();
        return c(n, ind+1, sum, count, A, ans);
    }
    return false;
}

vector<vector<int> > Solution::avgset(vector<int> &A) {
    int n = A.size();
    vector<int> ans;
    vector<vector<int>> res;
    int sum=0;
    for(auto i:A){
        sum+=i;
    }
    for(int i = 1; i < n; i++){
        if((sum*i)%n == 0){
            int ss = (sum*i)/n;
            if(c(n, 0, ss, i, A, ans)){
                res.push_back(ans);
                res.push_back(A);
                return res;
            } 
        }        
    }
    return res;
}

`

这是问题:https://www.interviewbit.com/problems/equal-average-partition/

Inerview 位不允许 cout,因此这些语句不起作用

c++ reference c++17
1个回答
0
投票

我认为这是一个概念化问题,而不是C++引用问题。

以下是应用于示例输入 (A = [1 7 15 29 11 9]) 的发布代码的日志记录,由于发布的 30000 个字符限制,i = 4 和 i = 5 被删除。查看发布的代码时可能会有所帮助。您可以看到矢量引用的行为符合预期。

    At the start of i = 1 in the avgset loop.
        A is [1 7 15 29 11 9].
        ans is [].
        res has no vectors.
        At the start of c with n = 6, ind = 0, sum = 12, and count = 1.
            A is [1 7 15 29 11 9].
            ans is [].
            Appending 1 to ans.
            At the start of c with n = 6, ind = 1, sum = 11, and count = 2.
                A is [1 7 15 29 11 9].
                ans is [1].
                Appending 7 to ans.
                At the start of c with n = 6, ind = 2, sum = 4, and count = 3.
                    A is [1 7 15 29 11 9].
                    ans is [1 7].
                    Returning true.
                Erasing the last element of ans.
                At the start of c with n = 6, ind = 2, sum = 11, and count = 2.
                    A is [1 7 15 29 11 9].
                    ans is [1].
                    Returning true.
                Returning false.
            Erasing the last element of ans.
            At the start of c with n = 6, ind = 1, sum = 12, and count = 1.
                A is [1 7 15 29 11 9].
                ans is [].
                Appending 7 to ans.
                At the start of c with n = 6, ind = 2, sum = 5, and count = 2.
                    A is [1 7 15 29 11 9].
                    ans is [7].
                    Returning true.
                Erasing the last element of ans.
                At the start of c with n = 6, ind = 2, sum = 12, and count = 1.
                    A is [1 7 15 29 11 9].
                    ans is [].
                    Returning true.
                Returning false.
            Returning false.

    At the start of i = 2 in the avgset loop.
        A is [1 7 15 29 11 9].
        ans is [].
        res has no vectors.
        At the start of c with n = 6, ind = 0, sum = 24, and count = 2.
            A is [1 7 15 29 11 9].
            ans is [].
            Appending 1 to ans.
            At the start of c with n = 6, ind = 1, sum = 23, and count = 3.
                A is [1 7 15 29 11 9].
                ans is [1].
                Appending 7 to ans.
                At the start of c with n = 6, ind = 2, sum = 16, and count = 4.
                    A is [1 7 15 29 11 9].
                    ans is [1 7].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 1, and count = 5.
                        A is [1 7 15 29 11 9].
                        ans is [1 7 15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 16, and count = 4.
                        A is [1 7 15 29 11 9].
                        ans is [1 7].
                        Returning true.
                    Returning false.
                Erasing the last element of ans.
                At the start of c with n = 6, ind = 2, sum = 23, and count = 3.
                    A is [1 7 15 29 11 9].
                    ans is [1].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 8, and count = 4.
                        A is [1 7 15 29 11 9].
                        ans is [1 15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 23, and count = 3.
                        A is [1 7 15 29 11 9].
                        ans is [1].
                        Returning true.
                    Returning false.
                Returning false.
            Erasing the last element of ans.
            At the start of c with n = 6, ind = 1, sum = 24, and count = 2.
                A is [1 7 15 29 11 9].
                ans is [].
                Appending 7 to ans.
                At the start of c with n = 6, ind = 2, sum = 17, and count = 3.
                    A is [1 7 15 29 11 9].
                    ans is [7].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 2, and count = 4.
                        A is [1 7 15 29 11 9].
                        ans is [7 15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 17, and count = 3.
                        A is [1 7 15 29 11 9].
                        ans is [7].
                        Returning true.
                    Returning false.
                Erasing the last element of ans.
                At the start of c with n = 6, ind = 2, sum = 24, and count = 2.
                    A is [1 7 15 29 11 9].
                    ans is [].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 9, and count = 3.
                        A is [1 7 15 29 11 9].
                        ans is [15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 24, and count = 2.
                        A is [1 7 15 29 11 9].
                        ans is [].
                        Returning true.
                    Returning false.
                Returning false.
            Returning false.

    At the start of i = 3 in the avgset loop.
        A is [1 7 15 29 11 9].
        ans is [].
        res has no vectors.
        At the start of c with n = 6, ind = 0, sum = 36, and count = 3.
            A is [1 7 15 29 11 9].
            ans is [].
            Appending 1 to ans.
            At the start of c with n = 6, ind = 1, sum = 35, and count = 4.
                A is [1 7 15 29 11 9].
                ans is [1].
                Appending 7 to ans.
                At the start of c with n = 6, ind = 2, sum = 28, and count = 5.
                    A is [1 7 15 29 11 9].
                    ans is [1 7].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 13, and count = 6.
                        A is [1 7 15 29 11 9].
                        ans is [1 7 15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 28, and count = 5.
                        A is [1 7 15 29 11 9].
                        ans is [1 7].
                        Returning true.
                    Returning false.
                Erasing the last element of ans.
                At the start of c with n = 6, ind = 2, sum = 35, and count = 4.
                    A is [1 7 15 29 11 9].
                    ans is [1].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 20, and count = 5.
                        A is [1 7 15 29 11 9].
                        ans is [1 15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 35, and count = 4.
                        A is [1 7 15 29 11 9].
                        ans is [1].
                        Appending 29 to ans.
                        At the start of c with n = 6, ind = 4, sum = 6, and count = 5.
                            A is [1 7 15 29 11 9].
                            ans is [1 29].
                            Returning true.
                        Erasing the last element of ans.
                        At the start of c with n = 6, ind = 4, sum = 35, and count = 4.
                            A is [1 7 15 29 11 9].
                            ans is [1].
                            Appending 11 to ans.
                            At the start of c with n = 6, ind = 5, sum = 24, and count = 5.
                                A is [1 7 15 29 11 9].
                                ans is [1 11].
                                Appending 9 to ans.
                                At the start of c with n = 6, ind = 6, sum = 15, and count = 6.
                                    A is [1 7 15 29 11 9].
                                    ans is [1 11 9].
                                    Returning false.
                                Erasing the last element of ans.
                                At the start of c with n = 6, ind = 6, sum = 24, and count = 5.
                                    A is [1 7 15 29 11 9].
                                    ans is [1 11].
                                    Returning false.
                                Returning false.
                            Erasing the last element of ans.
                            At the start of c with n = 6, ind = 5, sum = 35, and count = 4.
                                A is [1 7 15 29 11 9].
                                ans is [1].
                                Appending 9 to ans.
                                At the start of c with n = 6, ind = 6, sum = 26, and count = 5.
                                    A is [1 7 15 29 11 9].
                                    ans is [1 9].
                                    Returning false.
                                Erasing the last element of ans.
                                At the start of c with n = 6, ind = 6, sum = 35, and count = 4.
                                    A is [1 7 15 29 11 9].
                                    ans is [1].
                                    Returning false.
                                Returning false.
                            Returning false.
                        Returning false.
                    Returning false.
                Returning false.
            Erasing the last element of ans.
            At the start of c with n = 6, ind = 1, sum = 36, and count = 3.
                A is [1 7 15 29 11 9].
                ans is [].
                Appending 7 to ans.
                At the start of c with n = 6, ind = 2, sum = 29, and count = 4.
                    A is [1 7 15 29 11 9].
                    ans is [7].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 14, and count = 5.
                        A is [1 7 15 29 11 9].
                        ans is [7 15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 29, and count = 4.
                        A is [1 7 15 29 11 9].
                        ans is [7].
                        Appending 29 to ans.
                        At the start of c with n = 6, ind = 4, sum = 0, and count = 5.
                            A is [1 7 15 29 11 9].
                            ans is [7 29].
                            Returning true.
                        Erasing the last element of ans.
                        At the start of c with n = 6, ind = 4, sum = 29, and count = 4.
                            A is [1 7 15 29 11 9].
                            ans is [7].
                            Appending 11 to ans.
                            At the start of c with n = 6, ind = 5, sum = 18, and count = 5.
                                A is [1 7 15 29 11 9].
                                ans is [7 11].
                                Appending 9 to ans.
                                At the start of c with n = 6, ind = 6, sum = 9, and count = 6.
                                    A is [1 7 15 29 11 9].
                                    ans is [7 11 9].
                                    Returning false.
                                Erasing the last element of ans.
                                At the start of c with n = 6, ind = 6, sum = 18, and count = 5.
                                    A is [1 7 15 29 11 9].
                                    ans is [7 11].
                                    Returning false.
                                Returning false.
                            Erasing the last element of ans.
                            At the start of c with n = 6, ind = 5, sum = 29, and count = 4.
                                A is [1 7 15 29 11 9].
                                ans is [7].
                                Appending 9 to ans.
                                At the start of c with n = 6, ind = 6, sum = 20, and count = 5.
                                    A is [1 7 15 29 11 9].
                                    ans is [7 9].
                                    Returning false.
                                Erasing the last element of ans.
                                At the start of c with n = 6, ind = 6, sum = 29, and count = 4.
                                    A is [1 7 15 29 11 9].
                                    ans is [7].
                                    Returning false.
                                Returning false.
                            Returning false.
                        Returning false.
                    Returning false.
                Erasing the last element of ans.
                At the start of c with n = 6, ind = 2, sum = 36, and count = 3.
                    A is [1 7 15 29 11 9].
                    ans is [].
                    Appending 15 to ans.
                    At the start of c with n = 6, ind = 3, sum = 21, and count = 4.
                        A is [1 7 15 29 11 9].
                        ans is [15].
                        Returning true.
                    Erasing the last element of ans.
                    At the start of c with n = 6, ind = 3, sum = 36, and count = 3.
                        A is [1 7 15 29 11 9].
                        ans is [].
                        Appending 29 to ans.
                        At the start of c with n = 6, ind = 4, sum = 7, and count = 4.
                            A is [1 7 15 29 11 9].
                            ans is [29].
                            Returning true.
                        Erasing the last element of ans.
                        At the start of c with n = 6, ind = 4, sum = 36, and count = 3.
                            A is [1 7 15 29 11 9].
                            ans is [].
                            Appending 11 to ans.
                            At the start of c with n = 6, ind = 5, sum = 25, and count = 4.
                                A is [1 7 15 29 11 9].
                                ans is [11].
                                Appending 9 to ans.
                                At the start of c with n = 6, ind = 6, sum = 16, and count = 5.
                                    A is [1 7 15 29 11 9].
                                    ans is [11 9].
                                    Returning false.
                                Erasing the last element of ans.
                                At the start of c with n = 6, ind = 6, sum = 25, and count = 4.
                                    A is [1 7 15 29 11 9].
                                    ans is [11].
                                    Returning false.
                                Returning false.
                            Erasing the last element of ans.
                            At the start of c with n = 6, ind = 5, sum = 36, and count = 3.
                                A is [1 7 15 29 11 9].
                                ans is [].
                                Appending 9 to ans.
                                At the start of c with n = 6, ind = 6, sum = 27, and count = 4.
                                    A is [1 7 15 29 11 9].
                                    ans is [9].
                                    Returning false.
                                Erasing the last element of ans.
                                At the start of c with n = 6, ind = 6, sum = 36, and count = 3.
                                    A is [1 7 15 29 11 9].
                                    ans is [].
                                    Returning false.
                                Returning false.
                            Returning false.
                        Returning false.
                    Returning false.
                Returning false.
            Returning false.

    At the start of i = 4 in the avgset loop.
...

    At the start of i = 5 in the avgset loop.
...

    Result has no vectors.
© www.soinside.com 2019 - 2024. All rights reserved.