vendredi 25 mars 2022

signal: killed / std::bad_alloc What is making it order a kill

This code is meant to solve this Question. The Results show me getting test case 2&3 right but all the other cases provided a "std::bad alloc" and even when i run the cases on my side it says "Killed" in terminal! I have no idea what the problem is.

//Includes-------------------------------------------
#include <iostream>     //for using cout
#include <cmath>//for math
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <locale>
#include <vector>

using namespace std;
//Includes-------------------------------------------

//var------------------------------------------------
int n,k,inp,nhold,plates,platesb,platesc,moves;
vector<int> s1,s2,s3;
//var------------------------------------------------

void complete(){
    cout<<"\n WORKED!🍻\n";
}

void plateexe(){
    while(1!=0){
        platesb=(s2.size())-1;
        platesc=(s3.size())-1;
        if(s3[platesc]==1){
            moves++;
            s1.push_back(s3[platesc]);
            s3.pop_back();
        }
        if(s2[platesb]==1){
            moves++;
            s1.push_back(s2[platesb]);
            s2.pop_back();
        }
        if(s2[platesb]!=1){
            if(s3[platesc]!=1){
                break;
            }
        }
    }
}

int main(){
    cin>>n;
    nhold=n;
    while(n!=0){
        n--;
        cin>>inp;
        s1.push_back(inp);
    }
    n=nhold;
    s2.push_back(moves);
    s3.push_back(moves);
    while(n!=0){
        plates=(s1.size())-1;
        platesb=(s2.size())-1;
        platesc=(s3.size())-1;
        n--;
        if(s1[plates]==3){
            if(s3[platesc]==1){
                while(s3[platesc]==1){
                    moves++;
                    s2.push_back(s3[platesc]);
                    s3.pop_back();
                }
                moves++;
                s3.push_back(s1[plates]);
                s1.pop_back();
            }else{
                moves++;
                s3.push_back(s1[plates]);
                s1.pop_back();
            }
        }else if(s1[plates]==2){
            if(s2[platesb]==1){
                while(s2[platesb]==1){
                    moves++;
                    s3.push_back(s2[platesb]);
                    s2.pop_back();
                }
                moves++;
                s2.push_back(s1[plates]);
                s1.pop_back();
            }else{
                moves++;
                s2.push_back(s1[plates]);
                s1.pop_back();
            }
        }else if(s1[plates]==1){
            if(s1[plates-1]==2){
                moves++;
                s3.push_back(s1[plates]);
                s1.pop_back();
            }else if(s1[plates-1]==3){
                moves++;
                s2.push_back(s1[plates]);
                s1.pop_back();
            }else{
                if(plates!=0){
                    moves++;
                    s2.push_back(s1[plates]);
                    s1.pop_back();
                }else{
                    if(s1[plates]==1){
                        s2.push_back(s1[plates]);
                        s1.pop_back();
                        platesb=(s2.size())-1;
                        s1.push_back(s2[platesb]);
                        s2.pop_back();
                    }
                }
            }
        }
    }
    plateexe();
    cout<<moves;
}

`

When I was writing it i hadn't added added the code below:

if(plates!=0){
                    moves++;
                    s2.push_back(s1[plates]);
                    s1.pop_back();
                }else{
                    if(s1[plates]==1){
                        s2.push_back(s1[plates]);
                        s1.pop_back();
                        platesb=(s2.size())-1;
                        s1.push_back(s2[platesb]);
                        s2.pop_back();
                    }
                }

I gave me a simple not right answer but good for 2&3 but now its giving me this bad alloc error/ signal: killed.

Aucun commentaire:

Enregistrer un commentaire