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;
}
相关推荐
疯狂的喵2 小时前
C++编译期多态实现
开发语言·c++·算法
2301_765703142 小时前
C++中的协程编程
开发语言·c++·算法
m0_748708052 小时前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习3 小时前
【算法——c/c++]
c语言·c++·算法
lly2024063 小时前
jQuery Mobile 表格
开发语言
智码未来学堂3 小时前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
惊讶的猫3 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
m0_748233174 小时前
30秒掌握C++核心精髓
开发语言·c++
Fleshy数模4 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
Duang007_4 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python