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;
}
相关推荐
mjhcsp29 分钟前
C++ 背包DP解析
开发语言·c++
尘缘浮梦40 分钟前
协程asyncio入门案例 2
开发语言·python
juleskk43 分钟前
2.15 复试训练
开发语言·c++·算法
一个处女座的程序猿O(∩_∩)O1 小时前
Python面向对象的多态特性详解
开发语言·python
yngsqq1 小时前
多段线顶点遍历技巧(适用闭合和非闭合)
开发语言
宇木灵1 小时前
C语言基础-五、数组
c语言·开发语言·学习·算法
xyq20242 小时前
空对象模式
开发语言
宇木灵2 小时前
C语言基础-三、流程控制语句
java·c语言·前端
不懒不懒3 小时前
【Python办公自动化进阶指南:系统交互与网页操作实战】
开发语言·python·交互
普通网友3 小时前
C++与Rust交互编程
开发语言·c++·算法