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号2 分钟前
Qt5之中文字符串转换
开发语言·qt
CoderYanger15 分钟前
C.滑动窗口-求子数组个数-越短越合法——LCP 68. 美观的花束
java·开发语言·数据结构·算法·leetcode
stanleyrain16 分钟前
C++中关于const的说明
开发语言·c++
froginwe1116 分钟前
Git 安装配置
开发语言
萧鼎20 分钟前
Python PyWavelets(pywt)库完整技术指南:从小波理论到工程实践
开发语言·python
EXtreme3520 分钟前
【C语言/数据结构】零基础打造控制台游戏:贪吃蛇实战教程----链表与Win32 API的完美结合!
c语言·数据结构·链表·贪吃蛇·宽字符·win32 api·控制台编程
天下无敌笨笨熊24 分钟前
kotlin函数式编程
开发语言·数据库·kotlin
MediaTea27 分钟前
Python 装饰器:@property_name.deleter
开发语言·python
清风拂山岗 明月照大江35 分钟前
TCP/IP网络编程_hello,world!
开发语言·c++
向阳是我36 分钟前
v0.app的next.js项目自动部署到宝塔服务器教程
服务器·开发语言·javascript·github·ai编程