B. Nick and Array 简单简洁

因为在解决过程中没有看到比较简洁易懂的代码,所以在解决后发个博客给大家康康

利用公式ai = -ai -1处理数组,使数组积最大,我的思路是在输入时全部转成负值,同时找最小值,如果个数为奇数,转换最小值为正值,如果个数为偶数则不进行更多处理,最后输出

原链接:Codeforces - Problem Bhttps://codeforces.ml/contest/1180/problem/B

        Nick had received an awesome array of integers a=[a1,a2,…,an]a=[a1,a2,…,an] as a gift for his 55 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…ana1⋅a2⋅…an of its elements seemed to him not large enough.

        He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index ii (1≤i≤n1≤i≤n) and do ai:=−ai−1ai:=−ai−1.

        For example, he can change array [3,−1,−4,1][3,−1,−4,1] to an array [−4,−1,3,1][−4,−1,3,1] after applying this operation to elements with indices i=1i=1 and i=3i=3.

        Kolya had immediately understood that sometimes it's possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index.

        Help Kolya and print the array with the maximal possible product of elements a1⋅a2⋅…ana1⋅a2⋅…an which can be received using only this operation in some order.If there are multiple answers, print any of them.

The first line contains integer nn (1≤n≤1051≤n≤105) — number of integers in the array.

        The second line contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106) — elements of the array

Output

        Print nn numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array.

If there are multiple answers, print any of them.

样例:

  • in    4 2 2 2 2
  • out  -3 -3 -3 -3
  • in    1 0
  • out  0
  • in    3 -3 -3 2
  • out -3 -3 2 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
 
int num[1000000];
 
int main()
{
    int i;
    int N, nmin;
    scanf("%d", &N);
 
    nmin = 0;
    for (i = 0; i < N; i++)
    {
        scanf("%d", &num[i]);
        if (num[i] >= 0) num[i] = num[i] * (-1) - 1; // 全部转换为负数后 存到数组里
 
        if (num[nmin] > num[i]) nmin = i; // 找到最小数
    }
    
    if (N%2) num[nmin] = num[nmin] * (-1) - 1; // 如果个数是奇数,转换最小数为正数
 
    for (i = 0; i < N; i++)
        printf("%d ", num[i]);
 
    return 0;
}

你提供的 SQL 语句: ```sql SELECT NAME FROM YGW WHERE SSO = U.NICK_NAME ``` 是一个子查询的常见用法,但你说“取一条”,说明你想从结果中只获取**一条记录**,这在实际应用中很常见(比如关联查询时避免多行错误)。 下面我来详细解释并给出正确的写法和使用场景。 --- ### ✅ 目标 从表 `YGW` 中根据条件 `SSO = U.NICK_NAME` 查询 `NAME` 字段,并且 **只返回一条记录**。 > 假设 `U` 是外层查询中的别名(例如另一个表的别名),所以这是一个**相关子查询**。 --- ## ✅ 正确写法:限制只取一条(防止多值错误) ### 方法一:使用 `LIMIT 1`(MySQL / SQLite) ```sql SELECT NAME FROM YGW WHERE SSO = U.NICK_NAME LIMIT 1; ``` ### 方法二:使用 `TOP 1`(SQL Server / MS Access) ```sql SELECT TOP 1 NAME FROM YGW WHERE SSO = U.NICK_NAME; ``` ### 方法三:使用 `ROWNUM`(Oracle 旧版本) ```sql SELECT NAME FROM ( SELECT NAME FROM YGW WHERE SSO = U.NICK_NAME ) WHERE ROWNUM = 1; ``` ### 方法四:使用 `FETCH FIRST ROW ONLY`(标准 SQL,适用于 Oracle 12c+, DB2, PostgreSQL) ```sql SELECT NAME FROM YGW WHERE SSO = U.NICK_NAME FETCH FIRST 1 ROW ONLY; ``` --- ## 📌 实际应用场景示例(以 MySQL 为例) 假设你在做关联查询,想把用户昵称 `U.NICK_NAME` 映射为真实姓名 `YGW.NAME`: ```sql SELECT U.ID, U.NICK_NAME, (SELECT NAME FROM YGW WHERE SSO = U.NICK_NAME LIMIT 1) AS REAL_NAME FROM USERS U; ``` ✅ 这里使用了**标量子查询**,并且加了 `LIMIT 1` 确保即使有多个匹配也只返回一个值,避免运行时错误。 --- ## ⚠️ 注意事项 1. **如果没有索引**,`SSO = U.NICK_NAME` 可能性能很差。建议在 `YGW(SSO)` 上创建索引: ```sql CREATE INDEX idx_ygw_sso ON YGW(SSO); ``` 2. 如果 `SSO` 和 `NICK_NAME` 数据类型不一致(如一个是 varchar,一个是 number),可能导致隐式转换失败或索引失效。 3. 若没有匹配的数据,结果会是 `NULL`。 4. 如果你希望控制“取哪一条”(比如最新的),应加上排序: ```sql SELECT NAME FROM YGW WHERE SSO = U.NICK_NAME ORDER BY CREATE_TIME DESC -- 示例:取最新的一条 LIMIT 1; ``` --- ## 总结 | 数据库 | 取一条语法 | |-------------|-------------------------------| | MySQL | `LIMIT 1` | | SQL Server | `SELECT TOP 1 ...` | | Oracle | 子查询 + `ROWNUM = 1` | | PostgreSQL | `LIMIT 1` 或 `FETCH FIRST 1` | | 标准 SQL | `FETCH FIRST 1 ROW ONLY` | 始终记得:**当用于标量子查询时,必须确保最多返回一行,否则会报错。** ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值