编写一个在C编程中返回输入为1时返回2,输入为2时返回1的函数

编写一个在C编程中返回输入为1时返回2,输入为2时返回1的函数

需要制作一个函数,对于输入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

以上就是编写一个在C编程中返回输入为1时返回2,输入为2时返回1的函数的详细内容,更多请关注编程之家其它相关文章

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...