a********r 发帖数: 218 | 1 For questions 1-4, assume that an array stores integers and has a maximum
size of 100. The array is used to store two distinct data structures, BOTH
a queue and a stack. The answer should be written in C or C++ and should
not use any existing data structure libraries.
1.Write a function that pushes an integer into the stack.
2.Write a function that pops an integer from the stack.
3.Write a function that enqueues an integer into the queue.
4.Write a function that dequeues an integer from the queue.
5.For the given C function, below, please answer the 3 questions below:
int Function()
{
int intArray[N];
int arraySum = 0;
for (int i = 0;i< N;i++)
{
intArray[i] = i;
For(int J = 1;j
{
intArray[i] = J * intArray[
i]
}
}
For(int k = 0;k
{
arraySum += intArray[k];
}
return arraySum;
}
a)Give the O(n) runtime complexity for the function.
b)Give an optimized version (if one exists) for the function.
c) Give the O(n) runtime complexity for the optimized version. | l*********8 发帖数: 4642 | 2 1-4说是要在一个数组上同时实现stack和queue吧?
BOTH
【在 a********r 的大作中提到】 : For questions 1-4, assume that an array stores integers and has a maximum : size of 100. The array is used to store two distinct data structures, BOTH : a queue and a stack. The answer should be written in C or C++ and should : not use any existing data structure libraries. : 1.Write a function that pushes an integer into the stack. : 2.Write a function that pops an integer from the stack. : 3.Write a function that enqueues an integer into the queue. : 4.Write a function that dequeues an integer from the queue. : 5.For the given C function, below, please answer the 3 questions below: : int Function()
| l*********8 发帖数: 4642 | 3 5. 原来程序O(N^2)
改成O(N),
int Function(int N)
{
int Factorial=1;
for (int j=1; j
Factorial *= j;
return N*(N-1)/2 * Factorial ;
}
小心溢出 |
|