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;
}
相关推荐
方也_arkling1 小时前
【Java-Day08】static / final / 枚举
java·开发语言
风吹夏回1 小时前
Python 全局异常处理:从“满屏 try-except”到优雅兜底
开发语言·python
Chengbei111 小时前
一站式源码安全检测工具、云安全 / APP / 小程序源码敏感信息递归多层目录扫描AK、JWT、手机号、身份证等敏感信息
java·开发语言·安全·web安全·网络安全·系统安全·安全架构
llz_1121 小时前
web-第一次课后作业
java·开发语言·idea
kkeeper~1 小时前
0基础C语言积跬步之数据在内存中的存储
c语言·数据结构·算法
小熊Coding1 小时前
Python爬取当当网二手图书项目实战!
开发语言·爬虫·python·beautifulsoup·requests·二手图书
秋92 小时前
Java项目运行5天左右自动宕机:系统性定位与解决方案
java·开发语言·python
xiaoshuaishuai82 小时前
C# 内存管理与资源泄漏
开发语言·c#
lsx2024062 小时前
SVN 检出操作
开发语言
basketball6163 小时前
C++ NULL 和 nullptr 区别 以及 nullptr 的核心实现
java·开发语言·c++