首页 > 其他分享 >两数值交换的多种方式

两数值交换的多种方式

时间:2023-01-21 11:00:25浏览次数:35  
标签:多种 int 交换 数值 else printf input include strcmp

我现在只知道这三种方式,应该还有很多种

有大佬能帮我看看最后那里怎么弄吗,输入其他的还是会出结果

#include <stdio.h>
//#include <stdlib.h>
#include <string.h>

int main()
{
int a = 25;
int b = 52;
char input[10]={0};
printf("以下有三种求两数交换的方式\n");
scanf("%s",input);
if(strcmp(input,"第一种"))

{
//采用第三变量交换法
int temp=a;
a=b;
b=temp;
printf("%d %d",a,b);
}
//不用第三变量
else if(strcmp(input,"第二种"))
{
a=a+b;
a=a-b;
b=a-b;
printf("%d %d",a,b);
}
else if(strcmp(input,"第三种"))
{
a=a^b;
a=a^b;
b=a^b;
printf("%d %d",a,b);
}
else
{
printf("输入非法,请重新输入");
}
return 0;
}

标签:多种,int,交换,数值,else,printf,input,include,strcmp
From: https://blog.51cto.com/u_15943742/6021092

相关文章