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;
}
相关推荐
一过菜只因10 小时前
JavaWeb后端(spring--boot)
java·开发语言
五仁火烧10 小时前
安装rust开发环境
开发语言·后端·rust
Yue丶越10 小时前
【C语言】动态内存管理
c语言·开发语言
Edward1111111110 小时前
普通java项目转为maven项目 J文件后缀.java变C文件
java·开发语言·maven
赵谨言10 小时前
基于OpenCV的图像梯度与边缘检测研究
大数据·开发语言·经验分享·python
莓莓儿~10 小时前
Next.js 14 App Router数据获取开发手册
开发语言·前端·javascript
wjs202410 小时前
ionic 单选框详解
开发语言
serendipity_hky10 小时前
【go语言 | 第3篇】go中类的封装、继承、多态 + 反射
开发语言·后端·golang·反射
石国旺10 小时前
python打包PyInstaller程序,怎么越来越大,如何解决?
开发语言·python
沐知全栈开发10 小时前
Memcached stats items 命令详解
开发语言