CCF历年题目解析C++实现(简单题)2013-2022

目录

2013.12

2013.12.1 出现次数最多的数

2013.12.2  ISBN号码

2014.03

2014.03.1  相反数

2014.03.2  窗口

2014.09

2014.09.1  相邻数对

2014.09.2  画图

2014.09.3  字符串匹配 

2014.12

2014.12.1  门禁系统

2014.12.2  Z字形扫描

2015.03

2015.03.1  图像旋转

2015.03.2  数字排序

2015.03.3  节日

2015.09

2015.09.1  数列分段

2015.09.2  日期计算

2015.12

2015.12.1  数位之和

2015.12.2  消除类游戏

2016.04

2016.04.1  折点计数

2016.04.2

2016.09

2016.09.1  最大波动

2016.09.2  火车购票

2016.12        

2016.12.1  中间数

2016.12.2  

2017.03       

2017.03.1  分蛋糕

 2017.03.2  学生排队

2017.09        

2017.09.1  打酱油

2017.09.2

2017.12

2017.12.1  最小差值

2017.12.2  游戏

2018.03

2018.03.1  跳一跳

2018.03.2

2018.09        

2018.09.1  卖菜

2018.09.2  买菜

2018.12

2018.12.1  小明上学

2018.12.2  小明放学

2019.03                

2019.03.1  小中大

2019.03.2  二十四点


2013.12

2013.12.1 出现次数最多的数

#include <iostream>

using namespace std;

int count[10000] = {0};

int main() {
    int n;
    cin >> n;

    int arr[1000] = {0};

    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    int min = arr[0];
    int minCount = 0;

    for (int i = 0; i < n; ++i) {
        count[arr[i]]++;
        if (count[arr[i]] > minCount) {
            minCount = count[arr[i]];
            min = arr[i];
        } else if (count[arr[i]] == minCount) {
            if (min <= arr[i]) {
                continue;
            } else {
                min = arr[i];
            }
        } else {
            continue;
        }
    }

    cout << min << endl;
}

2013.12.2  ISBN号码

#include <iostream>
#include <cstring>

using namespace std;

void toNum(string &str, int number[20], int &len) {        //len记录number的长度

    for (int i = 0; i < str.length(); ++i) {
        if (str[i] == '-') {
            continue;
        } else {
            if (str[str.length() - 1] == 'X' && (i == str.length() - 1)) {
                number[len++] = 10;
                break;
            }
            number[len++] = str[i] - '0';
        }
    }
}

void ISBN(int number[], int len) {
    int sum = 0;
    int standard = 0;
    for (int i = 0; i < len - 1; ++i) {
        sum += number[i] * (i + 1);
    }

    standard = sum % 11;

    if (standard == number[len - 1]) {
        cout << "Right" << endl;
    } else {
        cout << number[0] << "-";
        for (int i = 1; i < 4; ++i) {
            cout << number[i];
        }
        cout << "-";
        for (int i = 4; i < 9; ++i) {
            cout << number[i];
        }

        if (standard == 10) {
            cout << "-" << "X" << endl;
        } else {
            cout << "-" << standard << endl;
        }
    }
}

int main() {
    string str;
    getline(cin, str);

    int number[20] = {0};
    int len = 0;

    toNum(str, number, len);
    ISBN(number, len);
}

2014.03

2014.03.1  相反数

#include <iostream>

const int SIZE = 500;

using namespace std;

int reverseCount(int numArr[], int N) {
    int count = 0;
    for (int i = 0; i < N; ++i) {
        for (int j = i + 1; j < N; ++j) {
            if (numArr[i] == -numArr[j]) {
                count++;
                break;
            }
        }
    }

    return count;
}
int main() {
    int N;
    cin >> N;

    int numArr[SIZE] = {0};

    for (int i = 0; i < N; ++i) {
        cin >> numArr[i];
    }

    int count = reverseCount(numArr, N);
    cout << count << endl;
}

