C练手题--Exclusive “or“ (xor) Logical Operator 【8 kyu】

一、原题

链接:Training on Exclusive "or" (xor) Logical Operator | Codewars

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Exclusive "or" (xor) Logical Operator Overview In some scripting languages like PHP, there exists a logical operator (e.g. &&, ||, and, or, etc.) called the "Exclusive Or" (hence the name of this Kata). The exclusive or evaluates two booleans. It then returns true if exactly one of the two expressions are true , false otherwise. For example: |

测试用例

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| false xor false == false // since both are false true xor false == true // exactly one of the two expressions are true false xor true == true // exactly one of the two expressions are true true xor true == false // Both are true. "xor" only returns true if EXACTLY one of the two expressions evaluate to true. |

二、解题

1、分析

两者相同返回false,两者不同返回true。

2、思路

(1)a==b return false;

(2)a!=b return true;

三、Myway

cs 复制代码
#include <stdbool.h>

bool xor(bool a, bool b) {
  if(a==b)
     
    return false;
  else
    return true;
}
相关推荐
沐知全栈开发2 小时前
Perl 数据库连接
开发语言
森叶2 小时前
Java 比 Python 高性能的原因:重点在高并发方面
java·开发语言·python
qq_316837752 小时前
uni.chooseMedia 读取base64 或 二进制
开发语言·前端·javascript
方圆工作室2 小时前
【C语言图形学】用*号绘制完美圆的三种算法详解与实现【AI】
c语言·开发语言·算法
小二·3 小时前
Python Web 开发进阶实战:混沌工程初探 —— 主动注入故障,构建高韧性系统
开发语言·前端·python
Lkygo3 小时前
LlamaIndex使用指南
linux·开发语言·python·llama
进阶小白猿3 小时前
Java技术八股学习Day20
java·开发语言·学习
代码村新手3 小时前
C++-类和对象(中)
java·开发语言·c++
葵花楹3 小时前
【JAVA课设】【游戏社交系统】
java·开发语言·游戏
赵谨言4 小时前
Python串口的三相交流电机控制系统研究
大数据·开发语言·经验分享·python