[ create a new paste ] login | about

Link: http://codepad.org/JE3I3AWL    [ raw code | output | fork ]

C++, pasted on Jul 1:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

int main(){
    string str;
    size_t row = 0;
    size_t column = 0;

    ifstream in("input.txt");
    ofstream out("output.txt");
    getline(in,str);
    stringstream ss(str);
    ss >> row ; ss >> column;
    out << "So dong : " << row << "  So cot : " << column;
    out << "\n-----------------Kho hang ban dau-----------------------\n\n";
    unsigned int **p = new unsigned int*[row];
    p[0] = new unsigned int[row*column];
    for(size_t k = 1; k < row; k ++)
        p[k] = p[k - 1] + column;

    for(size_t i = 0; i < row ; i ++){
        {
            getline(in,str);
            stringstream so(str);
            for(size_t j = 0; j < column; j ++ ){
                so >> p[i][j];
                out << p[i][j] << "  ";
            }
            out << "\n";
        }
    }
    in.close();

    unsigned int *rMax = new unsigned int [row];
    unsigned int *cMax = new unsigned int [column];

    for(size_t i = 0; i < row ; i ++){
        unsigned int rM = p[i][0];
        for(size_t j = 0; j < column ; j ++){
            if(p[i][j] > rM)
                rM = p[i][j];
        }
         rMax[i] = rM;
    }

    for(size_t i = 0 ; i < column; i ++){
        unsigned cM = p[0][i];
        for(size_t j = 0; j < row; j ++){
            if(p[j][i] > cM)
                cM = p[j][i];
        }
        cMax[i] = cM;
    }

    bool ** b = new bool *[row];
    b[0] = new bool[row * column];
    for(size_t i = 1; i < row; i ++)
        b[i] = b[i - 1] + column;

    for(size_t i = 0; i < row; i ++)
        for(size_t j = 0; j < column; j ++)
            b[i][j] = true;

    bool *unCheckRow = new bool [row];
    bool *unCheckColumn = new bool [column];

    for(size_t i = 0; i < row; i ++)
        unCheckRow[i] = true;

    for(size_t j = 0; j < column; j ++)
        unCheckColumn[j] = true;

    for(size_t i = 0; i < row; i ++)
        for(size_t j = 0; j < column; j ++)
            if((p[i][j] == rMax[i]) && (p[i][j] == cMax[j]) && unCheckRow[i] && unCheckColumn[j]){
                unCheckRow[i] = false;
                unCheckColumn[j] = false;
                b[i][j] = false;
            }

    for(size_t i = 0; i < row; i ++){
        for(size_t j = 0; j < column; j ++){
            if((p[i][j] == rMax[i]) && unCheckRow[i]){
                unCheckRow[i] = false;
                b[i][j] = false;
            }
        }
    }

    for(size_t i = 0; i < column; i ++){
        for(size_t j = 0; j < row; j ++){
            if(p[j][i] == cMax[i] && unCheckColumn[i]){
                unCheckColumn[i] = false;
                b[j][i] = false;
            }
        }
    }

    for(size_t i = 0; i < row ; i ++)
        for(size_t j = 0; j < column; j ++)
            if(p[i][j] < 2)
                b[i][j] = false;

    out << "\n-----------------Kho hang sau khi trom-----------------\n";

    static unsigned int count = 0;

    for(size_t i = 0; i < row; i ++){
        for(size_t j = 0; j < column; j ++)
            if(b[i][j]){
                count += p[i][j]  - 1;
                out << "1  ";
            }
            else{
                out << p[i][j] << "  ";
            }
        out << "\n";
    }

    out << "\n-----------------So luong hang bi mat-------------------\n";
    out << "Trom lay duoc : " << count << " thung hang.";

    out.close();

    delete[] p[0];
    delete[] p;
    delete[] b[0];
    delete[] b;
    delete[] unCheckRow;
    delete[] unCheckColumn;
    delete[] rMax;
    delete[] cMax;
    return 0;
}


Output:
1
2
3
memory clobbered past end of allocated block

Exited: ExitFailure 127


Create a new paste based on this one


Comments: