初学数据结构二

 1 Status DeleteK(SqList&a, int i, int k)
 2 {
 3     if (i < 1 || k<0 || i + k>a.length)
 4         return INFEASIBLE;
 5     else
 6         for (count = 1; count < k; count++)
 7         {
 8             for (j = a.length; j >= i + 1; j--)
 9                 a.elem[j - i] = a.elem[j];
10             a.length--;
11         }
12         return OK;
13 }
14 
15 Status DeleteK(SqList&a, int i, int k)
16 {
17     int j;
18     if (i<0 || i>a.length - 1 || k<0 || k>a.length - i)
19         return INFEASIBLE;
20     for (j = 0; j <= k; j++)
21         a.elem[j + i] = a.elem[j + i + k];
22     a.length = a.length - k;
23     return OK;
24 }

 

 1 int Time(int n)
 2 {
 3     count = 0;
 4     x = 2;
 5     while (x < n / 2)
 6     {
 7         x *= 2;
 8         count++;
 9     }
10     return count;
11 }
12 
13 int Fibonacci(int k, int n)
14 {
15     if (k < 1)
16         exit(OVERFLOW);
17     int *p, x;
18     p = new int[k + 1];
19     if (!p)
20         exit(OVERFLOW);
21     int i, j;
22     for (i = 0; i < k + 1; i++)
23     {
24         if (i < k - 1)
25             p[i] = 0;
26         else
27             p[i] = 1;
28     }
29 for (i = k + 1; i < n + 1; i++)
30 {
31     x = p[0];
32     for (j = 0; j < k; j++)
33         p[j] = p[j + 1];//数组向前移动
34     p[k] = 2 * p[k - 1] - x;//对数据最后一个元素赋值,否则为空,必须销毁
35 }
36 }

 

 1 #include<iostream>
 2 #include<cstdlib>
 3 #define N 10
 4 using namespace std;
 5 double polynomail(int a [], int i, double x, int n);
 6 int main()
 7 {
 8     double x;
 9     int n, i;
10     int a[N];
11     cout << "input x:";
12     cin >> x;
13     cout << "input n:";
14     cin >> n;
15     if (n > N - 1)
16         exit(0);
17     cout << "a[0]-a[n]:";
18     for (i = 0; i <= n; i++)
19         cin >> a[i];
20     cout << "The polynomail value is:" << polynomail(a, n, x, n) << endl;
21     system("pause");
22     return 0;
23 }
24 double polynomail(int a [], int i, double x, int n)
25 {
26     if (i > 0)
27         return a[n - i] + polynomail(a, i - 1, x, n)*x;
28     else
29         return a[n];
30 }

 

posted @ 2013-09-30 00:05  herizai007  阅读(205)  评论(0编辑  收藏  举报