最小生成树(贪心)--构造回文串(字符串 + 回文判断 + 构造)

1.最小生成树

java 复制代码
import java.util.*;


public class Main{
    
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        
        Arrays.sort(arr);
        long sum=0;
        
        for(int i=1;i<n;i++){
            sum+=arr[0]+arr[i];
        }
        System.out.print(sum);
    }
}

2.构造回文串(字符串 + 回文判断 + 构造)

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // 读取测试用例数量
        int t = sc.nextInt();
        
        // ✅ 用 for 循环跑 t 次(代替 while(n-->0))
        for(int i = 0; i < t; i++){
            int a = sc.nextInt();
            
            if(a == 1){
                System.out.println("a");
            } else {
                System.out.println("No");
            }
        }
    }
}
相关推荐
FakeOccupational2 小时前
【电路笔记 通信】IEEE 1588精密时间协议(PTP):数学假设+时间同步链路建模+消除主从偏差算法
笔记·算法
Bert.Cai2 小时前
Python input函数作用
开发语言·python
88号技师2 小时前
2026年3月中科院一区SCI-赏金猎人优化算法Bounty Hunter Optimizer-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
天启HTTP2 小时前
多线程环境下,动态IP怎么分配最合理
java·服务器·网络
再卷也是菜2 小时前
算法基础篇(12)Trie树
算法·字典树
weixin_464307632 小时前
QT宏、属性系统
开发语言·qt
hzb666662 小时前
xd_day32-day40
java·javascript·学习·安全·web安全·tomcat·php
2201_756206342 小时前
STM32F407 + ML307 阿里云物联网项目总结
c语言·开发语言·嵌入式硬件
小糯米6012 小时前
C++ 单调栈原理与模板
开发语言·c++·算法