跳到正文

题解

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

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

文章信息

发布
2021年7月3日
收录
2026年11月30日
更新
2026年11月30日
篇幅
约 1 分钟
获赞
41
题目
B2019

版本

  1. v1当前

int 如何转 bool 呢?以下摘自百度百科:

代码
bool取值false和true,0为false,非0为true。(例如-1和2都是true)。

所以这道题,如果输入的这个数不等于 0 ,输出的值就是 1 。否则就输出 0 。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a;
	cin >> a;
	if (a != 0) cout << 1;
	else cout << 0;
	return 0;
}
原文
抓取于
  1. liyangxi789113

    其实可以不用把bool型的转化进int/long long类型,直接输出就行

  2. liyangxi789113

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

  3. goat6666

    #include<bits/stdc++.h> using namespace std; int main() { int b; cin>>b; cout<<(bool)b; }//其实这个方法更简单

  4. Zhangruoyu20151114

    三目运算符了解一下:int n; cin>>n; cout<<(n ? 1 :0) (是不是更简单)

  5. ZJY20141004

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

  6. Colorful_W330

    cout<<(bool)a;

  7. #SUSPENDED#

    这样更简单: #include<iostream> int main() { int a; std::cin>>a; std::cout<<!!a; return 0; }

  8. ZHFCZ

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

  9. martin0701

    英雄所见略同

  10. luogu_mm

    very good