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;
}
相关推荐
JaJian.3 分钟前
Java后端服务假死问题排查与解决
java·开发语言
jjjxxxhhh12344 分钟前
RSA加密解密代码
开发语言·c++
Sun_gentle1 小时前
java.lang.RuntimeException: Could not load wrapper properties from ‘C:\Users\
java·开发语言·安卓
键盘鼓手苏苏1 小时前
Flutter for OpenHarmony:git 纯 Dart 实现的 Git 操作库(在应用内实现版本控制) 深度解析与鸿蒙适配指南
开发语言·git·flutter·华为·rust·自动化·harmonyos
橙露1 小时前
面向对象编程思想:Java 与 Python 的封装、继承与多态对比分析
java·开发语言·python
ShineWinsu1 小时前
对于C++:模版进阶的解析
开发语言·c++·面试·笔试·求职·进阶·模版
啊哈哈121382 小时前
Python基本语法复盘笔记1(输入输出/字符串/列表)
开发语言·笔记·python
qq_150841992 小时前
3天基于VS2026的C#编程入门及动态调用CH341DLLA64读写I2C从机
开发语言·c#
祈安_3 小时前
深入理解指针(一)
c语言·前端
Tony Bai3 小时前
Go 1.26 :go mod init 默认行为的变化与 Go 版本管理的哲学思辨
开发语言·后端·golang