2014.03.2  窗口

#include <iostream>
#include <vector>

using namespace std;

struct Window {
    int id;
    int x1, y1, x2, y2;
};

// 判断点(x, y)是否在矩形内部
bool isInside(Window w, int x, int y) {
    return (x >= w.x1 && x <= w.x2 && y >= w.y1 && y <= w.y2);
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<Window> windows(n);
    for (int i = 0; i < n; i++) {
        cin >> windows[i].x1 >> windows[i].y1 >> windows[i].x2 >> windows[i].y2;
        windows[i].id = i + 1;  // 为每个窗口赋一个编号
    }

    while (m--) {
        int x, y;
        cin >> x >> y;

        bool found = false;  // 是否找到了对应的窗口
        for (int i = n - 1; i >= 0; i--) {  // 从最上层的窗口开始遍历
            if (isInside(windows[i], x, y)) {
                cout << windows[i].id << endl;
                // 将当前窗口移到数组最前面
                Window tmp = windows[i];
                for (int j = i; j < n - 1; j++) {
                    windows[j] = windows[j + 1];
                }
                windows[n - 1] = tmp;
                found = true;
                break;
            }
        }

        if (!found) {
            cout << "IGNORED" << endl;
        }
    }

    return 0;
}

2014.09

2014.09.1  相邻数对

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;

    int arr[n];

    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    int count = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (arr[j] - arr[i] == 1 || arr[j] - arr[i] == -1) {
                count++;
            }
        }
    }

    cout << count << endl;
}

2014.09.2  画图

#include <iostream>

using namespace std;

/*struct Picture {
    int x1, y1, x2, y2;
};*/

int main() {
    int draw[100][100] = {0};
    int n;
    cin >> n;

    int x1, y1, x2, y2;
    int count = 0;
    for (int i = 0; i < n; ++i) {
        cin >> x1 >> y1 >> x2 >> y2;
        for (int j = x1; j < x2; ++j) {
            for (int k = y1; k < y2; ++k) {
                if (draw[j][k] == 1) {
                    continue;
                } else {
                    draw[j][k] = 1;
                    count++;
                }

            }
        }

    }
    cout << count << endl;
    return 0;
}

2014.09.3  字符串匹配 

#include <iostream>
#include <string>

using namespace std;

string isChange(string str, int opt) {
    if (opt) {
        return str;
    } else {
        for (int i = 0; i < str.length(); ++i) {
            if (str[i] >= 'a' && str[i] <= 'z') {
                str[i] -= 32;
            }
        }

        return str;
    }
}

/*void search(string str1, string str2, string str3) {
    for (int i = 0; i < str2.length() - str1.length() + 1; ++i) {
        if (str1 == str2.substr(i, str1.length())) {
            cout << str3 << endl;
            break;
        }
    }
}*/

//两种思路,注释的地方自己忘了find可以直接这样用。

void search(string str1, string str2, string str3) {
    if (str2.find(str1) != string::npos) {
        cout << str3 << endl;
        return;
    }
}


int main() {
    string string1[100];
    int opt;
    int n;
    string S;

    cin >> S;
    cin >> opt;
    cin >> n;

    string SS = isChange(S, opt);
    for (int i = 0; i < n; ++i) {
        cin >> string1[i];
    }

    for (int i = 0; i < n; ++i) {
        string temp = isChange(string1[i], opt);
        search(SS, temp, string1[i]);
    }

    return 0;
}

2014.12

2014.12.1  门禁系统

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;

    int arrID[n];
    int count[1000] = {0};

    for (int i = 0; i < n; ++i) {
        cin >> arrID[i];
        if (count[arrID[i]] == 0) {
            count[arrID[i]]++;          //也可以在初始化阶段用for给count先全部赋值为1
        }
    }

    for (int i = 0; i < n; ++i) {
        cout << count[arrID[i]]++ << " ";
    }
    cout << endl;
}

