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;
}
相关推荐
我能坚持多久3 分钟前
D22—C语言预处理详解:从宏定义到条件编译
c语言·开发语言
小猪咪piggy5 分钟前
【Python】(3) 函数
开发语言·python
青岑CTF14 分钟前
攻防世界-Php_rce-胎教版wp
开发语言·安全·web安全·网络安全·php
初次见面我叫泰隆31 分钟前
Qt——4、Qt窗口
开发语言·qt·客户端开发
瑞雪兆丰年兮35 分钟前
[从0开始学Java|第十一天]学生管理系统
java·开发语言
代码AI弗森1 小时前
Git Bash 与 PowerShell:定位差异、使用场景与选择建议
开发语言·git·bash
代码游侠1 小时前
C语言核心概念复习(一)
c语言·开发语言·c++·笔记·学习
Once_day1 小时前
C++之《Effective C++》读书总结(3)
c语言·c++
蜕变的土豆1 小时前
grpc-通关速成
开发语言·c++
-To be number.wan1 小时前
Python数据分析:英国电商销售数据实战
开发语言·python·数据分析