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 分钟前
C++右值与右值引用
开发语言·c++
IT猿手8 分钟前
基于 CBF 的多无人机编队动态避障路径规划研究,无人机及障碍物数量可以自定义修改,MATLAB代码
开发语言·matlab·无人机·动态路径规划
炸膛坦客10 分钟前
单片机/C/C++八股:(十六)C 中 malloc/free 和 C++ 中 new/delete 有什么区别?
c语言·开发语言·c++
@insist12310 分钟前
软件设计师-组网技术基础:网络设备、传输介质与局域网核心协议
开发语言·网络·软考·软件设计师·软件水平考试
Navigator_Z19 分钟前
LeetCode //C - 990. Satisfiability of Equality Equations
c语言·算法·leetcode
CSDN_Colinw25 分钟前
C++中的工厂方法模式
开发语言·c++·算法
liulilittle28 分钟前
范围随机算法实现
开发语言·c++·算法·lua·c·js
乌索普-31 分钟前
基于vue2的简易购物车
开发语言·前端·javascript
走粥33 分钟前
使用indexOf查找对象结合Pinia持久化引发的问题
开发语言·前端·javascript
csbysj20201 小时前
Python break 语句详解
开发语言