在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;
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短