Given the value 1. The two integer variables, the contents of the two values to be exchanged.
This topic and ideas: the definition of temporary variables c
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 3, b = 4, c; //定义变量
printf("初始值:%d %d\n", a, b);
c = a;
a = b;
b = c;
printf("交换后:%d %d\n", a, b);
system("pause");
return 0;
}
Execution results are as follows:
2 does not allow the creation of temporary variables, swap the contents of two numbers (additional topic)
This topic ideas: the use of the difference between a and b are the exchange operation
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 3, b = 4;
printf("初始值:%d %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("交换后:%d %d\n", a, b);
system("pause");
return 0;
}
Execution results are as follows:
3. The maximum value of 10 integers requirements.
This question ideas: find the maximum without defined pointer variable.
Note: It is recommended not to use arrays compare apples to apples, because the array index must be a positive integer or a logical value.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num, max=0x80000000;//给max附一个最小值
for (int i = 0; i < 10; i++){
scanf("%d", &num);
if (num>max)
max = num;
}
printf(" %d\n", max );
system("pause");
return 0;
}
Performed as follows:
4. The three numbers in descending output
this question ideas : pairwise comparison, to ensure a large exchange in the small front
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b, c, m;
scanf(" %d %d %d" ,&a,&b,&c);
if (a<b)
{
m = a; a = b; b = a;
}
if (a<c)
{
m = a; a = c; c = a;
}
if (b<c)
{
m = b; b = c; c = b;
}
printf("从大到小排序为:%d %d %d\n", a, b, c);
system("pause");
return 0;
}
Performed as follows:
The greatest common divisor of two numbers.
Outline of Solution 1: for i traversed, whether simultaneously be divisible by two integers (slower)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=0, b=0, max=0;
scanf("%d %d", &a, &b);
int smaller;
smaller = a > b ? b : a;
for (int i = 2; i <= smaller; i++){
if (a%i == 0 && b%i == 0){
max = i;
}
}
printf("最大公约数:%d\n", max);
system("pause");
return 0;
}