0
点赞
收藏
分享

微信扫一扫

两数值交换的多种方式

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

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

#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;
}

举报

相关推荐

0 条评论