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;
}
相关推荐
阿伟来咯~9 分钟前
vue3+Nest.js项目 部署阿里云
开发语言·javascript·ecmascript
爱编程的鱼40 分钟前
C# 继承详解
开发语言·c#
MyhEhud43 分钟前
kotlin flatMap 变换函数的特点和使用场景
开发语言·windows·kotlin
杰仔正在努力1 小时前
Java + Seleium4.X + TestNG自动化技术
java·开发语言·自动化
神仙别闹1 小时前
基于C#窗体+GDI+绘图实现分形树
开发语言·c#
爱凤的小光1 小时前
图漾官网Sample_V1版本C++语言完整参考例子---单相机版本
开发语言·c++·数码相机
weixin_307779131 小时前
Azure Synapse Dedicated SQL pool企业权限管理
开发语言·数据仓库·sql·azure·etl
三思而后行,慎承诺1 小时前
Kotlin 常见问题
开发语言·面试·kotlin
青瓦梦滋1 小时前
【语法】C++的继承
开发语言·c++
一颗知足的心2 小时前
Go语言之路————接口、泛型
开发语言·golang