A. Row

本文介绍了一个关于座位安排的问题,需要判断给定的座位排列是否为最大就坐安排,即是否满足无人相邻且无法通过增加人员而不违反无人相邻规则的情况。文章提供了具体的输入输出示例及解决该问题的C语言代码。

A. Row
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You’re given a row with
n
chairs. We call a seating of people “maximal” if the two following conditions hold:

There are no neighbors adjacent to anyone seated.
It’s impossible to seat one more person without violating the first rule.
The seating is given as a string consisting of zeros and ones (
0
means that the corresponding seat is empty,
1
— occupied). The goal is to determine whether this seating is “maximal”.

Note that the first and last seats are not adjacent (if
n

2
).

Input
The first line contains a single integer
n
(
1

n

1000
) — the number of chairs.

The next line contains a string of
n
characters, each of them is either zero or one, describing the seating.

Output
Output “Yes” (without quotation marks) if the seating is “maximal”. Otherwise print “No”.

You are allowed to print letters in whatever case you’d like (uppercase or lowercase).

Examples
inputCopy
3
101
outputCopy
Yes
inputCopy
4
1011
outputCopy
No
inputCopy
5
10001
outputCopy
No
Note
In sample case one the given seating is maximal.

In sample case two the person at chair three has a neighbour to the right.

In sample case three it is possible to seat yet another person into chair three.

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char a[1010];
int main()
{
    int n,i;
    int flag=0;
    scanf("%d%s",&n,a+1);
    a[0]='0';
    a[n+1]='0';
    for(i=1;i<=n;i++)
    {
        if((a[i-1]=='0'&&a[i]=='0'&&a[i+1]=='0')||(a[i]=='1'&&a[i+1]=='1'))
        {
            flag=1;
            break;
        }
    }
    if(flag==1)printf("No\n");
    else printf("Yes\n");
    return 0;
}

题意:题目是说不能有挨着的11或者000,那对于边上的00,100,001,这种情况完全可以把边上的0换成1,这种是不符合条件的,只需要在左右边上加上0,看是不是满足000就可以

你提供的是一条 SQL 查询语句,使用了 `LEFT JOIN` 关联两个表:`FTP_INDEX_COST_DATA` 和 `B_FTP_HEAD_INFO`。你的目标是“提取 SQL”,我理解为:**你想从 Java 代码中正确地分离出这条 SQL,并确保其逻辑清晰、可读性强、便于调试和维护**。 --- ### ✅ 提取并优化后的 SQL(独立版本) ```sql SELECT a.data_dt, a.row_id, a.dbcb, a.ldxzbtj, a.CREATE_TS, a.CREATE_BY, a.LAST_UPD_BY, a.LAST_UPD_TS FROM FTP_INDEX_COST_DATA a LEFT JOIN B_FTP_HEAD_INFO b ON a.row_id = b.row_id WHERE a.data_dt = ? AND b.BEG_DT <= ? AND b.END_DT >= ? ``` --- ### 🔍 SQL 功能解析 | 部分 | 说明 | |------|------| | `SELECT a.*` 字段 | 只选择 `FTP_INDEX_COST_DATA` 表中的关键字段 | | `LEFT JOIN B_FTP_HEAD_INFO b ON a.row_id = b.row_id` | 左连接配置/主数据表,可能用于获取有效时间段(`BEG_DT`, `END_DT`) | | `a.data_dt = ?` | 筛选指定日期的数据 | | `b.BEG_DT <= ? AND b.END_DT >= ?` | 要求该 `row_id` 在另一个表中的“生效期间”覆盖当前 `dataDt` | > ⚠️ 注意:虽然是 `LEFT JOIN`,但你在 `WHERE` 中使用了 `b.BEG_DT` 和 `b.END_DT`,这会导致 **实际变成内连接(INNER JOIN)效果**,因为 `NULL` 的记录会被过滤掉。 --- ### ❗重要问题:`LEFT JOIN` + `WHERE` 引用右表字段 → 失去左连接意义 #### 当前写法的问题: ```sql LEFT JOIN B_FTP_HEAD_INFO b ON ... WHERE ... b.BEG_DT <= ? AND b.END_DT >= ? ``` - 如果 `b` 没有匹配行,则 `b.BEG_DT` 和 `b.END_DT` 为 `NULL` - 加上 `WHERE b.BEG_DT <= ?` 后,这些 `NULL` 行将被排除 - 结果:等价于 `INNER JOIN` #### ✅ 正确做法(如果你想保留左连接语义): 应把条件移到 `ON` 子句中: ```sql SELECT a.data_dt, a.row_id, a.dbcb, a.ldxzbtj, a.CREATE_TS, a.CREATE_BY, a.LAST_UPD_BY, a.LAST_UPD_TS FROM FTP_INDEX_COST_DATA a LEFT JOIN B_FTP_HEAD_INFO b ON a.row_id = b.row_id AND b.BEG_DT <= ? AND b.END_DT >= ? WHERE a.data_dt = ? ``` ✅ 这样才能真正实现: - 所有 `a` 表中 `data_dt` 匹配的记录都返回 - 若 `b` 中有对应的有效期记录,则带上;否则 `b` 相关字段为 `NULL` --- ### 🧩 Java 中如何优雅地定义这个 SQL? 建议将 SQL 单独提取为常量或配置文件,提高可维护性: ```java private static final String QUERY_FTP_COST_DATA_SQL = "SELECT " + " a.data_dt, " + " a.row_id, " + " a.dbcb, " + " a.ldxzbtj, " + " a.CREATE_TS, " + " a.CREATE_BY, " + " a.LAST_UPD_BY, " + " a.LAST_UPD_TS " + "FROM " + " FTP_INDEX_COST_DATA a " + " LEFT JOIN B_FTP_HEAD_INFO b " + " ON a.row_id = b.row_id " + " AND b.BEG_DT <= ? " + " AND b.END_DT >= ? " + "WHERE " + " a.data_dt = ?"; ``` 然后在代码中使用: ```java List<FtpIndexCostData> list = jdbcTemplate.query( QUERY_FTP_COST_DATA_SQL, new BeanPropertyRowMapper<>(FtpIndexCostData.class), dataDt, dataDt, lastWorkDay // 注意参数顺序! ); ``` > ⚠️ 参数顺序必须与 `?` 出现顺序一致! --- ### ✅ 总结:原始 SQL 是否有问题? | 项目 | 说明 | |------|------| | SQL 语法 | ✅ 正确 | | JOIN 类型 | ⚠️ 名义是 `LEFT JOIN`,实为 `INNER JOIN` 效果 | | 条件位置 | ❌ 应将 `b.BEG_DT` / `END_DT` 移到 `ON` 中以保持左连接含义 | | 可读性 | ⚠️ 建议拆分字符串或外部化 SQL | | 参数绑定 | ✅ 正确,只要顺序对 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值