2014.12.2  Z字形扫描

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int arr[n][n];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> arr[i][j];
        }
    }

    int i = 0;
    int j = 0;
    int k = 0;      //控制扫描方向
    while (i < n && j < n) {
        if (i == n - 1 && j == n - 1) {
            cout << arr[i][j] << " ";
            break;
        }

        if (i == 0 && j != n -1) {
            cout << arr[i][j] << " ";
            j++;
            k++;
        } else if (j == 0 && i != n - 1) {
            cout << arr[i][j] << " ";
            i++;
            k++;
        } else if (i == n - 1) {
            cout << arr[i][j] << " ";
            j++;
            k++;
        } else if (j == n - 1) {
            cout << arr[i][j] << " ";
            i++;
            k++;
        }

        if (k % 2 == 0) {
            cout << arr[i][j] << " ";
            i--;
            j++;
        } else if (k % 2 == 1) {
            cout << arr[i][j] << " ";
            i++;
            j--;
        }
    }
    cout << endl;
}

2015.03

2015.03.1  图像旋转

#include <iostream>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    int arr[n][m];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> arr[i][j];
        }
    }



    int temp[m][n];
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            temp[m - i - 1][j] = arr[j][i];
        }
    }

    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            cout << temp[i][j] << " ";
        }
        cout << endl;
    }

}

2015.03.2  数字排序

        只有90分,不知道是哪里的原因。 

#include <iostream>
#include <queue>

using namespace std;

int arr[1000] = {0};
int count[1000] = {0};

struct Num {
    int value;
    int countNum;
};

priority_queue<Num> myQueue;

bool operator<(const Num &lhs, const Num &rhs) {
    if (lhs.countNum != rhs.countNum) {
        return lhs.countNum < rhs.countNum;
    } else {
        return lhs.value > rhs.value;
    }
}

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        count[arr[i]]++;
    }

    int len = 0;
    Num *num = new Num[n];
    for (int i = 0; i < 1000; ++i) {
        if (count[i] != 0) {
            num[len].value = i;
            num[len].countNum = count[i];
            len++;
        }
    }

    for (int i = 0; i < len; ++i) {
        myQueue.push(num[i]);
    }

    for (int i = 0; i < len; ++i) {
        cout << myQueue.top().value << " " << myQueue.top().countNum << endl;
        myQueue.pop();
    }

}

2015.03.3  节日

#include <iostream>
#include <cstdio>

using namespace std;

bool isLeap(const int &year) {
    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
        return true;
    } else {
        return false;
    }
}

int mons[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
    int a, b, c, y1, y2;
    cin >> a >> b >> c >> y1 >> y2;

    int beginYear = 1850;
    int beginMon = 1;
    int beginDay = 1;
    int beginC = 2;         //表示为周几

    int flag = 0;//表示是第几个星期
    for (int i = y1; i <= y2;) {

        if (beginYear == i && beginMon == a && ((beginC - 1) % 7 + 1) == c) {
            flag++;
        }
        if (beginYear == i && beginMon == a && flag == b && ((beginC - 1) % 7 + 1) == c) {
            printf("%04d/%02d/%02d\n", beginYear, beginMon, beginDay);
            i++;
            flag = 0;
        } else if (beginYear > i) {
            cout << "none" << endl;
            i++;
            flag = 0;
        }

        if (isLeap(beginYear)) {
            mons[2] = 29;
        } else {
            mons[2] = 28;
        }
        beginDay++;
        beginC++;
        if (beginDay > mons[beginMon]) {
            beginDay = 1;
            beginMon++;
            if (beginMon > 12) {
                beginMon = 1;
                beginYear++;
            }
        }


    }

    return 0;
}

2015.09

2015.09.1  数列分段

#include <iostream>

const int MAX = 1000;

using namespace std;

