/* 1.输入a、b、c 3个值,输出其中最大者,要求在子函数里面比较数大 */
#include<stdio.h>
int mycmp(int a,int b,int c)
{
int max = a;
if(max < b) max = b;
if(max < c) max = c;
return max;
}
int main()
{
int x1,x2,x3,max;
printf("Please input x1 x2 and x3:\n");
scanf("%d %d %d",&x1,&x2,&x3);
mycmp(x1,x2,x3);
max = mycmp(x1,x2,x3);
printf("max = %d\n",max);
return 0;
}
/* 2.编程输入三角形的三边长,计算并输出三角形面积和周长(可以只计算周长)。 */
#include<stdio.h>
#include<math.h>
double sum,area; //周长:sum,面积:area
void mycount(double a,double b,double c)
{
double p;
sum = a + b + c;
//面积:海伦公式
p = sum/2;
area = sqrt(p*(p-a)*(p-b)*(p-c));
}
int main()
{
double x1,x2,x3;
printf("Please input x1 x2 and x3:\n");
scanf("%lf %lf %lf",&x1,&x2,&x3);
mycount(x1,x2,x3);
printf("周长 = %f,面积 = %f\n",sum,area);
return 0;
}
/* 3.编写一个函数用冒泡法对10个整数由小到大顺序排列,用数组名作为实参 */
// 楼主注意啦,有些回答不是冒泡排序,而是简单选择排序,如 yumao121
// 当本身是从小到大排序的时候,倒霉熊lujin 解答要遍历n-1趟,其实只要遍历一趟
#include<stdio.h>
#include<math.h>
double sum,area; //周长:sum,面积:area
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void BubbleSort(int A[],int n)
{
int i,j,last; //last:记录最近被修改的记录
i = n - 1;
while( i>0 )
{
last = 0;
for(j = 0; j < i; j++)
{
if(A[j+1] < A[j])
{
swap(&A[j],&A[j+1]);
last = j;
}
}
i = last;
}
}
int main()
{
int i,B[10];
printf("please enter 10 nums:\n");
for(i = 0;i < 10;i++)
{
scanf("%d",&B[i]);
}
BubbleSort(B,10);
printf("sort:\n");
for(i = 0;i < 10;i++)
{
printf("%d ",B[i]);
}
printf("\n");
return 0;
}
温馨提示:答案为网友推荐,仅供参考