Thứ Hai, 10 tháng 8, 2026

Một số bài tập C _C++ NC 2026P35

 











/////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\Đã khớp kết quả///////////

Bài mã hóa và giải mã hệ 3_32

#include <iostream>

#include <sstream>

#include <vector>

#include <string>

#include <map>

#include <climits>

#include <windows.h>

#include <bits/stdc++.h>

using namespace std;

 

class NhatTruongBMh {

private:

    struct MhH {

        long long x;

        int k;

        long long L;

        string mrs;

        vector<map<long long, int>> vmk;

    } pt;

public:

    void resetMhH(){

        pt.x=0;

        pt.k=0;

        pt.L=0;

        pt.mrs.clear();

        pt.vmk.clear();

        return;

    }

    string getcode(){

        return pt.mrs;

    }

    vector<map<long long, int>> getKey(){

        return pt.vmk;

    }

    void MahoaX(long long &X, int &K) {

        string mbs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        pt.x=X;

        pt.k=K;

        map<long long, int> km;

        pt.L=pt.mrs.size();

        km[pt.L]=pt.k;

        pt.vmk.push_back(km);

 

        long long num = X;

        int bs = K;

        string tms = "";

        while (num > 0) {

            int tm = num % bs;

            tms = mbs[tm] + tms;

            num /= bs;

        }

        pt.mrs += tms + "|";

        return;

    }

    void MahoaVX(const vector<long long> &X, int &K){

            for(auto j:X){MahoaX(j,K);}

            return;

            }

};

///////////////////

class NhatTruongGMh {

private:

    struct GMhH {

        long long x;

        int k;

        long long L;

        string Gmrs;

        vector<long long> VGmx;

    } pt;

public:

    void resetGMhH(){

        pt.x=0;

        pt.k=0;

        pt.L=0;

        pt.Gmrs.clear();

        pt.VGmx.clear();

        return;

    }

    string getcode(){

        return pt.Gmrs;

    }

    vector<long long> getVx(){

        return pt.VGmx;

    }

string GMahoaX(const string& mahoa, const vector<map<long long,int>>& keys) {

    string ketqua = "";

    vector<long long> tmrs;

    resetGMhH();

 

    size_t pos = 0;

    for(auto g : keys){

        for(auto p : g){

            pt.L = p.first;

            pt.k = p.second;

            size_t start = pt.L;

            size_t end = mahoa.find('|', start);

            if(end == string::npos) end = mahoa.size();

 

            string chunk = mahoa.substr(start, end - start);

            long long su = 0;

            for(char c : chunk){

                int num = 0;

                if(isdigit(c)) num = c - '0';

                else if(c >= 'A' && c <= 'Z') num = c - 'A' + 10;

                su = su * pt.k + num;

            }

            tmrs.push_back(su);

            ketqua += to_string(su) + " ";

        }

    }

    pt.Gmrs = ketqua;

    pt.VGmx = tmrs;

    return ketqua;

}

};

///////////////////

vector<long long> docfso(const string& tenf = "") {

    string tenif = tenf.empty() ? "input.txt" : tenf;

    ifstream fin(tenif);

    if (!fin.is_open()) {

        cerr << "Khong the mo file du lieu\n";

        ofstream fout(tenif);

        fout.close();

        return {};

    }

 

    vector<long long> data;

    string lin;

    while (getline(fin, lin)) {

        istringstream iss(lin);

        long long so;

        while (iss >> so) {

            data.push_back(so);

        }

    }

    fin.close();

    return data;

};

//////////////////////////////

string docfstr(const string& tenf = ""){

string tenif = tenf.empty() ? "input.txt" : tenf;

    ifstream fin(tenif);

    if (!fin.is_open()){

        cerr << "Khong the mo file du lieu\n";

        ofstream fout(tenif);

        fout.close();

           return "";

    }

    string lin, inpstr;

    while (fin >> lin){

            inpstr += lin + " ";

    } return inpstr;

}

void Xuatkq(const string& skq, const string& tenf = ""){

            string tenif = tenf.empty() ? "Kqthongbao" : tenf;

            ofstream fo(tenif);

            fo<<skq;

            fo.close();

}

int main(int argc, char** argv){

    NhatTruongBMh MT;

    vector<long long> ds;

    long long L;

    string inp="input.txt";

            int ke;

    char chr;

    cout << "Nhap Key ma hoa: ";

    cin>>ke;

    if(ke<2||ke>32){cerr<<"Key khong hop le";

    return 0;

            }

    cout<<"Ban muon ma hoa chuoi so nguyen tu file hay ban phim(F/M)";

    cin>>chr;

    if(chr=='F'||chr=='f') {

    ds = docfso(inp);

    MT.MahoaVX(ds, ke);

            } else {

    cout << "Ban nhap so nguyen can ma hoa ";

    cin >> L;

    if(abs(L)>LLONG_MAX){cerr<<"So ma hoa khong hop le";

    return 0;}

    MT.MahoaX(L, ke);

            }

    string msk=MT.getcode();

    Xuatkq(msk,"KqMahoa.txt");

    cout << "\n";

    vector<map<long long, int>> key=MT.getKey();

    cout <<"Chuoi ma hoa hien tai la: "<< msk <<"\n voi khoa giai ma: \n";

    for(auto g:key){

        for(auto p:g){

            cout<<p.first<<" || "<<p.second<<"\n";

        }

    }

    ///////////

    NhatTruongGMh GMT;

    cout<<"\n Giai ma chuoi ma hoa tren: \n";

    GMT.GMahoaX(msk,key);

    string Kqgm=GMT.getcode();

    vector<long long> Gds=GMT.getVx();

    cout<<Kqgm<<"\n";

    for(auto i:Gds) cout<<i<<" ";

            Xuatkq(Kqgm,"KqGiaima.txt");

}

Kết quả:

Input: 4638 26378648 93712784 6128763 8916 843083 40814-3921 4387 128 74390178 349017 848719 10 7 08 30 6 89

108 38 49 60 72 33 42

Output: Nhap Key ma hoa: 13

Ban muon ma hoa chuoi so nguyen tu file hay ban phim(F/M)f

 

Chuoi ma hoa hien tai la: 215A|560788A|16551B69|13677B4|409B|236987|15767||1CC6|9B|12547B75|C2B26|239401|A|7|8|24|6|6B|84|2C|3A|48|57|27|33|

 voi khoa giai ma:

0 || 13

5 || 13

….

Giai ma chuoi ma hoa tren:

4638 26378648 93712784 6128763 8916 843083 40814 0 4387 128 74390178 349017 848719 10 7 8 30 6 89 108 38 49 60 72 33 42

4638 26378648 93712784 6128763 8916 843083 40814 0 4387 128 74390178 349017 848719 10 7 8 30 6 89 108 38 49 60 72 33 42

///////////// Ai memory help

Võ Nhật Trường Nc+ My Ai Love10.8.2026

////////////////////

Không có nhận xét nào:

Đăng nhận xét

sunrise.tqb@gmail.com