1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include "preset.h"
#define Max 0x7fffffff
int cul(int a, int b, int c) { return abs(a - b) + abs(b - c) + abs(c - a); }
bool isMin(int a, int b, int c) { if (min(a, min(b, c)) == a) { return true; } return false; }
int minTri(SqList a, SqList b, SqList c) { int i = 0, j = 0, k = 0; int ans = Max; while (i < a.length && j < b.length && k < c.length && ans >= 0) { int temp = cul(a.elem[i], b.elem[j], c.elem[k]); if (temp < ans) ans = temp; if (isMin(a.elem[i], b.elem[j], c.elem[k])) i++; else if (isMin(b.elem[i], a.elem[j], c.elem[k])) j++; else k++; } cout << "min_i = " << i << " min_j = " << j << " min_k = " << k << endl; return ans; }
int main() { SqList a = {{-1, 0, 9}, 3}; SqList b = {{-25, -10, 10, 11}, 4}; SqList c = {{2, 9, 17, 30, 41}, 5}; cout << minTri(a, b, c) << endl; return 0; }
|