代码之家  ›  专栏  ›  技术社区  ›  Kais Hasan

c++push和front中的队列

  •  -2
  • Kais Hasan  · 技术社区  · 8 年前

    代码是codeforces中一个问题的答案,所以我只需要知道代码中的编程错误是什么。。队列产生了一个问题,他没有推送元素,或者可能是输出中的问题。。你能帮助我吗?

    当我运行它时。。他们打印一个随机数字,而不是我把他们推到队列中的数字

    #include<bits/stdc++.h>
    
    using namespace std;
    bool check(int a , int b , string s){
        int test[a][b];
        int k = 0;
    
        for(int i = 0 ; i < b ; ++i){
            for(int j = 0 ; j < a ; ++j){
                test[i][j] = s[k++];
            }
        }
    
        bool f = true;
        bool fx = false;
    
        for(int i = 0 ; i < b ; ++i){
            for(int j = 0 ; j < a ; ++j){
                if(test[j][i] == 'O'){
                    f = false;
                    break;
                }
            }
            if(f){
                fx = true;
                break;
            }
            f = true;
        }
        return fx;
    }
    
    int main(void){
        int t;
        cin>>t;
    
        string s;
        queue<int> qa;
        queue<int> qb;
        const int n = 12;
        for(int i = 0 ; i < t ; ++i){
            cin>>s;
            int cnt = 0;
            int a[6] = {1  , 2 , 3 ,4  ,6 , 12};
            int b[6] = {12 , 6 , 4 , 3 ,2 ,  1};
    
            for(int j = 0 ; j < 6 ; ++j){
                if(check(a[j] , b[j] , s)){
                    ++cnt;
                    qa.push(a[j]);
                    qb.push(b[j]);
                }
            }
            cout<<cnt<<' ';
            while(!qa.empty() && !qb.empty()){
                cout<<qa.front() <<'x'<<qb.front()<<' ';
                qa.pop();
                qb.pop();
            }
            cout<<endl;
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Breno Castro Cardoso    8 年前

    在本部分中

    int test[a][b];
    int k = 0;
    for(int i = 0 ; i < b ; ++i)
    {
        for(int j = 0 ; j < a ; ++j)
        {
            test[i][j] = s[k++];
        }
    }
    

    您创建了测试[a][b],但在测试[b][a]之前您可以访问