2017.12.27 algorithm Analysis greedy algorithm to remove numbers to find the least value problem

Source: Internet
Author: User

1 n-bit positive integer A, delete the K-bit in it, get a new positive integer b, design a greedy algorithm, and get the smallest b for a given and K;

I think: First look at the example: a=5476579228; remove 4 bits, then the number of digits n=10,k=4, the minimum required number B is n-k=6 bit;

1, first find the highest number, because it is 6 digits, so the top bit can not be taken on the last 5 bit (because the relative order of the number can not be changed, assuming that if the last five digits of the 5th digit of 7, then the B can not be 6, the most 4-bit 79228) It is important to understand this! So the question becomes from 1th to K+1 (n (n-k-1)) Take the minimum value, why is k+1, can think for yourself. In this case, it becomes

/54765/79228 Select the smallest number in the middle of the slash!

2, so according to the sequence number 1, to obtain the minimum 4, then the highest level has been determined to be 4; then the 6-bit number becomes 5 bit to determine, the same upper reasoning process, the second high cannot take any number in the 4 bit, because the first bit determines the second position on the 4, So the number before 4 cannot be taken (because the relative order of the numbers cannot be changed), so the minimum value of the number within the slash is changed to 54/7657/9228, which gets 5. 3, then take the third digit 54765/79/228, the third place takes 7;547657/92/28 fourth place takes 2;54765792/2/8, fifth place can only be 2, sixth digit is 8; the resulting figure is 457228;

4, continue to think of a problem if the input integer A is 3346579228, the same n=10,k=4; what kind of problems do you encounter? The same upper process first step:/33465/79228, this time the interval has two the same minimum value 3, which value should be used? Is it obvious that the first 3,why should be selected? Because imagine if you take the second, the second time you can only select the minimum value in 4657, and take the first 3, you can get 3 in 34657.

5, this algorithm idea is probably this, the algorithm concrete how to realize it? First of all we need to know the program body to Loop n-k times, because only then we can cycle out the smallest number, followed by how to take the minimum value within the interval. I use the loop through the entire interval to achieve the minimum value, the most critical is to determine the starting position of the interval, the position of the first loop is best determined to be 1, the end position is k+1, the start of the second cycle is the first time to take the minimum value of the coordinate value plus 1, the end position is K + 2; then continue to record the minimum value of the coordinate value to calculate the next start position.

6. This is my code:

#include<iostream>using namespace std;int main(){    int num,k,n=0,a[100],x; cin>>num>>k; x=num;//计算length(a); while(x>0){  x=x/10;  n++; } a[0]=0;//将输入的整形数字,存入定义的数组中; for(int i=n;i>0;i--) {  int s=num%10;  a[i]=s;  num=num/10; } int j,p=0,minn[n-k+1],min,q; minn[0]=0; for(int i=1;i<=n-k;i++)//n-k次循环;  {  min=a[p+1];//定义q记录坐标;min[]记录每次所取的最小值  q=p+1;     for(j=p+1;j<=k+i;j++){         if(a[j]<min)      {          min=a[j];          q=j;         }     }     p=q;     minn[i]=min;    }    for(int i=1;i<=n-k;i++){     cout<<minn[i]; }    return 0;}

2017.12.27 algorithm Analysis greedy algorithm to remove numbers to find the least value problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.