csp201903

小中大

实现

#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

int n;
int q[N];

int main()
{
    
    
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
    int res[3];
    res[0] = q[0] * 2, res[1] = q[n - 1] * 2;
    if (n % 2) res[2] = q[n / 2] * 2;
    else res[2] = q[n / 2 - 1] + q[n / 2];

    sort(res, res + 3);
    for (int i = 2; i >= 0; i -- )
        if (res[i] % 2) printf("%.1lf ", res[i] / 2.0);
        else printf("%d ", res[i] / 2);
    return 0;
}

二十四点

实现

#include <bits/stdc++.h>

using namespace std;

stack<int> num;
stack<char> op;

void eval()
{
    
    
    int b = num.top(); num.pop();
    int a = num.top(); num.pop();
    char c = op.top(); op.pop();
    int x;
    if (c == '+') x = a + b;
    else if (c == '-') x = a - b;
    else if (c == 'x') x = a * b;
    else
    {
    
    
        if (a * b >= 0) x = a / b;
        else  // 向下取整。C++中a/b是向零取整
        {
    
    
            if (a % b == 0) x = a / b;
            else x = a / b - 1;
        }
    }
    num.push(x);
}

int main()
{
    
    
    unordered_map<char, int> pr;  //定义运算符的优先级
    pr['+'] = pr['-'] = 1;
    pr['x'] = pr['/'] = 2;
    int n;
    cin >> n;
    while (n -- )
    {
    
    
        string str;
        cin >> str;
        num = stack<int>();
        op = stack<char>();
        for (auto c: str)
            if (c >= '0' && c <= '9') num.push(c - '0');  //是数字则之间压入数字栈
            else
            {
    
    
                while (op.size() && pr[op.top()] >= pr[c]) eval();  //当前运算符优先级更低 进行运算
                op.push(c);   
            }
        while (op.size()) eval();
        if (num.top() == 24) puts("Yes");
        else puts("No");
    }
    return 0;
}

损坏的RAID5

实现

#include<bits/stdc++.h>
using namespace std;
int n,s,l;
int k;
const int N = 1010, M = 40 * 1024 * 8 + 10;
unsigned int disk[N][M / 8];
bool st[N];
char str[M];
unsigned int get(char c)
{
    
    
    if(c<='9') return c-'0';
    return c-'A'+10;
}
inline char get(unsigned int x)
{
    
    
    if (x <= 9) return x + '0';
    return x - 10 + 'A';
}

inline string u2s(unsigned int x)
{
    
    
    string res;
    for (int i = 7; i >= 0; i -- )
        res += get(x >> (i << 2) & 15);
    return res;
}
int main()
{
    
    
    int len;
    cin>>n>>s>>l;
    for(int i=0;i<l;i++)
    {
    
    
        scanf("%d", &k);
        getchar();
        fgets(str, M, stdin);
        st[k] = 1;
        int sz = strlen(str) - 1;
        for(int j=0;j<sz;j+=8)
        {
    
    
            
            unsigned int x = 0;
            for(int k=0;k<8;k++)
            {
    
    
                x = (x<<4)+get(str[j+k]);
            }
            disk[k][j>>3] = x;
            //cout<<x<<endl;
        }
        len = sz>>3;
    }
    int m,b;
    
    cin>>m;
    while(m--)
    {
    
    
        cin>>b;
        if(b>=(n-1)*len) puts("-");
        else
        {
    
    
            int k = b/s;
            int hang = k/(n-1);
            int lie = k%(n-1);
            lie = (lie+n-(hang)%n)%n;
            int r = hang * s + b % s;
           
            if(st[lie])
            {
    
    
                puts(u2s(disk[lie][r]).c_str());
                
            }
            else if(l==n-1)
            {
    
    
               unsigned int x = 0;
                for (int i = 0; i < n; i ++ ) x ^= disk[i][r];
                puts(u2s(x).c_str());
            }
            else puts("-");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Tracy_yi/article/details/129218016
今日推荐