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;
}
相关推荐
C++ 老炮儿的技术栈6 小时前
我们在设计tcp协议时,要传一个字符串过去,报文:头十长度十内容,是否要把‘\0‘也填入,长度是否包含‘\0‘
开发语言·数据结构·c++·mfc·c
传奇开心果编程7 小时前
【Rust入门练中学】第2课:变量、可变性与常量
开发语言·学习·rust
zr想努力7 小时前
从0开始配置Perforce4环境
开发语言
Navigator_Z8 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
幸运小圣8 小时前
PointerEvent 常用属性与事件整理【JavaScript】
开发语言·javascript·ecmascript
长谷深风1119 小时前
评测AI Agent:三种裁判各司其职
java·大数据·开发语言·人工智能·ai agent
边境悍匪9 小时前
蜗牛学苑 Java 智能体学习 Day42|项目周开发技术汇总 1 思维导图复盘
java·开发语言·spring boot·学习·spring
君顾110 小时前
本地托管机构管理源码实战:需求拆解、数据建模与多端部署
java·开发语言·托管
是隼人10 小时前
buuctf-pwn [NewStarCTF 2023 公开赛道]stack migration(64位栈迁移)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
传奇开心果编程11 小时前
【Rust入门练中学】 第3课:数据类型
开发语言·学习·rust