int main() {
    int n;
    cin >> n;

    int arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    int count = 0;
    for (int i = 1; i < n; ) {
        if (i == n - 1) {
            if (arr[i] == arr[i - 1]) {
                count++;
                break;
            } else {
                count+=2;
                break;
            }
        }
        if (arr[i] == arr[i - 1]) {
            i++;
        } else {
            count++;
            i++;
        }
    }

    cout << count << endl;
}

2015.09.2  日期计算

#include <iostream>

using namespace std;

bool isLeap(int year) {
    if (year & 400 == 0 || year % 4 == 0 && year % 100 != 0) {
        return true;
    } else {
        return false;
    }
}

int main() {
    int mons[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int y, m, d;
    int tempDay = 0;
    int tempMon = 1;
    cin >> y;
    cin >> d;

    if (isLeap(y)) {
        mons[2] = 29;
    } else {
        mons[2] = 28;
    }

    while (d > 0) {
        d--;
        tempDay++;
        if (tempDay > mons[tempMon]) {
            tempDay = 1;
            tempMon++;
            if (tempMon > 12) {
                y++;
                tempMon = 1;
            }
        }
    }

    cout << tempMon << endl;
    cout << tempDay << endl;
}

2015.12

2015.12.1  数位之和

#include <iostream>

using namespace std;

int sumNum(const long int &number, int &sum) {
    int remain = 0;
    if (number == 0) {
        return 0;
    } else {
        remain = number % 10;
        sum += remain;
        sumNum(number / 10, sum);
        return sum;
    }
}

int main() {
    int sum = 0;
    long int number;
    cin >> number;

    cout << sumNum(number, sum);
}

2015.12.2  消除类游戏

        这个代码写的一塌糊涂,估计除了我,别人读起来都会很费力,嵌套循环太多,阅读性不强,这个阶段自己还是把题目做出来就好,先通过眼下的复试。

        改进:当前的算法是通过遍历整个棋盘来寻找相同的元素并将其删除,这种方法在效率上并不是最优的。可以考虑使用更好的算法来解决这个问题。例如,使用递归来检查所有相同的元素,并将它们删除。这种方法的时间复杂度可能更小,效率更高。(chatGPT给出的意见,待到以后有时间再看看吧)

#include <iostream>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    int chess[n][m];
    int chessBack[n][m];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> chess[i][j];
            chessBack[i][j] = chess[i][j];
        }
    }

    int countI = 0;
    int countJ = 0;
    bool flagI = false;
    bool flagJ = false;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            while (true) {
                if (chess[i][j] == chess[i][j + countJ] && (j + countJ) < m) {
                    countJ++;
                } else {
                    if (countJ >= 3) {
                        flagJ = true;
                    } else {
                        flagJ = false;
                    }

                    if (chess[i][j] == chess[i + countI][j] && (i + countI) < n) {
                        countI++;
                    } else {
                        if (countI >= 3) {
                            flagI = true;
                        } else {
                            flagI = false;
                        }

                        if (flagJ) {
                            for (int k = 0; k < countJ; ++k) {
                                chessBack[i][j + k] = 0;
                            }
                        }

                        if (flagI) {
                            for (int k = 0; k < countI; ++k) {
                                chessBack[i + k][j] = 0;
                            }
                        }
                        break;

                    }

                }
            }

            flagI = flagJ = false;
            countI = countJ = 0;
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cout << chessBack[i][j] << " ";
        }
        cout << endl;
    }
}

2016.04

2016.04.1  折点计数

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;

    int arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    int count = 0;
    for (int i = 1; i < n; ++i) {
        if (i + 1 < n) {
            //异号相乘得负数
            if ((arr[i - 1] - arr[i]) * (arr[i] - arr[i + 1]) < 0) {
                count++;
            }
        }
    }

    cout << count << endl;
}

2016.04.2

没做出来,其他博主的答案

2016.09

2016.09.1  最大波动

#include <iostream>

using namespace std;

void maximum(int arr[], int n) {
    int max = abs(arr[1] - arr[0]);
    for (int i = 1; i < n; ++i) {
        if (abs(arr[i] - arr[i - 1]) > max) {
            max = abs(arr[i] - arr[i - 1]);
        }
    }
    cout << max << endl;
}

