IDEA快速构建SpringBoot项目

简介: IDEA快速构建SpringBoot项目

IDEA快速构建SpringBoot项目

概述:本文讲解IDEA如何快速构建SpringBoot项目。

进入Spring Initializr

创建controller文件

代码:

package com.example.springbootstudy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }
}

访问网址

合并集合

一共有 n 个数,编号是 1∼n,最开始每个数各自在一个集合中。

现在要进行 m 个操作,操作共有两种:

M a b,将编号为 a 和 b 的两个数所在的集合合并,如果两个数已经在同一个集合中,则忽略这个操作;

Q a b,询问编号为 a 和 b 的两个数是否在同一个集合中;

输入格式

第一行输入整数 n 和 m。

接下来 m 行,每行包含一个操作指令,指令为 M a b 或 Q a b 中的一种。

输出格式

对于每个询问指令 Q a b,都要输出一个结果,如果 a 和 b 在同一集合内,则输出 Yes,否则输出 No。

每个结果占一行。

数据范围

1≤n,m≤105

输入样例:

4 5

M 1 2

M 3 4

Q 1 2

Q 1 3

Q 3 4

输出样例:

Yes

No

Yes

提交代码

#include<iostream>
using namespace std;
const int N = 100010;
int n, m;
int p[N];
int find(int x)                 // 找到x的祖先节点
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) p[i] = i;
    while (m--)
    {
        char op;
        int a, b;
        scanf (" %c%d%d", &op, &a, &b);
        if (op == 'M') p[p[find(a)]] = find(b);        // 让a的祖先节点指向b的祖先节点
        else
        {
            if (find(a) == find(b)) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}
import java.io.*;
public class Main
{
    static int N = 100010;
    static int n, m;
    static int [] p = new int [N];
    static int find(int x)
    {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));   
        String [] str = reader.readLine().split(" ");
        n = Integer.parseInt(str[0]);
        m = Integer.parseInt(str[1]);
        for (int i = 1; i <= n; ++ i) p[i] = i;
        while (m -- > 0)
        {
            String op;
            int a, b;
            str = reader.readLine().split(" ");
            op = str[0];
            a = Integer.parseInt(str[1]);
            b = Integer.parseInt(str[2]);
            if (op.equals("M")) p[find(a)] = find(b);
            else 
            {
                if (find(a) == find(b)) System.out.println("Yes");
                else System.out.println("No");
            }
        }
    }
}
相关文章
|
13天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
|
20天前
|
Java 应用服务中间件 程序员
如何利用Idea创建一个Servlet项目(新手向)(下)
如何利用Idea创建一个Servlet项目(新手向)(下)
29 0
|
20天前
|
Java 应用服务中间件 API
如何利用Idea创建一个Servlet项目(新手向)(上)
如何利用Idea创建一个Servlet项目(新手向)
23 0
|
3天前
|
Java Maven
idea中maven项目pom文件Could not acquire lock(s)
idea中maven项目pom文件Could not acquire lock(s)
|
2天前
|
存储 Java 应用服务中间件
Springboot项目打war包部署到外置tomcat容器【详解版】
该文介绍了将Spring Boot应用改为war包并在外部Tomcat中部署的步骤:1) 修改pom.xml打包方式为war;2) 排除内置Tomcat依赖;3) 创建`ServletInitializer`类继承`SpringBootServletInitializer`;4) build部分需指定`finalName`;5) 使用`mvn clean package`打包,将war包放入外部Tomcat的webapps目录,通过startup脚本启动Tomcat并访问应用。注意,应用访问路径和静态资源引用需包含war包名。
|
2天前
|
Java
IDEA云行项目提示Error: java: OutOfMemoryError
IDEA云行项目提示Error: java: OutOfMemoryError
|
2天前
|
Java Spring
IDEA 创建 SpringCloud项目-多项目方式
IDEA 创建 SpringCloud项目-多项目方式
|
3天前
|
Java
SpringBoot 项目启动初始化一个Map对象到内存
SpringBoot 项目启动初始化一个Map对象到内存
|
3天前
|
Java Docker 微服务
|
4天前
|
Java
解决IDEA打开java项目出现橙色j无法运行
解决IDEA打开java项目出现橙色j无法运行
8 0