在C / C ++中交换两个变量值而不使用第三个变量
以下是交换两个变量的示例。
示例
#include <stdio.h>
int main() {
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b);
a += b -= a = b - a;
printf("\nAfter Swapping : %d\t%d", a, b);
return 0;
}输出结果
Enter the value of a : 23 Enter the value of b : 43 After Swapping : 4323
在上面的程序中,声明了两个变量a和b,并在运行时对其进行了动态初始化。
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b);交换数字时不使用任何第三个变量。
a += b -= a = b - a;
热门推荐
8 敏字的祝福语简短
10 高温早安祝福语简短精辟
11 生日祝福语短句简短霸气
12 生日祝福语古语简短创意
13 送菜的祝福语简短霸气
14 祝福语简短大全微信
15 新年祝福语简短创意兄弟
16 毕业贺卡老师祝福语简短
17 祝福语简短唯美10字
18 猪年元旦祝福语简短创意