Tuesday, 13 September 2016

Hacker Rank Solution Program In C++ For " Preprocessor Solution"

Problem :- Hacker Rank Solution Program In C++ For " Preprocessor Solution"

Preprocessor :-Preprocessor directives are lines included in the code preceded by a hash sign (#). These lines are directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.

Solution :-

#define toStr(x) #x
#define io(v) cin >> v
#define INF 100000000
#define foreach(v, i) for(int i = 0; i < v.size(); i++)
#define FUNCTION(name, op) void name(int& a, int b) { if (b op a) a = b; }

#include <iostream>
#include <vector>
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<int> v(n);
 foreach(v, i) {
  io(v)[i];
 }
 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;
}

Output :-


Hacker Rank Solution Program In C++ For " Preprocessor Solution"

No comments:
Write comments

Recommended Posts × +