常用的用户类与数据库表

简介: 常用的用户类与数据库表

常用的用户类与数据库表

概述:本文存储的是,我们构建项目的过程中经常用到的user类和对应的数据库建表语句。

User类

package bean;
public class User {
    private int id;  // id
    private int age; // 年龄
    private String name; // 姓名
    private String sex; // 性别
    // 无参构造方法
    public User() {
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

SQL建表

drop table if exists tb_user;
create table tb_user 
(
  id int primary key auto_increment,
  user_name varchar(30),
  password varchar(30),
  age int,
  sex varchar(2),
  realname varchar(30)
);
  insert into tb_user (user_name, password, age, sex, realname)
  values ('496464156489', '123456', 25, '男', '李华');

合并集合

一共有 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");
            }
        }
    }
}

滑动窗口

给定一个大小为 n≤106 的数组。

有一个大小为 k 的滑动窗口,它从数组的最左边移动到最右边。

你只能在窗口中看到 k 个数字。

每次滑动窗口向右移动一个位置。

以下是一个例子:

该数组为 [1 3 -1 -3 5 3 6 7],k 为 3。

窗口位置 最小值 最大值

[1 3 -1] -3 5 3 6 7 -1 3

1 [3 -1 -3] 5 3 6 7 -3 3

1 3 [-1 -3 5] 3 6 7 -3 5

1 3 -1 [-3 5 3] 6 7 -3 5

1 3 -1 -3 [5 3 6] 7 3 6

1 3 -1 -3 5 [3 6 7] 3 7

你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。

输入格式

输入包含两行。

第一行包含两个整数 n 和 k,分别代表数组长度和滑动窗口的长度。

第二行有 n 个整数,代表数组的具体数值。

同行数据之间用空格隔开。

输出格式

输出包含两个。

第一行输出,从左至右,每个位置滑动窗口中的最小值。

第二行输出,从左至右,每个位置滑动窗口中的最大值。

输入样例:

8 3

1 3 -1 -3 5 3 6 7

输出样例:

-1 -3 -3 -3 3 3

3 3 5 5 6 7

提交代码

C++

#include<iostream>
using namespace std;
const int N = 1000010;
int a[N], q[N], hh, tt = -1;
int main()
{
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; ++ i)    // 这个题要注意的是 q队列里面存放的是位置
    {
        scanf ("%d", &a[i]);        // 先求的是最小值
        if (i - k + 1 > q[hh]) ++hh;  // 如果最小值的位置已经滑出窗口了 然后就
                                    // ++ hh代表这个数已经没了
        while (hh <= tt && a[i] <= a[q[tt]]) -- tt; // 先确保队列里面有数字
                                    // 然后如果新来的数字要小于 队列里面的最小值
                                    // 那么--tt 就代表当前队列的最小值去掉
        q[++ tt] = i;  // 把新来的数字放到队列中
        if (i + 1 >= k) printf ("%d ", a[q[hh]]); // 当前队列的长度已经满足k了
                                    // 就可以把对首的元素输出出来
    }
    puts("");
    int hh = 0, tt = -1;
    for (int i = 0; i < n; ++ i)
    {
        if (i - k + 1 > q[hh]) ++ hh;
        while (hh <= tt && a[i] >= a[q[tt]]) -- tt;
        q[++ tt] = i;
        if (i + 1 >= k) printf("%d ", a[q[hh]]);
    }
    return 0;
}

Java

