删数问题-注意贪心策略-不严格

来源:互联网 发布:上海知恩服饰有限公司 编辑:程序博客网 时间:2024/05/01 06:02

http://blog.csdn.net/cxllyg/article/details/8213312

给定n位整数q,去掉其中任意k<=n个数字后,剩下的数字按原次序排列组成一个新的正整数。对于给定的n位正整数a和正整数k,设计一个算法找出剩下数字组成的新数最小的删数方案。

比如,178543删除四个数字后,最小的新数是13。

贪心策略:最近下降点优先

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <string>  
  4. using namespace std;  
  5.   
  6. void delek(vector<int> &s, int k)  
  7. {  
  8.     int n=s.size();  
  9.     if(k>=n)  
  10.         ;  
  11.     else  
  12.     {  
  13.         while(k>0)  
  14.         {  
  15.             for(vector<int>::iterator it=s.begin(); it<s.end()-1 && *(it)<*(it+1); it++) ;  
  16.             s.erase(it);  
  17.             k--;  
  18.         }  
  19.           
  20.     }  
  21.   
  22. }  
  23.   
  24.   
  25. void main()  
  26. {  
  27.     vector<int> s;  
  28.     s.push_back(1);  
  29.     s.push_back(7);  
  30.     s.push_back(8);  
  31.     s.push_back(5);  
  32.     s.push_back(4);  
  33.     s.push_back(3);  
  34.   
  35.       
  36.     delek(s, 4);  
  37.   
  38.     for(vector<int>::iterator it=s.begin(); it!=s.end(); it++)  
  39.         cout<<*it;  
  40.   
  41.     cout<<endl;  
  42. }  
0 0
原创粉丝点击