Write a program to find the number of K in the array and the position of the output number. For example, in {2, 4, 3, 7}, the first number is 7 and the position is 4. The numbers of the second and third largest are 4, which can be output at any position of 1 and 3. Function interface: int find_orderk (const int * narry, const int N, const int K)
The algorithm complexity cannot be O (N ^ 2)
Reference answer:
1
# Include <stdio. h>
# Include <stdlib. h>
# Include <time. h>
/***************************************
Input: N: Number of array elements K: Number of the largest number
A: array elements to be searched
****************************************/
# Define n 100
Void rand_select (int *, Int, INT );
Int partition (int *, Int, INT );
Int swap (Int &, Int &);
Int K, ans;
Int main ()
{
Int n, a [n], I;
While (scanf ("% d", & N, & K )! = EOF)
{
Srand (Time (null ));
K --;
For (I = 0; I <n; I ++)
Scanf ("% d", A + I );
Rand_select (A, 0, n-1 );
Printf ("% d/N", ANS );
}
Return 0;
}
Void rand_select (int A [], int P, int q)
{
Int m;
If (P <= q)
{
M = partition (A, p, q );
If (k = m)
{
Ans = A [m];
Return;
}
Else if (k> m)
Rand_select (a, m + 1, q );
Else
Rand_select (A, P, m-1 );
}
}
Int partition (int A [], int P, int q)
{
Int last, I;
If (Q! = P)
Swap (A [rand () % (Q-p) + P], a [p]);
For (I = p + 1, last = P; I <= Q; I ++)
If (A [I]> = A [p])
Swap (A [I], a [++ last]);
Swap (A [last], a [p]);
Return last;
}
Int swap (Int & P, Int & Q)
{
Int temp = P;
P = Q;
Q = temp;
Return 0;
}
2. You can use quick sorting to sort the data. Use another address to search for the Code as follows, and run it in VC ++ 6.0.
// Quick sorting
# Include <iostream>
Using namespace STD;
Int partition (int * l, int low, int high)
{
Int temp = L [low];
Int Pt = L [low];
While (low {
While (low <High & L [High]> = pt)
-- High;
L [low] = L [High];
While (low <High & L [low] <= pt)
++ Low;
L [low] = temp;
}
L [low] = temp;
Return low;
}
Void qsort (int * l, int low, int high)
{
If (low {
Int PL = partition (L, low, high );
Qsort (L, low, pl-1 );
Qsort (L, PL + 1, high );
}
}
Int main ()
{
Int narry [2, 100], ADDR [2, 100];
Int sum = 1, t;
Cout <"input number:" <Endl;
Cin> T;
While (T! =-1)
{
Narry [Sum] = T;
ADDR [Sum-1] = T;
Sum ++;
Cin> T;
}
Sum-= 1;
Qsort (narry, 1, sum );
For (INT I = 1; I <= sum; I ++)
Cout <narry [I] <'/T ';
Cout <Endl;
Int K;
Cout <"Please input place you want:" <Endl;
Cin> K;
Int AA = 1;
Int KK = 0;
For (;;)
{
If (AA = K)
Break;
If (narry [Kk]! = Narry [Kk + 1])
{
AA + = 1;
Kk ++;
}
}
Cout <"the No." <k <"number is:" <narry [Sum-kk] <Endl;
Cout <"and it's place is :";
For (I = 0; I <sum; I ++)
{
If (ADDR [I] = narry [Sum-kk])
Cout <I <'/T ';
}
Return 0;
}
It's rare to see this question today. I wrote this answer when I tested Baidu last year:
# Include <math. h>
# Include <time. h>
# Include <string>
# Include <iostream>
# Include <vector>
Using namespace STD;
# Define N 10
# Define K 5
Int main (INT argc, char * argv [])
{
Srand (unsigned) Time (null ));
If (k> N)
{
Cout <"error! "<Endl;
Return 1;
}
Vector <int> SRC;
Cout <"Source" <n <"data:" <Endl;
For (INT I = 0; I <n; I ++)
{
SRC. push_back (RAND ());
Cout <SRC [I] <"";
}
Vector <int> maxnum; // The maximum number of K records stored in sequence. The first value is the maximum value, and the last value is the maximum value.
For (I = 0; I <K; I ++)
{
Maxnum. push_back (-999999); // initialize maxnum, K-9999999
}
For (I = 0; I <n; I ++)
{
For (Int J = 0; j <maxnum. Size (); j ++)
{
If (SRC [I]> = maxnum [J]) // if the value is greater than the current value, the value is placed in the current position, and the subsequent digits are backward.
{
For (INT I1 = maxnum. Size ()-1; I1> J; I1 --)
{
Maxnum [I1] = maxnum [i1-1];
}
Maxnum [J] = SRC [I];
Break;
}
}
}
Cout <Endl <"Number" <k <"big number:" <maxnum [k-1] <Endl;
Return 0;
}
Analysis: The algorithm only accesses n numbers once. the time complexity of this part is O (n). However, each access must be compared with K numbers, therefore, the overall time complexity is O (n * K)
Idea: 1. Consider if (k> N) Exit (0)
2. If number N is a big one, use pile/stack sort
3. If number N is a small one, use Quick Sort;
4; find your k Number and print in the screen;
Find_orderk (const int * narry, const int N, const int K)
{
If (N> K) Exit (0 );
Sort (* narry );
For (I = 0; I <n; I ++)
If (I = k) return narry [k];/* The number of the K is similiar to point */
}
========================================================== ================================ Function functions: returns a K-small element (using the fast-forward idea)
Parameter: (t a [] target array | int l left boundary | int r right boundary | minimum int K element)
========================================================== ====================================== Template <class T>
T select (t a [], int L, int R, int K)
{
If (L> = r) return a [l]; // return directly if a parameter error occurs.
Int I = L;
Int J = R + 1;
T token = A [I];
While (true)
{
Do
{
I = I + 1;
} While (A [I]> latency );
Do
{
J = J-1;
} While (A [J] <values );
If (I> = J)
{
Break;
}
Swap (A [I], a [J]);
}
If (J-L + 1 = k) // if the position of the current benchmark is exactly K, the benchmark value is returned if the condition is met.
{
Return response;
}
A [l] = A [J];
A [J] = bytes;
If (J-L + 1 <K)
{
Return select (A, J + 1, R, K-J + L-1); // if the current position of the benchmark is on the left side of K, the right side is sorted quickly.
}
Else
{
Return select (A, L, J-1, k); // if the current position of the baseline is on the right of K, the left is arranged
}
}
Analysis Program:
# Include <stdio. h>
Class
{
Public:
Static int NUMA;
A ()
{
Num ++;
};
~ A ()
{
Num --;
};
Virtual void print (void)
{
Printf ("Class A, Mum: % d/N", num );
}
Void test (void)
{
Printf ("Class A test/N ");
Print ();
}
};
Class B: public
{
Public:
Static int numb;
B ()
{
Num --;
};
~ B ()
{
};
Virtual void print ()
{
Printf ("Class B, num: % d/N", num );
}
Void test (void)
{
Printf ("Class B test/N ");
Print ();
}
};
Class C
{
Public:
Virtual void print ()
{
Printf ("Class B/N ");
}
};
Int A: NUMA = 0;
Int B: numb = 0;
Void main ()
{
B;
B. Print ();
B. Test ();
Printf ("1/N ");
A *;
B * P = new (Class B );
P-> Print ();
P-> test ();
Printf ("1/N ");
A = P;
A-> Print ();
A-> test ();
Delete ();
Printf ("sizeof (c): % d/N", sizeof (c ));
}
# Include <stdio. h>
Class A1
{
Public:
A1 () {dosomething ();}
Virtual void dosomething () {printf ("I Am A/N ");}
Void test () {dosomething ();}
};
Class B1: Public A1
{
Public:
Virtual void dosomething () {printf ("I Am B/N ");}
};
Void main ()
{
B1 * B = new B1;
B-> test ();
}
1. During C ++ development, the class destructor used for base classes are generally virtual functions.
Class clxbase
{
Public:
Clxbase (){};
Virtual ~ Clxbase (){};
Virtual void dosomething () {cout <"do something in class clxbase! "<Endl ;};
};
Class clxderived: Public clxbase
{
Public:
Clxderived (){};
~ Clxderived () {cout <"output from the destructor of class clxderived! "<Endl ;};
Void dosomething () {cout <"do something in class clxderived! "<Endl ;};
};
Void main ()
{
Clxbase * Ptest = new clxderived;
Ptest-> dosomething ();
Delete Ptest;
}
The output result is:
Do something in class clxderived!
Output from the destructor of class clxderived!
This is very easy to understand.
However, if you remove the virtual before the clxbase destructor, the output result is as follows:
Do something in class clxderived!
That is to say, the destructor of the class clxderived is not called at all! Under normal circumstances, all the class destructor release the memory resources, and if the Destructor is not called, memory leakage will occur. I think all c ++ programmers know this danger. Of course, if you do other work in the destructor, all your efforts will be in vain.
Therefore, the answer to the question at the beginning of the article is -- This is to call the destructor of a derived class when a base class pointer is used to delete an object of A derived class.