import java.io.*;
public class Main
{
    final static int N = 1000010;
    static int [] a = new int [N];
    static int [] q = new int [N];
    static int hh = 0, tt = -1;
    public static void main(String[] args) throws IOException
    {
        int n, k;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        String [] str = reader.readLine().split(" ");
        n = Integer.parseInt(str[0]);
        k = Integer.parseInt(str[1]);
        str = reader.readLine().split(" ");
        for (int i = 0; i < n; ++ i) a[i] = Integer.parseInt(str[i]);
        // for (int i = 0; i < n; ++ i)
        // {
        //     if (hh <= tt && i - k + 1 > q[hh])  ++ hh;
        //     while (hh <= tt && a[i] <= a[q[hh]]) -- tt;
        //     q[++ tt] = i;
        //     if (i + 1 >= k) out.write(a[q[hh]]+" ");
        // }
        for(int i = 0; i < n; i ++)
        {
            if(hh <= tt && i - q[hh] + 1 > k) hh++;//判断队头是否已经滑出窗口
            while(hh <= tt && a[q[tt]] >= a[i]) tt--;//出队
            q[++tt] = i;//入队
            if(i >= k - 1) out.write(a[q[hh]]+" ");
        }
        out.write("\n");
        hh = 0;
        tt = -1;
        // for (int i  = 0; i < n; ++ i)
        // {
        //     if (hh <= tt && i - k + 1 > q[hh]) ++ hh;
        //     while (hh <= tt && a[i] >= a[q[hh]]) -- tt;
        //     q[++ tt] = i;
        //     if (i + 1 >= k) out.write(a[q[hh]]+" ");
        // }
        for(int i = 0; i < n; i ++)
        {
            if(hh <= tt && i - q[hh] + 1 > k) hh++;//判断队头是否已经滑出窗口
            while(hh <= tt && a[q[tt]] <= a[i]) tt--;//出队
            q[++tt] = i;//入队
            if(i >= k - 1) out.write(a[q[hh]]+" ");
        }
        out.flush();
        out.close();
    }
}
相关文章
|
Python
matplotlib 按指定的时间间隔生成坐标轴
matplotlib 提供了自定义生成时间轴的库,而当我们需要按照自己定义的时间间隔去生成时间轴时,时间轴并没有正常显示,只是按照一个时间跨度更大的方式显示,本文强制 matplotlib 严格按照要求自定义的时间间隔来坐标轴,并对每一行代码做了详细的说明。
7986 0
matplotlib 按指定的时间间隔生成坐标轴
|
9月前
|
数据采集 存储 监控
注塑机数据采集如何实现与 MES 系统的双向数据闭环?
本文从落地实操出发,系统解析注塑机数据采集的适配范围、400+核心参数、四大应用场景及系统对接方案,直击设备协议繁杂、数据维度不足、系统孤岛等三大难点,提供非侵入式、全覆盖、可闭环的数字化升级路径。
983 6
|
7月前
|
安全 网络协议 网络安全
终端网络访问控制的技术演进与工程实践:从边界防御到内核级微分段
本文探讨网络边界消融背景下终端级微分段技术,提出基于策略域的“策略随行”管控范式,融合内核层(WFP/eBPF)过滤、IP/端口级精细控制、应用级QoS调度及零信任动态授权,实现东西向流量隔离与纵深防御,助力企业安全架构向零信任演进。(239字)
|
Java API 开发者
Spring 控制反转与依赖注入:从玄学编程到科学管理
在传统开发中,手动`new`对象导致紧耦合、难以维护和测试。控制反转(IoC)将对象创建交给框架,实现解耦。Spring通过IOC容器自动管理对象生命周期,开发者只需声明依赖,无需关心创建细节。依赖注入(DI)是IoC的具体实现方式,支持构造器、Setter和字段注入。构造器注入推荐使用,保证依赖不可变且易于测试。对于多个同类型Bean,可用`@Qualifier`或`@Primary`解决冲突。此外,Spring还支持依赖查找(DL),开发者主动从容器获取Bean,适用于动态场景,但侵入性强。掌握IoC与DI,有助于构建灵活、可维护的Spring应用。
|
10月前
|
消息中间件 人工智能 边缘计算
空地协同让电力巡检更智能 ——从人工攀爬到立体监测的技术演进
这是一套“空地协同”智能电力巡检系统:无人机搭载RTK+20倍变焦相机自主巡线,工程车集成AI边缘计算(Jetson AGX Orin)与红外检测,实现厘米级定位、小目标识别(92%+准确率)、无网环境本地分析+断点续传。系统已落地应用,显著降低人工登塔风险。
846 4
|
消息中间件 存储 负载均衡
🔥产品:直播送礼延迟这么大,你就不能快点吗
先赞后看,南哥助你Java进阶一大半其实抖音的实时音视频技术RTC,是来源于火山引擎RTC的支持,抖音、火山引擎、巨量引擎属于字节跳动公司旗下的不同业务板块。我是南哥,一个Java学习与进阶的领路人。相信对你通关面试、拿下Offer进入心心念念的公司有所帮助。
920 6
🔥产品:直播送礼延迟这么大,你就不能快点吗
|
数据采集 存储 JSON
构建1688店铺商品数据集:Python爬虫数据采集与格式化实践
构建1688店铺商品数据集:Python爬虫数据采集与格式化实践
|
自动驾驶 物联网 5G