有几道简单的C语言的题,请大家帮帮忙帮我做一下,谢谢。

2024-12-04 06:38:48
推荐回答(3个)
回答1:

2.
用double 就行了
3. double sum(double a,double b)
{
return a+b;
}
double lose(double a,doulbe b)
{
if(a>b) return a-b;
else return b-a;
}
4.
int cmpbig(int a,int b,int c)
{
if(a>b && a>c) return a;
else if(b>a && b>c) return b;
else if(c>a && c>b) return c;
}
int cmpsmall(int a,int b,int c)
{
if(a else if(b else if(c}

回答2:

1,
#include

float foo(int n)
{
int i;
float f = -1, sum = 0;
for (i = 1; i <= n; i++) {
sum += f / i;
f = -f;
}
return sum;
}
int main(void)
{
int n;
printf("Input n:");
scanf("%d", &n);
printf("%.2f\n", foo(n));
return 0;
}

2,
#include

void foo(float a, float b, float *sum, float *sub)
{
*sum = a + b;
*sub = a - b;
}
int main(void)
{
float a, b, c, d;
printf("Input 2 float:");
scanf("%f%f", &a, &b);
foo(a, b, &c, &d);
printf("sum=%.2f, sub=%.2f\n", c, d);
return 0;
}

3,
#include

void foo(int a, int b, int c, int *max, int *min)
{
if (a > b) {
*max = a > c ? a : c;
*min = b < c ? b : c;
} else {
*max = b > c ? b : c;
*min = a < c ? a : c;
}
}

int main(void)
{
int a, b, c, max, min;
printf("Input 3 int:");
scanf("%d%d%d", &a, &b, &c);
foo(a, b, c, &max, &min);
printf("max=%d, min=%d\n", max, min);
return 0;
}

回答3:

楼上一定没读过书,题目意思都不看清楚。
2的最后表达式和前面序列不一样。我是按前面表达式写的。
ide:codeblock

#include

using namespace std;

double returnNValue(int n)
{
double num = 0;
for(int i = 1; i <= n; i++)
{
double t = (double)i;
if(n%2)
num += 1/t;
else
num += -1*(1/t);
}
return num;
}

void cal(double a,double b,double &sum,double &dif)
{
sum = a + b;
dif = a - b;
}

void getMaxAndMin(double a,double b,double c,double &max,double &min)
{
if(a >= b)
{
if(a >= c)
max = a;
else
max = c;
if(b <= c)
min = b;
else
min = c;
}
else
{
if(b >= c)
max = b;
else
max = c;
if(a <= c)
min = a;
else
min = c;
}
}

int main()
{
cout<
double a = 10;
double b = 50;
double sum;
double dif;
cal(a,b,sum,dif);
cout<<"sum:"<
double c = 30;
double max;
double min;
getMaxAndMin(a,b,c,max,min);
cout<<"max:"< return 0;
}