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;
}
相关推荐
草莓熊Lotso27 分钟前
C++ 二叉搜索树(BST)完全指南:从概念原理、核心操作到底层实现
java·运维·开发语言·c++·人工智能·经验分享·c++进阶
oliveira-time36 分钟前
单例模式中的饿汉式
java·开发语言
Go away, devil2 小时前
Java-----集合
java·开发语言
VBA63374 小时前
VBA即用型代码手册:利用函数保存为PDF文件UseFunctionSaveAsPDF
开发语言
say_fall4 小时前
C语言编程实战:每日刷题 - day2
c语言·开发语言·学习
上去我就QWER5 小时前
Qt快捷键“魔法师”:QKeySequence
开发语言·c++·qt
Pluto_CSND7 小时前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
惊讶的猫9 小时前
LSTM论文解读
开发语言·python
獨枭9 小时前
C# 本地项目引用失效与恢复全攻略
开发语言·c#·visual studio
国服第二切图仔10 小时前
Rust开发之Trait 定义通用行为——实现形状面积计算系统
开发语言·网络·rust