XMUOJ校外实训1

发布时间 2023-07-03 16:05:33作者: 雪之下,树之旁

A+B

a, b = map(int, input().strip().split())
print(a + b)

熄灯问题

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

int n = 5, m = 6;
int a[10][10]; 

int vis[10]; // 枚举第一行的
int ans[10][10]; 
int fx[5] = {0, 1, -1, 0, 0}; 
int fy[5] = {0, 0, 0, 1, -1}; 

bool check(){
    memset(ans, 0, sizeof(ans)); 
    int b[10][10]; 
    memcpy(b, a, sizeof(b)); 
    for(int i = 1; i <= m; i++){
        if(vis[i]){
            for(int k = 0; k <= 4; k++){
                b[1+fx[k]][i+fy[k]] ^= 1; 
            }
        }  
        ans[1][i] = vis[i]; 
    }
    for(int i = 2; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(b[i-1][j] == 0) continue; 
            else{
                ans[i][j] = 1; 
                for(int k = 0; k <= 4; k++){
                    b[i+fx[k]][j+fy[k]] ^= 1; 
                }
            }
        }
    }
    // for(int i = 1; i <= n; i++){
    //     for(int j = 1; j <= m; j++){
    //         cout << b[i][j] << " "; 
    //     }
    //     cout << endl; 
    // }
    // cout << "---------------------\n" << endl; 
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(b[i][j] == 1) return 0; 
        }
    }
    return 1; 
}

bool flag = 0; 

void dfs(int now){
    if(flag) return ; 
    if(now == 7){
        if(check()){
            flag = 1; 
        }
        return ; 
    }
    for(int i = 0; i < 2; i++){
        vis[now] = i; 
        dfs(now + 1); 
    }
    return ; 
}

int main(){
    int T; cin >> T; 
    for(int Case = 1; Case <= T; Case++){
        flag = 0; 
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++)
                cin >> a[i][j]; 
        }
        dfs(1); 
        printf("PUZZLE #%d\n", Case); 
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                printf("%d ", ans[i][j]); 
            }
            printf("\n"); 
        }
    }
    return 0; 
}

排序考试

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

int main(){
    int T; cin >> T; 
    while(T--){
        int n; cin >> n; 
        vector <int> G; 
        for(int i = 1; i <= n; i++){
            int x; cin >> x; 
            G.push_back(x); 
        }
        sort(G.begin(), G.end()); 
        for(int i = 0; i < G.size(); i++){
            cout << G[i]; 
            if(i == G.size() - 1)
                continue; 
            cout << " "; 
        }
        cout << endl; 
    }
    return 0; 
}

完美立方

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100010

int n; 
ll Cu(ll x){
    return x * x * x; 
} 

struct node{
    int a, b, c, d; 
} t[N]; 

bool cmp(node a, node b){
    return a.a < b.a || (a.a == b.a && a.b < b.b) || (a.a == b.a && a.b == b.b && a.c < b.c) || (a.a == b.a && a.a == b.b && a.c == b.c && a.d < b.d); 
}

int main(){
    cin >> n; 
    int cnt = 0; 
    for(int a = 1; a <= n; a++){
        for(int b = a; b >= 2; b--){
            for(int c = b;c >= 2; c--){
                for(int d = c; d >= 2; d--){
                    if(Cu(a) == Cu(b) + Cu(c) + Cu(d)){
                        t[++cnt] = (node){a, d, c, b}; 
                    }
                }
            }
        }
    }
    sort(t + 1, t + cnt + 1, cmp);
    for(int i = 1; i <= cnt; i++){
        printf("Cube = %d, Triple = (%d,%d,%d)\n", t[i].a, t[i].b, t[i].c, t[i].d); 
    } 
    return 0; 
}

人的周期

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

int p, e, h, d; 

int main(){
    int Case = 0; 
    while(cin >> p >> e >> h >> d && p != -1){
        Case++; 
        while(p - 23 >= 0) p -= 23; 
        while(e - 28 >= 0) e -= 28; 
        while(h - 33 >= 0) h -= 33; 
        int now = d; 
        while(666){
            now++; 
            if((now - p) % 23 == 0 && (now - e) % 28 == 0 && (now - h) % 33 == 0){
                break; 
            }
            
        }
        printf("Case %d: the next triple peak occurs in %d days.\n", Case, now - d); 
    }
    return 0; 
}

假币问题

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


int vis[27]; 

struct Query{
    string s[3]; 
} q[3]; 

bool hav = 0; 
int check(int h, int weight){ // weight == 1: 重
    string now;
    now.push_back(h);  
    for(int i = 0; i < 3; i++){ // 对每个Query 检查
        string l = q[i].s[0], r = q[i].s[1], result = q[i].s[2]; 
        if(weight == 0) swap(l, r); 
        switch (result[0])
        {
        case 'u':
            if(l.find(now) == string::npos) return 0; 
            break; 
            break;
        case 'd':
            if(r.find(now) == string::npos) return 0; 
            break; 
        case 'e':
            if(l.find(now) != string::npos || r.find(now) != string::npos) return 0; 
            break; 
        default:
            break;
        }
    }
    return 1; 
}

