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;
}
相关推荐
卢锡荣38 分钟前
单芯双 C 盲插,一线通显电 ——LDR6020P 盲插 Type‑C 显示器方案深度解析
c语言·开发语言·ios·计算机外设·电脑
legendary_16339 分钟前
PD显示器方案新维度:Type-C充电,投屏,显示技术革新
c语言·开发语言·计算机外设
ic爱吃蓝莓40 分钟前
美团测开一面
java·开发语言
sghuter40 分钟前
AI赋能CI/CD:Gemini实战脚本生成
开发语言·人工智能·ci/cd·青少年编程·r语言
me83240 分钟前
【深入java语句】关于System.out.println();的底层逻辑
java·开发语言
1candobetter44 分钟前
JAVA后端开发——多模块 Maven 项目 POM 管理规范实践
java·开发语言·maven
光电笑映1 小时前
深入C++异常:栈展开、异常安全与工程规范
开发语言·c++·c
码农爱学习1 小时前
用简单的例子,来理解C指针
c语言·开发语言
敲敲千反田1 小时前
CMS和G1
java·开发语言·jvm
sycmancia1 小时前
Qt——Qt中的文件操作、文本流和数据流
开发语言·qt