In: Computer Science
PLease using C#
-14 ~ -7
-2 ~ 5
33 ~ 44
Numbers generated must also randomly distributed across the three ranges.
Exception handling object o2 = null; try { int i2 = (int)o2; // Error }
catch (InvalidCastException e) { }
catch (ArgumentException e) when (e.ParamName == "…") { }
catch (FileNotFoundException e) { // FileNotFoundExceptions are handled here. } catch (IOException e) { // Extract some information from this exception, and then // throw it to the parent method. if (e.Source != null) Console.WriteLine("IOException source: {0}", e.Source); throw; }
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector <int> area; vector < vector <int> > rect; int sum; Solution(vector<vector<int> >& rects) { rect = rects; sum = 0; for(int i =0 ; i < rects.size(); i++){ int x1 = rects[i][0]; int y1 = rects[i][1]; int x2 = rects[i][2]; int y2 = rects[i][3]; int temp = (abs(x2 - x1) + 1) * (abs(y2 - y1) + 1); sum += temp; area.push_back(sum); } } vector<int> pick() { int randArea = rand() % sum + 1; int i; for(i = 0; i < area.size(); i++){ if(randArea <= area[i]) break; } int dist_x = rand() % (abs(rect[i][0] - rect[i][2] ) + 1); int dist_y = rand() % (abs(rect[i][1] - rect[i][3] ) + 1); return {dist_x + rect[i][0], dist_y + rect[i][1]}; } }; main(){ vector<vector<int> > v = {{1, 1, 5, 5}}; Solution ob(v); print_vector(ob.pick()); print_vector(ob.pick()); print_vector(ob.pick()); }
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector <int> area; vector < vector <int> > rect; int sum; Solution(vector<vector<int> >& rects) { rect = rects; sum = 0; for(int i =0 ; i < rects.size(); i++){ int x1 = rects[i][0]; int y1 = rects[i][1]; int x2 = rects[i][2]; int y2 = rects[i][3]; int temp = (abs(x2 - x1) + 1) * (abs(y2 - y1) + 1); sum += temp; area.push_back(sum); } } vector<int> pick() { int randArea = rand() % sum + 1; int i; for(i = 0; i < area.size(); i++){ if(randArea <= area[i]) break; } int dist_x = rand() % (abs(rect[i][0] - rect[i][2] ) + 1); int dist_y = rand() % (abs(rect[i][1] - rect[i][3] ) + 1); return {dist_x + rect[i][0], dist_y + rect[i][1]}; } }; main(){ vector<vector<int> > v = {{1, 1, 5, 5}}; Solution ob(v); print_vector(ob.pick()); print_vector(ob.pick()); print_vector(ob.pick()); }
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector <int> area; vector < vector <int> > rect; int sum; Solution(vector<vector<int> >& rects) { rect = rects; sum = 0; for(int i =0 ; i < rects.size(); i++){ int x1 = rects[i][0]; int y1 = rects[i][1]; int x2 = rects[i][2]; int y2 = rects[i][3]; int temp = (abs(x2 - x1) + 1) * (abs(y2 - y1) + 1); sum += temp; area.push_back(sum); } } vector<int> pick() { int randArea = rand() % sum + 1; int i; for(i = 0; i < area.size(); i++){ if(randArea <= area[i]) break; } int dist_x = rand() % (abs(rect[i][0] - rect[i][2] ) + 1); int dist_y = rand() % (abs(rect[i][1] - rect[i][3] ) + 1); return {dist_x + rect[i][0], dist_y + rect[i][1]}; } }; main(){ vector<vector<int> > v = {{1, 1, 5, 5}}; Solution ob(v); print_vector(ob.pick()); print_vector(ob.pick()); print_vector(ob.pick()); }
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector <int> area; vector < vector <int> > rect; int sum; Solution(vector<vector<int> >& rects) { rect = rects; sum = 0; for(int i =0 ; i < rects.size(); i++){ int x1 = rects[i][0]; int y1 = rects[i][1]; int x2 = rects[i][2]; int y2 = rects[i][3]; int temp = (abs(x2 - x1) + 1) * (abs(y2 - y1) + 1); sum += temp; area.push_back(sum); } } vector<int> pick() { int randArea = rand() % sum + 1; int i; for(i = 0; i < area.size(); i++){ if(randArea <= area[i]) break; } int dist_x = rand() % (abs(rect[i][0] - rect[i][2] ) + 1); int dist_y = rand() % (abs(rect[i][1] - rect[i][3] ) + 1); return {dist_x + rect[i][0], dist_y + rect[i][1]}; } }; main(){ vector<vector<int> > v = {{1, 1, 5, 5}}; Solution ob(v); print_vector(ob.pick()); print_vector(ob.pick()); print_vector(ob.pick()); }