1. 给定两个整形变量的值,将两个值的内容进行交换。
#include <stdio.h>
#include<stdlib.h>
int main1()
{
int a = 10, b = 12;
int* p1 = &b;
int* p2 = &a;
int t;
t =* p1;
*p1 =* p2;
*p2 = t;
a = *p2;
b = *p1;
printf("%d,%d",a,b);
system("pause");
return 0;
}
怎么说呢,在写这个代码的时候,我认为是非常简单的,但是到论坛索了一下发现使用指针是较为准确的。
2. 不允许创建临时变量,交换两个数的内容(附加题)
#include<stdio.h>
int main2()
{
int a = 18, b = 22;
a = a + b;
b = a - b;
a = a - b;
printf("%d\n", a);
printf("%d\n",b );
return 0;
}
3.求10 个整数中最大值。
#include <stdio.h>
int main3()
{
int a[20] = { 12, 11, 22, 23, 34, 3, 21, 45, 66, 56 };
int i = 1;
int max = a[0];
for (i = 0; i < 10; i++)
{
if (a[i]>max)
max = a[i];
}
printf("max=%d", max);
return 0;
}
4.将三个数按从大到小输出。
#include <stdio.h>
int main4()
{
int a = 0;
int b = 0;
int c = 0;
int t = 0;
printf("Please enter three numbers: ");
scanf_s("%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("the sorted numbers are: ");
printf("%d,%d,%d\n", a, b, c);
return 0;
}
5.求两个数的最大公约数。
#include <stdio.h>
int main()
{
int a, b;
printf("enter two numbers: ");
scanf_s("%d,%d", &a, &b);
if (a < b)
{
int t = a;
a = b;
b = t;
}
while ((a%b) != 0)
{
int t = b;
b = a%b;
a = t;
}
printf("最大公约数是: ");
printf("%d", b);
return 0;
}
springbear
19-3.18