1. 给定两个整形变量的值,将两个值的内容进行交换
//给定两个整形变量的值,将两个值的内容进行交换
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 1, b = 2, c;
c = a;
a = b;
b = c;
printf("%d %d\n", a, b);
system("pause");
return 0;
}
2. 不允许创建临时变量,交换两个数的内容
//不允许创建临时变量,交换两个数的内容
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 1, b = 2;
a = a + b;
b = a - b;
a = a - b;
printf("%d %d", a, b);
system("pause");
return 0;
}
3. 求10 个整数中最大值
//求10 个整数中最大值
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = { 150,100,30,120,101,60,70,80,0,0 };
int max = arr[0]; //假设数组中一个数为最大值
int i = 0;
for (i = 0; i <= 9; i++) {
if (arr[i] > max) { //将其他数与最大值进行比较
max = arr[i];
}
}
printf("%d\n", max);
system("pause");
return 0;
}
4. 将三个数按从大到小输出
//将三个数按从大到小输出
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a < b) { //将大的数交换到前面
int t = a;
a = b;
b = t;
}
if (b < c) {
int t = b;
b = c;
c = t;
}
if (a < b) {
int t = a;
a = b;
b = t;
}
printf("%d>%d>%d", a, b, c);
system("pause");
return 0;
}
5. 求两个数的最大公约数
//求两个数的最大公约数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a, b, i;
scanf("%d %d", &a, &b);
if (a > b) {
i = b;
}
else {
i = a;
}
for (; i > 0; i--) {
if ((a%i == 0)&&(b%i == 0)) {
printf("%d\n", i);
break; //从大往小查,可同时整除两个数则跳出循环
}
}
system("pause");
return 0;
}

被折叠的 条评论
为什么被折叠?



