6.3总结

· · 个人记录

T1:三角形分类

题目理解

给定三条边长 a, b, c(正整数 \leq 10000),需判断三角形类型并按以下顺序输出:

  1. 不能构成三角形 → Not triangle
  2. 直角三角形 → Right triangle
  3. 锐角三角形 → Acute triangle
  4. 钝角三角形 → Obtuse triangle
  5. 等腰三角形 → Isosceles triangle
  6. 等边三角形 → Equilateral triangle
    若满足多个条件,按顺序分行输出。

代码思路分析

#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int a,b,c,f=0;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>a>>b>>c;
    int s[5]={a,b,c};
    sort(s,s+3);          // 排序三边
    int x=s[0],y=s[1],z=s[2];

    // 判断三角形存在性
    if(x+y<=z || x+z<=y || y+z<=x){
        cout<<"Not triangle";
        return 0;
    }

    // 按角分类(基于最大边z)
    if(x*x+y*y==z*z){
        cout<<"Right triangle\n";
    }
    else if(x*x+y*y>z*z){
        cout<<"Acute triangle\n";
    }
    else cout<<"Obtuse triangle\n";

    // 等腰判断
    if(x==y || y==z || x==z) 
        cout<<"Isosceles triangle\n";

    // 等边判断
    if(x==y && y==z) 
        cout<<"Equilateral triangle"; 
    return 0;
}

T2:幸运素数

题目理解

在区间 [m, n] 中找出所有"幸运素数"(1 \le m \le n \le 9999)。

幸运素数就是:

  1. 本身是素数
  2. 不断移除最低位后仍是素数
  3. 最终得到一位素数时停止

代码思路分析

#include<iostream>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
int m,n;
bool isPrime[10000+10];  //素数标记数组

//埃式筛法
void prime(){
    memset(isPrime,true,sizeof isPrime);
    isPrime[0] = isPrime[1] = false;  // 0和1非素数
    for(int i=2; i*i<=10010; i++){    // 筛法核心
        if(isPrime[i]){
            for(int j=i*i; j<=10010; j+=i){
                isPrime[j] = false;
            }
        }
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    prime();
    cin >> m >> n;
    for(int i=m; i<=n; i++){
        if(!isPrime[i]) continue;  // 非素数跳过
        int x = i;
        bool flag = true;

        // 逐位检查
        while(x > 0){
            if(!isPrime[x]) {      // 当前数非素数
                flag = false;
                break;
            }
            if(x < 10) break;     // 已到一位数
            x /= 10;              // 移除最低位
        }
        if(flag) cout << i << endl;  // 输出幸运素数
    }
    return 0;
}

T3:幻界法则

题目理解

元素操控者可施展 n 次赤焰冲击(伤害 a_i)和 m 次冰霜新星(伤害 b_j)。通过合理排序攻击,最大化总伤害:

代码思路分析

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k, a[1000005], b[1000005];
signed main() {
    cin >> n >> m >> k;
    int sum = 0, l = min(n, m);
    // 计算基础伤害总和
    for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
    for (int i = 1; i <= m; i++) cin >> b[i], sum += b[i];

    // 关键步骤:降序排序赤焰伤害
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    // 贪心计算额外收益
    for (int i = 1; i <= l; i++) {
        if (a[i] > k) sum += a[i];  // 赤焰触发更优
        else sum += k;               // 冰霜触发更优
    }
    cout << sum;
    return 0;
}

T4:花园中的美丽值最大化

题目理解

给定 n 朵花的活力值序列 a_i,花园总美丽值为 \sum_{i=1}^n \max_{j=1}^i a_j。允许进行恰好 k 次交换操作(每次交换两朵花的绽放顺序),目标是最大化总美丽值。

代码思路分析

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e5 + 10;

ll n, k, a[N];

void solve(){
    cin >> n >> k;
    ll maxn = -1, ans = 0;
    // 读取数据并求全局最大值
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        maxn = max(maxn, a[i]);
    }

    // 情况1:k=0(无交换)
    if (k == 0) { 
        ans = a[1];
        ll cur_max = a[1];
        for (int i = 2; i <= n; i++) {
            cur_max = max(cur_max, a[i]);  // 更新前缀最大值
            ans += cur_max;  // 累加当前美丽值
        }
        cout << ans << endl;
        return;
    }

    // 情况2:k=1(一次交换)
    if (k == 1) {
        if (n > 2) {
            // n>2时,将全局最大值换到首位
            cout << n * maxn << endl;
        } else {
            // n=2时特殊处理
            if (a[1] > a[2]) 
                cout << a[1] + a[2] << endl;  // 交换后为[a2,a1]
            else 
                cout << a[2] + a[2] << endl;  // 交换后为[a1,a2]→[a2,a1]→最大值在前
        }
        return;
    }

    // 情况3:k≥2
    if (n > 2) {
        // n>2时,通过交换使最大值在首位
        cout << n * maxn << endl;
    } else {
        // n=2时,根据交换次数的奇偶性决定
        if (a[1] > a[2]) {
            if (k % 2 == 1) 
                cout << a[1] + a[2] << endl;  // 奇数次交换后为[a2,a1]
            else 
                cout << a[1] + a[1] << endl;  // 偶数次交换回归原序列
        } else {
            if (k % 2 == 1) 
                cout << a[2] + a[2] << endl;  // 奇数次交换后为[a2,a1]
            else 
                cout << a[1] + a[2] << endl;  // 偶数次交换回归原序列
        }
    }
}

T5:旋转三角形

题目理解

给定两个 N 层三角形(每行元素数等于行号),每个位置为0或1。可对三角形 A 进行任意次以下操作:

代码思路分析

#include<bits/stdc++.h>
using namespace std;
int n, b[15][15], ans = INT_MAX;

// 关键函数:计算变换后A与B的差异
void contrast(int w[15][15]) {
    int sum = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= i; j++)
            if(w[i][j] != b[i][j]) sum++;
    ans = min(ans, sum);  // 更新全局最小差异
}

int main() {
    cin >> n;
    int a[15][15], a1[15][15], a2[15][15];

    // 输入三角形A和B
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= i; j++) 
            cin >> a[i][j];
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= i; j++) 
            cin >> b[i][j];

    // 枚举6种变换
    contrast(a);  // 原始形态

    // 顺时针120°变换
    for(int j = n, x = 1; j >= 1; j--, x++) 
        for(int i = j, y = 1; i <= n; i++, y++) 
            a1[x][y] = a[i][j];
    contrast(a1);

    // 顺时针240°变换
    for(int j = n, x = 1; j >= 1; j--, x++) 
        for(int i = j, y = 1; i <= n; i++, y++) 
            a2[x][y] = a1[i][j];
    contrast(a2);

    // 对称变换
    int c[15][15], c1[15][15], c2[15][15];
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= i; j++) 
            c[i][j] = a[i][i-j+1];  // 左右翻转
    contrast(c);

    // 对称+顺时针120°
    for(int j = n, x = 1; j >= 1; j--, x++) 
        for(int i = j, y = 1; i <= n; i++, y++) 
            c1[x][y] = c[i][j];
    contrast(c1);

    // 对称+顺时针240°
    for(int j = n, x = 1; j >= 1; j--, x++) 
        for(int i = j, y = 1; i <= n; i++, y++) 
            c2[x][y] = c1[i][j];
    contrast(c2);

    cout << ans;
    return 0;
}