1. 给定两个整形变量的值,将两个值的内容进行交换。
int i = 1,
j = 2,
temp = 0;
temp = i;
i = j;
j = temp;
printf("i:%d j:%d", i, j);
2. 不允许创建临时变量,交换两个数的内容
int i = 1,
j = 2;
i = i + j;
j = i - j;
i = i - j;
printf("i:%d j:%d", i, j);
3.打印10个整数最大值
int num[10] = {6,10,2,4,6,1,8,9,3,5};
int j,
max = num[0],
len = sizeof(num) / sizeof(num[0]);
for (j = 1; j < len; j++) {
if (num[j] > max)
max = num[j];
}
printf("这十个整数中的最大数是:%d\n", max);
4.将三个数按从大到小输出。
int a, b, c, temp;
printf("输入三个数:\n");
scanf_s("%d %d %d", &a, &b, &c);
if (a < b) {
temp = a; a = b; b = temp;
}
if (b < c) {
temp = b; b = c; c = temp;
}
if (a < b) {
temp = a; a = b, b = temp;
}
printf("从大到小:%d %d %d\n", a, b, c);
5.求两个数的最大公约数
int x, y, temp;
printf("请输入两个数\n");
scanf_s("%d,%d", &x, &y);
if (x < y) {
temp = x;
x = y;
y = temp;
}
for (temp = y; x % temp || y % temp; temp --);
printf("最大公约数为:%d\n", temp);