编写一个在输入 1 时返回 2,在输入 2 时返回 1 的 C 编程函数
要编写一个在输入 1 时返回 2、在输入 2 时返回 1 的函数。根据所使用的逻辑,有许多方法可以编写此函数。最简单的方法是使用条件语句,即如果数字为 1 则返回 2,否则返回 1,其他方法包括使用数学运算(任何方法都可以)和异或运算。
举例
#include <stdio.h>
// Method 1 using the if statement
int reverseif(int x) {
if (x == 1) return 2;
else return 1;
}
// Method 2 using the subtarction form sum of the two numbers (3 in this case)
int reversesub(int x){
return (3-x);
}
int main() {
printf("%d
", reverseif(1));
printf("%d
", reversesub(2));
return 0;
}输出
2 1
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP