对于一些简单的C语言程序的学习,我发现还是有一定的规律。在下边的几个C语言程序中,可以发现,几乎每个里边都用到了C语言中的“交换”,如果能够了解对它运用,那么能够学到好多东西。
- 给定两个整形变量的值,将两个值的内容进行交换。
#include<stdlib.h>
#include<stdio.h>
int main(){
int a = 1;
int b = 2;
int c;
c = a;
a = b;
b = c;
printf("%d %d\n", a, b);
system(“pause”);
return 0;
} - 不允许创建临时变量,交换两个数的内容。
#include<stdlib.h>
#include<stdio.h>
int main(){
int a = 2;
int b = 5;
a = a + b;
b = a - b;
a = a - b;
printf("%d %d\n", a, b);
system(“pause”);
return 0;
}
3.求10 个整数中最大值。
#include<stdlib.h>
#include<stdio.h>
int main(){
printf(“输入十个整数:\n”);
int i, MAX, n;
scanf("%d", &n);//输入第一个数 //可以将第一次的N放入循环之中,对输入次数进行判定也可以达到目的。
MAX = n;//假设第一个数是最大的整数
for (i = 2; i <= 10; i++){
scanf("%d", &n);
if (n > MAX){
MAX = n;//下一个数大于第一个数
}
}
printf("%d\n", MAX);//输出较大的数
system(“pause”);
return 0;
}
4.将三个数按从大到小输出。
#include<stdlib.h>
#include<stdio.h>
int main(){
printf(“输入三个数字:\n”);
int a, b, c, d;
scanf("%d %d %d", &a, &b, &c);
if (b > a){//先将三个数两两比较,然后按小大排列,之后输出换回去即可。
d = a;
a = b;
b = d;
}if (c > a){
d = a;
a = c;
c = d;
}if (b > c){
d = c;
c = b;
b = d;
}printf("%d %d %d\n", a, c, b);
system(“pause”);
return 0;
}
5.求两个数的最大公约数。
#include<stdlib.h>//辗转相除法
#include<stdio.h>
int main(){
int a, b, temp;//将余数定义为变量
printf(“输入两个数:\n”);
scanf("%d %d", &a, &b);//取地址
if (a > b){
while (a%b != 0){
temp = a%b;
a = b;//交换法
b = temp;
} printf("%d\n", b);
}
if(a < b){
while (b%a != 0){
temp = b%a;
b = a;
a = temp;
} printf("%d\n", a);
}
system(“pause”);
return 0;
}