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;
}
相关推荐
chilavert318几秒前
技术演进中的开发沉思-230 Ajax:Prototype.js 重构原生 DOM
开发语言·前端·javascript
csbysj20206 分钟前
SVN 标签
开发语言
2501_930707788 分钟前
如何在 C# 中分离饼图的某个区域
开发语言·c#
缺点内向12 分钟前
如何在C#中添加Excel文档属性?
开发语言·数据库·c#·.net·excel
Howie Zphile22 分钟前
做移动端的 Next.js 项目,可以选哪些 UI?
开发语言·javascript·ui
无限进步_25 分钟前
C语言文件操作函数解析
c语言·开发语言·数据库·c++·后端·visual studio
星辰烈龙30 分钟前
黑马程序员Java基础7
java·开发语言
big-seal32 分钟前
分页列表中能够按照名称查询,使用 mybatis 的 Example 动态构造 SQL where 条件
java·开发语言
福尔摩斯张32 分钟前
C语言文件操作详解(一):文件的打开与关闭(详细)
java·linux·运维·服务器·c语言·数据结构·算法
white-persist32 分钟前
【攻防世界】reverse | answer_to_everything 详细题解 WP
c语言·开发语言·汇编·python·算法·网络安全·everything