// judge a point in which side of one line consisted with tow line in plane // @author ligurecn // xuzhou, 2019.03.19 #include<iostream> #include<ctime> #include<cstdlib> #include<time.h>
usingnamespace std;
doublerandom(double start, double end){ return start+(end-start)*rand()/(RAND_MAX + 1.0); } boolgetJudgement(double point[], double line[2][2]){ //reconstruct vertors like a, b // then use cross product method to judge // if result is positive, point in left, otherwise negetive, point in right bool isLeft=false; double a[2], b[2]; for(int i=0; i<2; i++) a[i] = line[1][i]-line[0][i], b[i]=point[i]-line[0][i];
srand(int(time(0)));//seed from time for (int i = 0; i < 2; i++) { for(int j=0; j<2; j++)line[i][j] = random(-10, 10); point[i] = random(-10, 10); }
// judge point position in which side of line // if in left side, return true // in right side, return false bool isLeft = getJudgement(point, line); cout << "- point(" << point[0] << ", " << point[1]<< ")\n"; if(isLeft) cout << "-> in left side of line: "; else cout << "-> right side of line: ";
//编译 $ g++ lrj.cpp -o pp ///////////////////////////Test 1 $ ./pp - point(-6.2113, 8.92874) -> in left side of line: (0.0824081,9.7702)<->(-5.83127,9.57272) ///////////////////////////Test 2 $ ./pp - point(8.20822, 8.60886) -> right side of line: (4.5333,-5.9355)<->(-8.27962,-2.37346) ///////////////////////////Test 3 $ ./pp - point(-1.58741, 0.621722) -> right side of line: (0.306838,-4.67183)<->(-6.34671,0.975676)
// sum test // @author ligurecn // xuzhou, 2019-03-18 #include<iostream> #include<ctime> #include<cstdlib> #include<time.h>
usingnamespace std;
doublerandom(double start, double end){ return start+(end-start)*rand()/(RAND_MAX + 1.0); } intminv(int v[], int len){ int mn = v[0]; for(int i=0; i<len; i++){ if (v[i]<mn) mn = v[i]; } return mn; } intmaxv(int v[], int len){ int mx= v[0]; for(int i=0; i<len; i++){ if(v[i]>mx) mx = v[i]; } return mx; }
intmain(int argc, char** argv){ // tips of using. if(argc < 2){ cout << "- Usage: " << argv[0] << " Num" << endl; } // get the num para. int num = atoi(argv[1]); // initial array: V. int V[num]; // seed random number via unix time var. srand((int)time(0)); // generate the array via random function. for (int i=0; i< num; i++) V[i] = int(random(-50, 50)); // then display them. cout << "- Original array:\n"; for (int i=0; i< num; i++) cout << V[i] << " "; cout << endl; // get sum of first N int var in V. int S[num]; for (int i=0; i< num; i++) { if(i==0) S[i]=V[i]; else S[i]=S[i-1]+V[i]; } // see the array of sum. cout << "Sum array:\n"; for (int i=0; i< num; i++) cout << S[i] << " "; cout << endl; // pick out the min & max value of sum array. int mins = minv(S, num); int maxs = maxv(S, num); int mins_idx, maxs_idx; // fetch min & max index via their value. // perhaps, there are more than one of them. for (int i=0; i< num; i++){ if(S[i]==mins) mins_idx = i; if(S[i]==maxs) maxs_idx = i; } // show the final answer. cout << "-> min sum value is: " << mins << ", in position: " << mins_idx+1 << endl; cout << "-> max sum value is: " << maxs << ", in position: " << maxs_idx+1 << endl; return0; }
// 编译 $g++ test.cpp -o test /////////////////////////////1 $./test 10 - Original array: 20 -11 47 -36 24 45 6 46 -44 -5 Sum array: 20 9 56 20 44 89 95 141 97 92 -> min sum value is: 9, in position: 2 -> max sum value is: 141, in position: 8 /////////////////////////////2 $ ./test 15 - Original array: 25 -15 6 -16 -6 -31 -29 -21 24 -9 28 14 -3 -46 35 Sum array: 25 10 16 0 -6 -37 -66 -87 -63 -72 -44 -30 -33 -79 -44 -> min sum value is: -87, in position: 8 -> max sum value is: 25, in position: 1 /////////////////////////////3 $ ./test 20 - Original array: 13 -6 -27 -12 48 -45 -35 28 -3 26 49 20 29 17 -32 -31 36 28 1 31 Sum array: 13 7 -20 -32 16 -29 -64 -36 -39 -13 36 56 85 102 70 39 75 103 104 135 -> min sum value is: -64, in position: 7 -> max sum value is: 135, in position: 20