int main() {
    int n;
    cin >> n;

    int arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    maximum(arr, n);

}

2016.09.2  火车购票

我这个方法复杂度比较高,不是最优的解法。

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int chooseSeat[n];
    int seatArr[20][5] = {0};

    for (int i = 0; i < n; ++i) {
        cin >> chooseSeat[i];
    }

    for (int i = 0; i < n; ++i) {
        bool flag = false;
        for (int j = 0; j < 20; ++j) {
            int count = 0;
            for (int k = 0; k < 5; ++k) {
                if (seatArr[j][k] == 0) {
                    count++;
                }
                if (chooseSeat[i] == count) {
                    count = 0;
                    for (int l = 0; l < 5; ++l) {
                        if (seatArr[j][l] == 0) {
                            count++;

                            seatArr[j][l] = 1;
                            cout << j * 5 + l + 1 << " ";
                            if (count == chooseSeat[i]) {
                                break;
                            }
                        }
                    }
                    flag = true;
                    if (flag) {
                        break;
                    }
                }
            }
            if (flag) {
                break;
            }
        }

        if (!flag) {
            for (int j = 0; j < 20; ++j) {
                for (int k = 0; k < 5; ++k) {
                    if (seatArr[j][k] == 0) {
                        seatArr[j][k] = 1;
                        cout << j * 5 + k + 1 << " ";
                    }
                }
            }
        }

        cout << endl;
    }
}

2016.12        

2016.12.1  中间数

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;

    int num[n];
    for (int i = 0; i < n; ++i) {
        cin >> num[i];
    }

    for (int i = 0; i < n; ++i) {
        int midNum = num[i];
        int countBig = 0;
        int countMin = 0;
        for (int j = 0; j < n; ++j) {
            if (num[j] > midNum) {
                countBig++;
            } else if (num[j] < midNum) {
                countMin++;
            }
        }
        if (countBig == countMin) {
            cout << midNum << endl;
            break;
        }

        if (i == n - 1) {
            cout << "-1" << endl;
        }
    }
}

2016.12.2  

没做出来,其他博主的答案

2017.03       

2017.03.1  分蛋糕

#include <iostream>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    int arr[n];

    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    int count = 0;

    for (int i = 0; i < n; ++i) {
        if (arr[i] >= k) {
            arr[i] = 0;
            count++;
        } else {
            int temp = 0;
            for (int j = 0; j < n - i; ++j) {
                temp += arr[i + j];
                arr[i + j] = 0;
                if (temp >= k) {
                    count++;
                    i = i + j;
                    break;
                }

                if (j == n - i - 1) {
                    count++;
                    i = i + j;
                }
            }
        }
    }

    cout << count << endl;

}

 2017.03.2  学生排队

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int m;
    cin >> m;

    int studentArr[n + 1];
    for (int i = 0; i <= n; ++i) {
        studentArr[i] = i;
    }

    int p, q;
    for (int i = 0; i < m; ++i) {
        cin >> p >> q;
        for (int j = 1; j < n + 1; ++j) {
            if (studentArr[j] == p) {
//                int temp = p;
                if (q > 0) {
                    for (int k = j; k < j + q; ++k) {
                        studentArr[k] = studentArr[k + 1];
                    }
                    studentArr[j + q] = p;
                    break;
                } else {
                    for (int k = j; k > j + q; --k) {
                        studentArr[k] = studentArr[k - 1];
                    }
                    studentArr[j + q] = p;
                    break;
                }

            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        cout << studentArr[i] << " ";
    }
    cout << endl;
}

2017.09        

2017.09.1  打酱油

#include <iostream>

using namespace std;

int main() {
    int N;
    int count = 0;
    cin >> N;

    if (N / 50 > 0) {
        int i = N / 50;
        count = i * 7;
        N = N % 50;
    }

    if (N / 30 > 0) {
        int i = N / 30;
        count += i * 4;
        N = N % 30;
    }

    if (N / 10 > 0) {
        int i = N / 10;
        count += i;
    }

    cout << count << endl;
}

2017.09.2

不会

2017.12

2017.12.1  最小差值

#include <iostream>

using namespace std;

int main() {
    int min = 0;
    int n;
    cin >> n;

    int arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    min = abs(arr[1] - arr[0]);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (abs(arr[j] - arr[i]) < min) {
                min = abs(arr[j] - arr[i]);
            }
        }
    }

    cout << min << endl;
}

