跳到正文

题解

B2019 整型与布尔型的转换 题解

rain_dew约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年12月15日
收录
2026年11月30日
更新
2026年11月30日
篇幅
约 1 分钟
获赞
5
题目
B2019

版本

  1. v1当前

本题解为 Python 题解,原题链接:B2019 整型与布尔型的转换

在 Python3 中有 bool() 函数,其真假返回机制与本题目相同,但如果是真返回 True,否则返回 False。需再次转换为 00 与 11 即可。

本题目代码如下:

Python
a=bool(int(input()));
if a==True:
    print(1);
else:
    print(0);

当然,如果当前并未学习 if() 函数,也可以通过 bool 型转为 int 型做题,代码如下:

Python
a=int(bool(int(input())));
print(a);
原文
抓取于
  1. YuanMingTai

    (为什么 Python 要分号)

  2. anguswan

    wtf

  3. liuyixiao01

    #include<iostream> using namespace std; int main(){ int a; cin>>a; cout<<bool(a); return 0; }

  4. chl18577299174

    不是不能把题解发布在评论区吗?

  5. ck2601086729

    #include<bits/stdc++.h> using namespace std; double a; int main() { cin>>a; a=(bool)a; a=(int)a; cout<<a; return 0; }

  6. kangjingxiang

    没什么难的 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; if(n==0) cout<<0; else cout<<1; return 0; }