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;
}
相关推荐
q5673152310 分钟前
IBM官网新闻爬虫代码示例
开发语言·分布式·爬虫
能工智人小辰11 分钟前
Codeforces Round 509 (Div. 2) C. Coffee Break
c语言·c++·算法
笨笨马甲16 分钟前
附加模块--Qt OpenGL模块功能及架构
开发语言·qt
钮钴禄·爱因斯晨1 小时前
Java 面向对象进阶之多态:从概念到实践的深度解析
java·开发语言·数据结构
鸽子炖汤1 小时前
Java中==和equals的区别
java·开发语言·jvm
有个傻瓜1 小时前
PHP语言核心技术全景解析
开发语言·kubernetes·php
菥菥爱嘻嘻2 小时前
JS手写代码篇---手写ajax
开发语言·javascript·ajax
江梦寻2 小时前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
霖檬ing2 小时前
Python——MySQL远程控制
开发语言·python·mysql
qq_429879673 小时前
省略号和可变参数模板
开发语言·c++·算法