[关闭]
@rg070836rg 2015-11-01T22:26:49.000000Z 字数 2790 阅读 1409

算法概论作业2.4

算法概论作业


6.1

  1. 1. 定义一维数组a[n]
  2. 2. dist[i]来存放到a[i]结尾的子序列的最大和
  3. 3. 求出dist[]中的最大值,即为最大连续子序列的和
  4. 4. 每次dist[i-1] + array[i] <= array[i]时,将begin = i
  5. 5. 每次sum<dist[i]时,end = i

代码如下:

  1. int main()
  2. {
  3. int array[n];
  4. srand((unsigned)time(NULL));
  5. for (int i = 0; i<n; i++) //生成串
  6. {
  7. int temp = rand()%50-20;
  8. array[i] = temp;
  9. cout<<array[i]<<" ";
  10. }
  11. int dist[n] = {0};
  12. int sum = 0;
  13. int end;
  14. int begin;
  15. for (i = 1; i<n; i++)
  16. {
  17. if (dist[i-1] + array[i] > array[i])
  18. {
  19. dist[i] = dist[i-1]+array[i];
  20. }
  21. else
  22. {
  23. dist[i] = array[i];
  24. begin = i;
  25. }
  26. if (sum<dist[i])
  27. {
  28. sum = dist[i];
  29. end = i;
  30. }
  31. }
  32. cout<<endl;
  33. cout<<”最大相连子序列:”;
  34. for(i = begin; i<=end ; i++)
  35. {
  36. cout<<array[i]<<” ”;
  37. }
  38. cout<<endl;
  39. cout<<"最大和为:"<<sum<<endl;
  40. return 0;
  41. }

6.2

思想:

  1. 1. p[i]为停留在第i个旅店的总最小惩罚
  2. 2. 枚举上一个落脚点位置j,则有p[i] = min(p[j] + (200-(a[i] a[j]))2),并将min存放入容器route中,用于存放路径。
  3. 3. p[n]即为总最小惩罚。
  1. int main()
  2. {
  3. int array[n];
  4. vector <int> route;
  5. srand((unsigned)time(NULL));
  6. array[0] = 0;
  7. for (int i = 1; i<n; i++) //随机产生英里数
  8. {
  9. int temp = rand()%100+10;
  10. array[i] = temp;
  11. cout<<array[i]<<" ";
  12. }
  13. int dist[n] = {0};
  14. int min ;
  15. for (i = 1; i<=n; i++)
  16. {
  17. for(int j = 1 ; j<=i ;j++) //枚举上一个落脚点位置j
  18. {
  19. if (array[i]-array[j]<=200) //行走距离不得超过200
  20. {
  21. if (dist[j] + (200-(array[i]-array[j])* (array[i]-array[j])) < dist[j-1] + (200-(array[i]-array[j-1])* (array[i]-array[j-1])))
  22. {
  23. dist[i] = dist[j] + (200-(array[i]-array[j])* (array[i]-array[j]));
  24. min = j;
  25. }
  26. }
  27. }
  28. if (min>0 && min<n ) //保存路径
  29. {
  30. route.push_back(min);
  31. }
  32. }
  33. cout<<endl;
  34. cout<<"总最小惩罚为:"<<dist[n]<<endl;
  35. for (i = 0; i< route.size()-1; i++)
  36. {
  37. cout<<route[i]<<"->";
  38. }
  39. cout<<route[route.size()-1];
  40. return 0;
  41. }

6.3

  1. 1. r[i]表示前i个位置开分店的最大总利润,且r0 = 0;
  2. 2. j为上一个满足mi-mjk的位置;
  3. 3. ri = max{ri-1,rj+pi};
  4. 若取rj+pi,说明要在i点建酒店,则将i放入容器route
  5. 4. 最大总利润即为rn

代码如下:

  1. int main()
  2. {
  3. int m[n];//距离高速公路起点的距离
  4. int p[n];//可带来的利润
  5. int k = 50;
  6. vector <int> route;
  7. srand((unsigned)time(NULL));
  8. m[0] = 0;
  9. p[0] = 0;
  10. int i,j = 0;
  11. for (i = 1; i<n; i++)
  12. {
  13. int temp = rand()%100+20;
  14. int temp2 = rand()%1000+100;
  15. m[i] = m[i-1]+ temp;
  16. p[i] = temp2;
  17. cout<<i<<" 距离: "<<m[i]<<" 利润:"<<p[i]<<endl;
  18. }
  19. int r[n] = {0};
  20. int max ;
  21. for (i = 1; i<=n; i++)
  22. {
  23. for(j = 0; j <= i; j++)//遍历之前的点
  24. {
  25. if (m[i]-m[j]>=k)
  26. {
  27. if(r[i-1]>r[j]+p[i])
  28. {
  29. r[i] = r[i-1];
  30. }
  31. else
  32. {
  33. r[i] = r[j]+p[i];
  34. route.push_back(i);//将要建造的点入队
  35. }
  36. }
  37. }
  38. }
  39. cout<<endl;
  40. cout<<r[n]<<endl;
  41. for (i = 1; i<route.size()-1;i++)//输出建造点
  42. {
  43. if (route[i]!=route[i-1])
  44. {
  45. cout<<route[i]<<"->";
  46. }
  47. }
  48. cout<<route[route.size()-1]<<endl;
  49. return 0;
  50. }

6.4

  1. 1. vi表示前i个字符组成的子串能否被有效分割;
  2. 2. dict[i][j] ==1 为从第i个元素到第j个元素课组成合法字符串;
  3. 3. dict[j][i]==1 && v[j] == 1,则v[i] = 1,即前i个字符可被有效分割,并将j入队;
  1. vector <int> route;
  2. string s = "itwasthebest";
  3. bool dict[n][n] =
  4. {{1,1,0,0,0,0,0,0,0,0,0,0},/*i*/
  5. {0,0,0,0,0,0,0,0,0,0,0,0},/*t*/
  6. {0,0,0,0,1,1,0,0,0,0,0,0},/*w*/
  7. {0,0,0,1,1,0,0,0,0,0,0,0},/*a*/
  8. {0,0,0,0,0,0,0,0,0,0,0,0},/*s*/
  9. {0,0,0,0,0,0,0,1,0,0,0,0},/*t*/
  10. {0,0,0,0,0,0,0,1,0,0,0,0},/*h*/
  11. {0,0,0,0,0,0,0,0,0,0,0,0},/*e*/
  12. {0,0,0,0,0,0,0,0,0,1,0,1},/*b*/
  13. {0,0,0,0,0,0,0,0,0,0,0,0},/*e*/
  14. {0,0,0,0,0,0,0,0,0,0,0,0},/*s*/
  15. {0,0,0,0,0,0,0,0,0,0,0,0}};/*t*/
  16. int v[n] = {1};
  17. for (int i=0; i<n;i++)
  18. {
  19. for (int j=0; j<i; j++)
  20. {
  21. if (dict[j][i]==1 && v[j] == 1)
  22. {
  23. v[i] = 1;
  24. route.push_back(j);
  25. }
  26. }
  27. }
  28. cout<<v[n-1]<<endl;
  29. for (i = 0; i<route.size();i++)
  30. {
  31. cout<<route[i]<<"->";
  32. }
  33. cout<<endl;
  34. return 0;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注