C语言学习笔记:07_交换两个数的多个方法
2015-07-05 13:53
651 查看
/* * 07_交换两个数的多个方法.c * * Created on: 2015年7月4日 * Author: zhong */ //交换两类的几种算法 #include <stdio.h> #include <stdlib.h> //1:使用第三方变量 void use_temp(){ int a=10,b=11; int temp=a; a=b; b=temp; printf("使用第三方变量:a=%d,b=%d\n",a,b); } //2:使用指针交换两个数 void user_swap(int *a, int *b) { int c; c = *a; *a = *b; *b = c; printf("使用指针:a=%d,b=%d\n",a,b); } //3:使用+ -法一 :这种方法当a,b过大,相加时可能会溢出 void use_math_add_sub_01(){ int a=10,b=11; a=a+b; b=a-b; a=a-b; printf("使用加减法一:a=%d,b=%d\n",a,b); } //4:使用+ -法二 void use_math_add_sub_02(){ int a=10,b=11; a=b-a; //1 b=b-a; //11-1=10 a=a+b; // 1+10=11 printf("使用加减法二:a=%d,b=%d\n",a,b); } //4:使用^(异或)算法 void use_or(){ int a=10,b=11; a=a^b; //1 b=a^b; //11-1=10 a=a^b; // 1+10=11 printf("使用^(异或)算法:a=%d,b=%d\n",a,b); } int main7() { use_temp(); use_math_add_sub_01(); use_math_add_sub_02(); use_or(); // system("pause"); }
相关文章推荐
- C语言学习笔记:06_逻辑运算符和逻辑表达式
- C语言学习笔记:05_关系运算符和关系表达式
- C语言学习笔记:01_基本数据类型
- C语言学习笔记:04_选择结构
- C语言学习笔记:02_输入输出函数
- C/C++ Volatile关键词深度剖析
- c++面试题
- c++ public、private和protected区别
- 【GNU】自由软件开发C&C++(环境安装篇)
- 关于C++中用两个迭代器方式初始化string的知识
- 算法导论学习---红黑树详解之插入(C语言实现)
- C++关于磁盘的数据的操作
- 【C语言】编写函数实现库函数atof
- 【C语言】编写函数实现库函数atoi,把字符串转换成整形(考虑数字过大)
- VC++6.0 Debug单步调试简单入门
- C++ traits技术
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- 重温《C++ Primer》笔记九 const形参小结
- 《C语言及程序设计》实践参考——链表版通信录
- 《C语言及程序设计》实践参考——改造链表