练一练_2

给定两个整形变量的值,将两个值的内容进行交换。
#include<stdio.h>
#include<stdlib.h>

int main() {
int num1 = 11;
int num2 = 89;
int temp = num1;
num1 = num2;
num2 = temp;
printf(“num1=%d\nnum2=%d\n”, num1,num2);
system(“pause”);
return 0;
}

不允许创建临时变量,交换两个数的内容
#include<stdio.h>
#include<stdlib.h>
int main() {
int num1 = 11;
int num2 = 67;
num1 = num1 + num2 ;
num2 =num1-num2;
num1 = num1 - num2;
printf(“num1=%d\nnum2=%d\n”, num1, num2);
system(“pause”);
return 0;
}

求10 个整数中最大值
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int compare(int *arr) {
int max = 0;
int i = 0;
for ( i = 0; i <=9; ++i) {

	if (arr[i] > max) {
		max = arr[i];
	}
}
return max;

}
int main() {
int arr[10] = {0};
printf(“输入十个整数:\n”);
int i = 0;
for ( i = 0; i <10; i++)
{
scanf("%d ", &arr[i]);
}
int result = 0;
result=compare(arr);
printf(“这十个整数中的最大值是%d”,result);
system(“pause”);
return 0;
}

将三个数按从大到小输出。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main() {
int num1,num2,num3;
printf(“输入三个数: “);
scanf(”%d%d%d”, &num1, &num2, &num3);
if (num1 < num2) {
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;

}
if (num1 < num3) {
	num1 = num1 + num3;
	num3 = num1 - num3;
	num1 = num1 - num3;

}
if (num2 < num3) {
	
	num2 = num2 + num3;
	num3 = num2 - num3;
	num2 = num2 - num3;
}
printf("将他们从大到小排列: %d  %d  %d\n", num1, num2, num3);
system("pause");
return 0;

}

求两个数的最大公约数

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

int commonmeasure(int x,int y){
if (x <y) {
x = x + y;
y = x - y;
x = x - y;
}
int t = x%y;
if (t != 0){
x = y;
y = t;
commonmeasure(x, y);
}
else
return y;
}
int main() {

int num1, num2;
printf("输入两个整数,求他们的最大公约数:");
scanf("%d%d", &num1, &num2);
int result= commonmeasure(num1,num2);
printf("%d和%d的最大公约数是%d\n", num1, num2, result);

system("pause");
return 0;

}

上一篇:C#基础:可空类型


下一篇:uva-211-The Domino Effect