Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.6 kB
18
Indexable
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_FILE_NUM = 21;

int disk[MAX_FILE_NUM + 5][2];
bool choose[MAX_FILE_NUM + 5];
int File_Count = 0;

bool ans[MAX_FILE_NUM + 5];
int Ans_Total_SZ = 1e9;

int Target_Num;
int Target_SZ;

void exist_del_sol(int Cur_File, int Choosed_Num, int Total_SZ) {
    if (Choosed_Num == Target_Num) {                               // already choose Target_Num files
        if (abs(Total_SZ - Target_SZ) < abs(Ans_Total_SZ - Target_SZ)) {  // beter than answer
            for (int i = 1; i <= MAX_FILE_NUM; i++) ans[i] = choose[i];
            Ans_Total_SZ = Total_SZ;
        }
        return;
    }
    if (Cur_File > MAX_FILE_NUM) return;
    // choose
    choose[Cur_File] = true;
    exist_del_sol(Cur_File + 1, Choosed_Num + 1, Total_SZ + disk[Cur_File][1]);
    // not choose
    choose[Cur_File] = false;
    exist_del_sol(Cur_File + 1, Choosed_Num, Total_SZ);
}

signed main() {
    while (true) {
        cout << "Options: ";
        int opt;
        cin >> opt;
        if (opt == 1) {
            cout << "P lease input file name and file size: ";
            int name, sz;
            cin >> name >> sz;
            for (int i = 1; i <= MAX_FILE_NUM; i++) {
                if (disk[i][0] == 0) {
                    disk[i][0] = name;
                    disk[i][1] = sz;
                    break;
                }
            }
            File_Count++;
            if (File_Count == MAX_FILE_NUM) {
                cout << "Hard drive exceeds its capacity, please enter the number of files to be deleted: ";
                cin >> Target_Num >> Target_SZ;
                Ans_Total_SZ = 1e9;
                exist_del_sol(1, 0, 0);
                int output_number = 0;
                for (int i = 1; i <= MAX_FILE_NUM; i++) {
                    if (ans[i] == true) {
                        cout << disk[i][0] << ' ';    // print file name
                        disk[i][0] = disk[i][1] = 0;  // delete file
                        File_Count--;
                    }
                }
                cout << '\n';
            }
        } else if (opt == 2) {
            cout << "Please input the file name: ";
            int target;
            cin >> target;
            bool exist = false;
            for (int i = 1; i <= MAX_FILE_NUM; i++) {
                if (disk[i][0] == target) {
                    exist = true;
                    break;
                }
            }
            if (exist) cout << "YES\n";
            else cout << "NO\n";
        } else {
            break;
        }
    }
}