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;
}
相关推荐
维度攻城狮3 分钟前
ros2参数通信案例
开发语言·windows·python·ros2·参数通信
清水白石00812 分钟前
Python 与尾递归:为何不优化?如何优雅绕过?
开发语言·python
王大傻092819 分钟前
使用python for循环与ord() + chr()实现字符串加密
开发语言·python
Louis Maos25 分钟前
堆与栈分配的本质区别
java·开发语言
毕设源码-朱学姐1 小时前
【开题答辩全过程】以 果蔬禽蛋生鲜食品采购配送系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
不要em0啦1 小时前
从0开始学python:函数与数据容器
开发语言·python
zfj3211 小时前
java线程的不同状态下调用interrupt()方法后的行为及注意事项
java·开发语言·interrupt·线程中断·线程状态
XLYcmy2 小时前
高级密码猜测生成器AdvancedPasswordGenerator密码生成器程序详细分析
开发语言·python·算法·网络安全·开发工具·源代码·口令安全
廋到被风吹走2 小时前
【Java】【Jdk】Jdk17->Jdk21
java·开发语言
2201_761199042 小时前
7.statefulset
开发语言·kubernetes·php