int main(){
    int T; cin >> T; 
    while (T--){
        for(int i = 0; i < 3; i++){
            cin >> q[i].s[0] >> q[i].s[1] >> q[i].s[2]; 
        }
        for(int now = 'A'; now <= 'L'; now++){
            // hav = 0; 
            bool ans = check(now, 1); 
            if(ans){
                // if(!hav) continue; 
                printf("%c is the counterfeit coin and it is heavy. \n", now); 
                break; 
            }
            ans = check(now, 0); 
            if(ans){
                // if(!hav) continue; 
                printf("%c is the counterfeit coin and it is light. \n", now); 
                break; 
            }
        }
    }
    return 0; 
    
}

两数之和

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

int tot, n; 
int a[N];

int main(){
    cin >> tot >> n; 
    for(int i = 1; i <= n; i++){
        cin >> a[i]; 
    }
    int p = 1, q = n; 
    while(a[p] + a[q] != tot){
        if(a[p] + a[q] > tot) q--; 
        else if(a[p] + a[q] < tot) p++; 
    }
    cout << p - 1 << " " << q - 1 << endl; 
    return 0; 
}

三数之和

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

int n; 
int a[N]; 
int tot; 

struct node{
    int x, y, z;

    bool operator < (const node &a) const{
        if(x < a.x) return 1; 
        else if(x == a.x && y < a.y) return 1; 
        else if(x == a.x && y == a.y && z < a.z) return 1; 
        else return 0; 
    }
} ; 

int main(){
    cin >> tot >> n; 
    for(int i = 1; i <= n; i++){
        cin >> a[i]; 
    }
    sort(a + 1, a + n + 1); 
    vector <node> G; 
    for(int k = 2; k <= n - 1; k++){
        int p = 1, q = n; 
        while(p < k && q > k){
            if(a[p] + a[q] > tot - a[k]) q--; 
            if(a[p] + a[q] < tot - a[k]) p++; 
            if(a[p] + a[q] + a[k] == tot && a[p] != a[q] && a[p] != a[k] && a[q] != a[k]) 
                G.push_back((node){a[p], a[k], a[q]}), p++;
        }   
        if(p == k || q == k) continue; 
    }
    sort(G.begin(), G.end()); 
    for(auto it : G){
        cout << it.x << " " << it.y << " " << it.z << endl; 
    }
    return 0; 
}

四数之和

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

int n; 
int a[N]; 
int tot; 

struct node{
    int x, y, z, m;

    bool operator < (const node &a) const{
        if(x < a.x) return 1; 
        else if(x == a.x && y < a.y) return 1; 
        else if(x == a.x && y == a.y && z < a.z) return 1; 
        else if(x == a.x && y == a.y && z == a.z && m < a.m) return 1; 
        else return 0; 
    }
} ; 

int main(){
    cin >> tot >> n; 
    for(int i = 1; i <= n; i++){
        cin >> a[i]; 
    }
    sort(a + 1, a + n + 1); 
    vector <node> G; 
    for(int k = 2; k <= n - 1; k++){
    for(int h = k + 1; h <= n - 1; h++){
        int p = 1, q = n; 
        while(p < k && q > h){
            if(a[p] + a[q] > tot - a[k] - a[h]) q--; 
            if(a[p] + a[q] < tot - a[k] - a[h]) p++; 
            if(a[p] + a[q] + a[k] + a[h] == tot && a[p] != a[q] && a[p] != a[k] && a[q] != a[k]) 
                G.push_back((node){a[p], a[k], a[h], a[q]}), p++;
        }   
    }
    }
    sort(G.begin(), G.end()); 
    for(auto it : G){
        cout << it.x << " " << it.y << " " << it.z << " " << it.m << endl; 
    }
    return 0; 
}

二进制密码锁

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

char a[N], b[N], t[N]; 
int n; 

int minn = 1e9; 

int main(){
    scanf("%s", a + 1); 
    scanf("%s", t + 1); 
    n = strlen(a + 1); 
    for(int i = 1; i <= n; i++){
        a[i] -= '0'; 
        t[i] -= '0'; 
    }
    for(int i = 0; i <= 2; i++){
            int num = 0; 
            memcpy(b, a, sizeof(b)); 
            if(i == 1){
                b[1] ^= 1, b[2] ^= 1; 
                num++; 
            }
            
            
            for(int i = 2; i <= n; i++){
                if(b[i-1] != t[i-1]){
                    b[i-1] ^= 1, b[i] ^= 1, b[i+1] ^= 1; 
                    num++; 
                }   
            }
            bool flag = 1;  
            for(int i = 1; i <= n; i++){
                if(b[i] != t[i]) flag = 0; 
            }
            if(flag){
                minn = min(minn, num); 
            }
        
    }
    if(minn == 1e9)
        cout << "impossible " << endl; 
    else cout << minn << endl;
    return 0; 
}