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;
}
相关推荐
leo_2322 小时前
从开发语言角度来谈谈SMP(上)--SMP(软件制作平台)语言基础知识之十三
开发语言·开发工具·smp(软件制作平台)·应用系统
Larry_Yanan10 小时前
Qt多进程(三)QLocalSocket
开发语言·c++·qt·ui
醒过来摸鱼10 小时前
Java classloader
java·开发语言·python
superman超哥10 小时前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
小鸡吃米…10 小时前
Python - 继承
开发语言·python
JIngJaneIL11 小时前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
祁思妙想11 小时前
Python中的FastAPI框架的设计特点和性能优势
开发语言·python·fastapi
唐装鼠11 小时前
rust自动调用Deref(deepseek)
开发语言·算法·rust
Lucas5555555511 小时前
现代C++四十不惑:AI时代系统软件的基石与新征程
开发语言·c++·人工智能
源代码•宸11 小时前
goframe框架签到系统项目(BITFIELD 命令详解、Redis Key 设计、goframe 框架教程、安装MySQL)
开发语言·数据库·经验分享·redis·后端·mysql·golang