#include<stdio.h>
#include<stdlib.h>
int main() {
int a = 2;
int b = 3;
int c;
printf("初始时:\n");
printf("a=%d b=%d\n", a, b);
c = a;
a = b;
b = c;
printf("交换后:\n");
printf("a=%d b=%d\n", a, b);
system("pause");
return 0;
}
2 不允许创建临时变量,交换两个数的内容
#include<stdio.h>
#include<stdlib.h>
int main() {//可以使用加减法和异或法,不过加减法需要考虑溢出
int a = 2;
int b = 3;
printf("初始时:\n");
printf("a=%d b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后:\n");
printf("a=%d b=%d\n", a, b);
system("pause");
return 0;
}
3 求十个整数中的最大值
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main() {
int x;
int arr[10];
printf("请输入十个整数:\n");
for (x = 0; x <= 9; x++) {
scanf("%d", &arr[x]);
}
int a = arr[0];
for (int y = 1; y <= 9; y++) {
if (a < arr[y]) {
a = arr[y];
}
}
printf("%d\n ", a);
system("pause");
return 0;
}
4 将三个数按从大到小输出
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main() {
int a, b, c, t;
printf("请输入a,b,c三个数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
t = a;
a = b;
b = t;
}
if (a > c) {
t = a;
a = c;
c = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
printf("从大到小排列为:\n");
printf("%d %d %d\n", c, b, a);
system("pause");
return 0;
}
5 求两个数的最大公约数
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main() {
int a;
int b;
int t;
printf("请输入两个整数:\n");
scanf("%d %d", &a, &b);
if (a < b) {
t = a;
a = b;
b = t;
}
while (b != 0) {
t = a % b;
a = b;
b = t;
}
printf("最大公约数为:\n");
printf("%d\n", a);
system("pause");
return 0;
}
1 给定两个整形变量的值,将两个值的内容交换#include<stdio.h>#include<stdlib.h>int main() { int a = 2; int b = 3; int c; printf("初始时:\n"); printf("a=%d b=%d\n", a, b); c = a; a = b; b = c; printf("交...