Preprocessor Solution

  • + 0 comments

    C++

    **#define INF 1000000

    define minimum(a,b) a=(ausing ternary op/

    define maximum(a,b) a=(a>b)?a:b

    define foreach(v,i) for(int i=0;i

    define io(v,i) cin>>v[i] /defining input/

    define toStr(res) #res /*stringify operator-> # */

    define FUNCTION(min_max, signal) void calc_##min_max(int& a, int b) {a = a signal b ? a : b;}

    /*a is passed by ref while b is passed by val || calc_##min_max , concatenation operator in preprocessor->> '##' -> if signal is < then it becomes calc_minimum /*

    include

    include

    using namespace std;

    if !defined toStr || !defined io || !defined FUNCTION || !defined INF

    error Missing preprocessor definitions

    endif

    FUNCTION(minimum, <) FUNCTION(maximum, >)

    int main(){ int n; cin >> n; vector v(n); **foreach(v, i) { io(v,i); ** /slightly modified as the prev given format was having few problem/ } int mn = INF; int mx = -INF; foreach(v, i) { minimum(mn, v[i]); maximum(mx, v[i]); } int ans = mx - mn; cout << toStr(Result =) <<' '<< ans; return 0;

    }