201903-1(小中大),201903-2(二十四点)

//201903-1
//分别对不同情况进行分析
//n的奇偶决定不同的中位数
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    scanf("%d", &n);
    int arr[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    sort(arr, arr + n);

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

    }

    return 0;
}

//201903-1
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    scanf("%d", &n);
    vector<int> v(n);
    for (int i = 0; i < n; i++)
        scanf("%d", &v[i]);
    sort(v.begin(), v.end());
    n % 2 != 0 ?
    printf("%d %d %d\n", v[n - 1], v[n / 2], v[0]) :
    (v[n / 2] + v[n / 2 - 1]) % 2 == 0 ?
    printf("%d %d %d\n", v[n - 1], (v[n / 2] + v[n / 2 - 1]) / 2, v[0]) :
    printf("%d %.1f %d\n", v[n - 1], (v[n / 2] + v[n / 2 - 1]) / 2.0, v[0]);

    return 0;
}

//201903-2
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    scanf("%d", &n);

    //存储结果
    deque<string> final;
    while (n--) {
        string str;
        cin >> str;

        //存储运算符
        deque<char> c;
        //存储数字
        deque<int> d;

        //将运算符和数字分别存储到动态数组中
        for (int i = 0; i < str.size(); i++)
            if (i == 1 || i == 3 || i == 5)
                c.emplace_back(str[i]);
            else
                d.emplace_back(str[i] - '0');

        int count = 3;//起始为3个运算符
        for (int i = 0; i < count; i++)
            if (c[i] == 'x' || c[i] == '/') {

                if (c[i] == 'x')
                    d[i] = d[i] * d[i + 1];
                else
                    d[i] = d[i] / d[i + 1];

                //将下一个运算符和下一个数字前移
                for (int j = i + 1; j < count; j++)
                    c[j - 1] = c[j], d[j] = d[j + 1];
                //因为运算符和下一个数字的前移,所以对应的原本的运算符个数和遍历的下标要进行减减
                count--, i--;
            }

        //若当前全部运算都是乘或除
        //则所有运算结果都会存储到d[0]
        int result = d[0];
        for (int i = 0; i < count; i++)
            if (c[i] == '+')
                result = d[i] + d[i + 1], d[i + 1] = result;
            else
                result = d[i] - d[i + 1], d[i + 1] = result;

        result == 24 ? final.emplace_back("Yes") : final.emplace_back("No");
    }

    for (auto it:final)
        printf("%s\n", it.c_str());
    return 0;
}

//201903-2
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    scanf("%d", &n);
    deque<char> d;
    while (n--) {
        string str;
        cin >> str;
        for (int i = 0; i < str.size(); i++) {
            if (str[i] == 'x' || str[i] == '/') {
                int num1, num2;
                string temp;
//                取数据
                while (!d.empty())
                    if (d.back() != '+' && d.back() != '-') {
                        temp.insert(temp.begin(), d.back());
                        d.pop_back();
                    } else
                        break;

//                将字符串转换为数字
                num1 = atoi(temp.c_str());
                num2 = str[i + 1] - '0';
                temp = to_string(str[i++] == 'x' ? num1 * num2 : num1 / num2);
                for (int j = 0; j < temp.size(); j++)
                    d.emplace_back(temp[j]);
//                for (int i = 0; i < temp.size(); i++)
//                    num1 = (temp[i] - '0') + num1 * (i * 10);
            } else
                d.emplace_back(str[i]);
        }


        deque<string> s;
        vector<char> c;
        string temp;
        while (!d.empty())
            if (d.front() != '+' && d.front() != '-')
                temp += d.front(), d.pop_front();
            else {
                s.emplace_back(temp);
                temp.clear();
                c.emplace_back(d.front());
                d.pop_front();
            }

        s.emplace_back(temp);
        for (auto it:c) {
            int num1 = atoi(s.front().c_str());
            s.pop_front();
            int num2 = atoi(s.front().c_str());
            s.pop_front();
            s.insert(s.begin(), to_string(it == '+' ? num1 + num2 : num1 - num2));
        }

        if ((*s.begin() == "24"))
            printf("Yes\n");
        else
            printf("No\n");
//        printf("%s\n", (*s.begin()).c_str());

//        while (!d.empty()) {
//            printf("%c", d.front());
//            d.pop_front();
//    }
    }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值