您的位置:首页 > 编程语言

基础编程02

2018-03-23 09:40 30 查看
1. 给定两个整形变量的值,将两个值的内容进行交换。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
int temp;
temp = i;
i = j;
j = temp;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
2. 不允许创建临时变量,交换两个数的内容(附加题)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
i = i + j;
j = i - j;
i = i - j;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 20;
i = i ^ j;
j = i ^ j;
i = i ^ j;
printf("i=%d,j=%d\n", i, j);
system("pause");
return 0;
}
3.求10 个整数中最大值。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[10] = { 40, 35, 2, 34, 54, 67, 23, 42, 46, 78 };
int i,temp;
int max = arr[0];
for (i = 0; i < 10; i++)
{
if (arr[i] >= max)
{
temp = arr[i];
arr[i] = max;
max = temp;
}
}
printf("max=%d\n", max);
system("pause");
return 0;
}
4.将三个数按从大到小输出。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,temp;
printf("Please input three numbers:");
scanf("%d,%d,%d", &a, &b, &c);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
if (a < c)
{
temp = a;
a = c;
c = temp;
}
if (b < c)
{
temp = b;
b = c;
c = temp;
}
printf("%d,%d,%d\n", a, b, c);
system("pause");
return 0;
}
5.求两个数的最大公约数。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,temp;
printf("Please input two numbers:");
scanf("%d,%d", &a, &b);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
while(b!=0)
{
c = a%b;
a = b;
b = c;
}
printf("a,b的最大公约数为%d\n", a);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: