- 给定两个整形变量的值,将两个值的内容进行交换。
#include <stdio.h>
#include <stdlib.h>
int Cbl1() {
int a = 1, b = 2;
int t;//临时变量
printf("a=%d,b=%d",a,b);
t = a;
a = b;
b = t;
printf("a=%d,b=%d", a, b);
system("pause");
return 0;
}
- 不允许创建临时变量,交换两个数的内容(附加题)
#include <stdio.h>
#include <stdlib.h>
int Cbl2() {
int a = 1, b = 2;
printf("a=%d,b=%d\n", a, b);
a = a - b;
b = a + b;
a = b - a;
printf("a=%d,b=%d\n", a, b);
system("pause");
return 0;
}
3.求10 个整数中最大值。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int maxNum() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
int max=0;
for (i;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 num3() {
int a, b, c, t; /*定义4个基本整型变量a、b、c、t*/
printf("Please input a,b,c:\n"); /*双引号内的普通字符原样输出并换行*/
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
/*输入任意3个数*/
if (a > b) /*如果a大于b,借助中间变量t实现a与b值的互换*/
{
t = a;
a = b;
b = t;
}
if (a > c) /*如果a大于c,借助中间变景t实现a与c值的互换*/
{
t = a;
a = c;
c = t;
}
if (b > c) /*如果b大于c,借助中间变量t实现b与c值的互换*/
{
t = b;
b = c;
c = t;
}
printf("The order of the number is:\n");
printf("%d>%d>%d", c, b, a); /*输出函数顺序输出a、b、c的值*/
system("pause");
return 0;
}
5.求两个数的最大公约数。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int gcd()
{
int m, n, temp, i;
printf("Input m & n:");
scanf("%d", &m);
scanf("%d", &n);
if (m < n) /*比较大小,使得m中存储大数,n中存储小数*/
{ /*交换m和n的值*/
temp = m;
m = n;
n = temp;
}
for (i = n; i > 0; i--) /*按照从大到小的顺序寻找满足条件的自然数*/
if (m%i == 0 && n%i == 0)
{/*输出满足条件的自然数并结束循环*/
printf("The GCD of %d and %d is: %d\n", m, n, i);
break;
}
system("pause");
return 0;
}