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;
}
相关推荐
C^h11 小时前
c++ string
开发语言·c++
听风说ml11 小时前
socket编程 【C语言】
c语言·udp·tcp·socket编程
raindayinrain11 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的11 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao11 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
学逆向的12 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的13 小时前
C++第一节课
开发语言·c++
“AI国潮设计-小江”13 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
小王C语言13 小时前
Qt常用控件:容器类控件,标题分组框、标签页(浏览器标签)
开发语言·qt
我是唐青枫13 小时前
Kotlin Delegation 委托详解
开发语言·kotlin