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;
}
相关推荐
hanbr2 小时前
C++ 初涉
开发语言·c++
Дерек的学习记录2 小时前
C++:入门基础(下)
开发语言·数据结构·c++·学习·算法·visualstudio
云小逸3 小时前
【nmap源码解析】Nmap 核心技术深度解析:从源码到实战
开发语言·网络·windows·nmap
前路不黑暗@3 小时前
Java项目:Java脚手架项目的公共模块的实现(二)
java·开发语言·spring boot·学习·spring cloud·maven·idea
人道领域3 小时前
Spring核心注解全解析
java·开发语言·spring boot
云深麋鹿4 小时前
标准库中的String类
开发语言·c++·容器
myron66884 小时前
基于STM32LXXX的模数转换芯片ADC(MCP3421A0T-E/CH)驱动C程序设计
c语言·stm32·嵌入式硬件
脱离语言5 小时前
Jeecg3.8.2 前端经验汇总
开发语言·前端·javascript
MOONICK5 小时前
C#基础入门
java·开发语言
女王大人万岁5 小时前
Golang标准库 CGO 介绍与使用指南
服务器·开发语言·后端·golang