2017.12.2  游戏

#include <iostream>
#include <queue>

using namespace std;

struct Student {
    int id;
    int number;
};

int main() {
    int n, k;
    cin >> n >> k;
    Student *student = new Student[1000];
    queue<Student> myQueue;
    for (int i = 0; i < n; ++i) {
        student[i].id = i + 1;
        student[i].number = 0;
        myQueue.push(student[i]);
    }

    int addNumber;
    while (myQueue.size() > 1) {
        myQueue.front().number = addNumber;
        if (myQueue.front().number % k == 0 || myQueue.front().number % 10 == k) {
            addNumber = myQueue.front().number + 1;
            myQueue.pop();
        } else {
            Student temp = myQueue.front();
            addNumber = myQueue.front().number + 1;
            myQueue.pop();
            myQueue.push(temp);
        }
    }

    cout << myQueue.front().id << endl;
}

2018.03

2018.03.1  跳一跳

#include <iostream>

using namespace std;

int main() {
    int arr[30] = {0};
    int grade = 0;
    int isContinue = 0;
    for (int i = 0; i < 30; ++i) {
        cin >> arr[i];
        if (arr[i] == 0) {
            break;
        }

        if (i == 0) {
            if (arr[i] == 2) {
                isContinue += 2;
                grade += isContinue;
            } else {
                isContinue = 0;
                grade += 1;
            }

            continue;
        }

        if (arr[i] == 2) {
            if (isContinue != 0) {
                isContinue += 2;
            } else {
                isContinue = 2;
            }
            grade += isContinue;
        } else {
            grade += 1;
            isContinue = 0;
        }
    }

    cout << grade;


}

2018.03.2

不会

2018.09        

2018.09.1  卖菜

#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    int after[n];
    for (int i = 0; i < n; ++i) {
        if (i == 0) {
            after[0] = (arr[0] + arr[1]) / 2;
        } else if (i == n - 1) {
            after[n - 1] = (arr[n - 1] + arr[n - 2]) / 2;
        } else {
            after[i] = (arr[i] + arr[i - 1] + arr[i + 1]) / 3;
        }
    }

    for (int i = 0; i < n; ++i) {
        cout << after[i] << " ";
    }
}

2018.09.2  买菜

#include <iostream>

using namespace std;

int H[1000000] = {0};
int W[1000000] = {0};

int main() {
    int n;
    cin >> n;
    int a, b;
    for (int i = 0; i < n; ++i) {
        cin >> a >> b;
        for (int j = a; j < b; ++j) {
            H[j] = 1;
        }
    }
    for (int i = 0; i < n; ++i) {
        cin >> a >> b;
        for (int j = a; j < b; ++j) {
            W[j] = 1;
        }
    }

    int emptyTime = 0;
    for (int i = 0; i < 1000000; ++i) {
        if (H[i] == 1 && W[i] == 1) {
            emptyTime++;
        }
    }

    cout << emptyTime << endl;
}

2018.12

2018.12.1  小明上学

#include <iostream>

using namespace std;

int main() {
    int r, y, g;
    cin >> r >> y >> g;

    int n;
    cin >> n;

    int count = 0;
    int k;
    int t;
    for (int i = 0; i < n; ++i) {
        cin >> k >> t;
        if (k == 0) {
            count += t;
        } else if (k == 1) {
            count += t;
        } else if (k == 2) {
            count += t + r;
        } else if (k == 3) {
            continue;
        }
    }

    cout << count << endl;
}

