dev/c/avg_task/avg.c

79 lines
1.2 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
//~ int avg(int a, int b)
//~ {
//~ if (abs(a) < abs(b))
//~ return a + (b - a) / 2;
//~ else
//~ return b + (a - b) / 2;
//~ }
int avg(int a, int b)
{
return abs(a) < abs(b) ? a + (b - a) / 2 : b + (a - b) / 2;
}
//~ int avg(int a, int b)
//~ {
//~ if (sign(a) != sign(b))
//~ return (a + b) / 2;
//~ else
//~ return a + (b - a) / 2;
//~ }
//~ int avg(int a, int b)
//~ {
//~ if (((a > 0) && (b > 0)) || ((a < 0) && (b < 0)))
//~ return a + (b - a) / 2;
//~ else
//~ return (a + b) / 2;
//~ }
int main(int argc, char *argv[])
{
int ab[][2] = {
{3, 3},
{3, 4},
{4, 4},
{4, 5},
{5, 5},
{5, 4},
{4, 3},
{-3, 3},
{-3, 4},
{-4, 4},
{-4, 5},
{-5, 5},
{-5, 4},
{-4, 3},
{3, -3},
{3, -4},
{4, -4},
{4, -5},
{5, -5},
{5, -4},
{4, -3},
{-3, -3},
{-3, -4},
{-4, -4},
{-4, -5},
{-5, -5},
{-5, -4},
{-4, -3}
};
size_t i;
for (i = 0; i < sizeof(ab) / 2 / sizeof(int); i++) {
int iavg = avg(ab[i][0],ab[i][1]);
int davg = ((double)ab[i][0] + (double)ab[i][1]) / 2.0;
if (iavg != davg)
printf("avg(%d, %d) = %d (верное = %d)\n", ab[i][0], ab[i][1], avg(ab[i][0],ab[i][1]), davg);
}
return 0;
}