面试题:有两个二维数组A[10][10],B[10][10],A中数据位置一一对应B数组所在位置,请写一个函数:任意给出一个数值(数组A中找出最接近值),在A中找到数值所在行列,找到B中对应值

#include 
#define diff(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
int main()
{
    float farr[4][4] = {{0.1, 0.2, 0.3, 0.4}, {0.5, 0.6, 0.7, 0.8}, {0.9, 1.0, 1.1, 1.2}, {1.3, 1.4, 1.5, 1.6}};
    int iarr[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
    float target;
    scanf("%f", &target);
    int x = 0, y = 0;
    float min = diff(farr[0][0], target);
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (diff(farr[i][j], target) < min)
            {
                min = diff(farr[i][j], target);
                x = i;
                y = j;
            }
        }
    }
    printf("%d\n", iarr[x][y]);
}

你可能感兴趣的:(算法)