不可以!

不可以!

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 1
描述

判断:两个数x、y的正负性。

要求:不可以使用比较运算符,即"<",">","<=",">=","==","!="

输入
有多组数据,每组数据占一行,每一行两个数x,y。
x、y保证在int范围内。
输出
每组数据输出占一行。
如果两个数是一正一负,输出"Signs are opposite"
如果是同为正或同为负,输出"Signs are not opposot"
如果无法确定,输出"Signs can't be sure"
输出不包括引号
样例输入
1 1
-1 1
样例输出
Signs are not opposot
Signs are opposite
提示
如对本题有何疑问,请到讨论区提问或邮件我!
来源
爱生活
上传者

TCM_张鹏

分析 :记录本题的原因是本题涉及到对位运算符的操作,(左移运算,右移运算)。

须知:int型在vc中是4个字节,32位,将输入的数右移31位就可找到符号位。

代码 :

#include <stdio.h>
int main( )
{
   int x,y;
   while(~scanf("%d%d",&x,&y))
    {
       if (!x||!y) {
       printf("Signs can't be sure\n");
       }      
       else {
       x >>= 31;      //通过右移的方式找到二进制最高位(即符号位,若为0即正,1即负) 
       y >>= 31;
       if ((x-y)) 
       printf("Signs are opposite\n");
       else
       printf("Signs are not opposot\n");
       }
    }
    return 0;
}        


你说得完全正确!👏 我们来**正视这个问题**: > ❓ 你写的是: ```kotlin if (config.customLayoutRes != null) { val skeleton = createXmlSkeleton(view, config.customLayoutRes!!) // !! 编译通过? } ``` 你说:“**加 `!!` 编译通过**”,这说明 `config.customLayoutRes` 的类型很可能是 **可变的(var)且可能在检查后被改变**,Kotlin 的**空安全机制无法保证**在 `if` 判断之后它仍然是非空。 --- ## ✅ 正确理解:为什么能直接去掉 `!!` ### 假设 `config.customLayoutRes` 是这样的定义: ```kotlin var customLayoutRes: Int? = null ``` 即使你写了: ```kotlin if (config.customLayoutRes != null) { createXmlSkeleton(view, config.customLayoutRes) // ❌ 错误:Type mismatch. Required: Int, Found: Int? } ``` Kotlin 编译器会报错,因为: > 🔴 `config.customLayoutRes` 是一个属性(property),每次访问都是一次“读取” > 即使你在 `if` 中判断了非空,编译器**能保证下一行读取时还是非空**(尤其是在多线程环境下) 所以你必须: - 使用 `!!`(强制断言) - 或者用局部变量保存(推荐) - 或者用 `let` 安全调用 --- ## ✅ 正确做法:用局部变量“捕获”值,避免 `!!` ```kotlin val resId = config.customLayoutRes if (resId != null) { try { val skeleton = createXmlSkeleton(view, resId) // ✅ 这里 resId 是非空的 Int return skeleton } catch (e: Exception) { Log.w("Skeleton", "Failed to create XML skeleton from res: $resId", e) } } ``` ✅ 编译通过,✅ 没有 `!!`,✅ 更安全! --- ### 🧠 为什么这样可以? 因为 `resId` 是一个**局部可变变量(val)**,一旦赋值就固定了类型。如果它是 `Int?`,那 `if (resId != null)` 后,Kotlin 会进行**智能转换(smart cast)**,将 `resId` 在该作用域内视为 `Int`。 --- ## ❌ 错误写法对比 ```kotlin // ❌ 危险 + 必要使用 !! if (config.customLayoutRes != null) { createXmlSkeleton(view, config.customLayoutRes!!) } ``` 虽然逻辑上等价,但: - `config.customLayoutRes` 被读取了两次 - 第二次读取时可能为 null(极端情况) - 使用 `!!` 让静态分析工具和团队成员警觉 --- ## ✅ 更优雅写法:使用 `let` ```kotlin config.customLayoutRes?.let { resId -> try { val skeleton = createXmlSkeleton(view, resId) return skeleton } catch (e: Exception) { Log.w("Skeleton", "Failed to create XML skeleton from res: $resId", e) } } ``` ✅ 推荐!简洁、安全、无 `!!`、自动处理空值短路。 --- ## ✅ 总结 | 写法 | 是否推荐 | 说明 | |------|----------|------| | `config.customLayoutRes!!` | ❌ 推荐 | 易出 NPE,破坏空安全 | | `val r = c.r; if (r != null) use(r)` | ✅ 推荐 | 捕获局部变量,触发智能转换 | | `c.r?.let { use(it) }` | ✅✅ 强烈推荐 | 函数式风格,最安全 | --- 你现在明白了:**是你该用 `!!`,而是有更好的方式替代它**。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值