2018.12.2  小明放学

#include <iostream>

using namespace std;

int main() {
    int r, y, g;
    cin >> r >> y >> g;
    int n;
    cin >> n;
    int k, t;
    long long int countTime = 0;
    int light[r + g + y];
    for (int i = 0; i < g; ++i) {
        light[i] = 3;
    }
    for (int i = g; i < g + y; ++i) {
        light[i] = 2;
    }
    for (int i = g + y; i < g + y + r; ++i) {
        light[i] = 1;
    }

    for (int i = 0; i < n; ++i) {
        cin >> k >> t;
        if (k == 0) {
            countTime += t;
        } else {
            int temp;
            if (k == 1) {
                temp = (countTime + (g + y + r - t)) % (r + g + y);
            } else if (k == 2) {
                temp = (countTime + (g + y - t)) % (r + g + y);
            } else if (k == 3) {
                temp = (countTime + (g - t)) % (r + g + y);
            }
            k = light[temp];
            switch (k) {
                case 1:
                    t = g + r + y - temp;
                    countTime += t;
                    break;
                case 2:
                    t = g + y - temp + r;
                    countTime += t;
                    break;
                case 3:
                    break;
            }
        }
    }

    cout << countTime << endl;
    return 0;
}

2019.03                

2019.03.1  小中大

#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    int max, min;

    if (arr[0] < arr[n - 1]) {
        min = arr[0];
        max = arr[n - 1];
    } else {
        min = arr[n - 1];
        max = arr[0];
    }

    if (n % 2 != 0) {
        printf("%d %d %d\n", max, arr[n / 2], min);
    } else {
        float mid = (float)(arr[(n - 1) / 2] + arr[n / 2]) / 2;
        if ((int)mid != mid){
            printf("%d %.1f %d\n", max, mid, min);
        } else {
            printf("%d %d %d\n", max, (int)mid, min);
        }

    }

}

2019.03.2  二十四点

#include <iostream>
#include <string>
#include <map>
#include <stack>

using namespace std;

map<char, int> priorityMap = {
        {'+', 1},
        {'-', 1},
        {'x', 2},
        {'/', 2}
};

stack<int> numStack;
stack<char> operatorStack;

void figureSum(stack<int> &num, stack<char> &oper) {
    int sum = 0;
    int rhs = num.top();
    num.pop();
    int lhs = num.top();
    num.pop();

    switch (oper.top()) {
        case '+':
            sum = lhs + rhs;
            operatorStack.pop();
            break;
        case '-':
            sum = lhs - rhs;
            operatorStack.pop();
            break;
        case 'x':
            sum = lhs * rhs;
            operatorStack.pop();
            break;
        case '/':
            sum = lhs / rhs;
            operatorStack.pop();
            break;
    }

    num.push(sum);
}

int main() {
    int n;
    cin >> n;
    string str[n];
    for (int i = 0; i < n; ++i) {
        cin >> str[i];
    }

    for (int i = 0; i < n; ++i) {
        string example = str[i];
        for (int j = 0; j < 7; ++j) {
            if (example[j] >= '1' && example[j] <= '9') {
                numStack.push((int)(example[j] - '0'));
            } else {
                if (operatorStack.empty()) {
                    operatorStack.push(example[j]);
                } else {
                    if (priorityMap[operatorStack.top()] >= priorityMap[example[j]]) {
                        figureSum(numStack, operatorStack);
                        operatorStack.push(example[j]);
                    } else {
                        operatorStack.push(example[j]);
                    }
                }

            }
        }

        while (!operatorStack.empty()) {
            figureSum(numStack, operatorStack);
        }

        if (numStack.top() == 24) {
            cout << "Yes" << endl;
            numStack.pop();
        } else {
            cout << "No" << endl;
            numStack.pop();
        }

    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值