The number of deletions of greedy Algorithms

Source: Internet
Author: User

Given N-digit positive integer a, remove any K ≤ n digits, and the remaining digits are arranged in the original order to form a new positive integer.
For the given n-bit positive integers A and K, an algorithm is designed to find the smallest number of the remaining digits.Delete countSolution.

 

The principle of the algorithm should be that, starting from the highest bit, one-to-low-level search, once the number of the previous (high) Digit exceeds the current BIT, delete the previous digit until the number of K is deleted, if K entries are not deleted at the end, it indicates that the number is sorted from small to large, and the number of required digits is deleted from the maximum bit. My algorithms are as follows:

# Include <stdio. h>

 

Void main (){

Int I, J;

Int prefix;

Long N [18] = {1456385 ,}; //

Int Lenn = 18;

Int S = 12;

Int K = 0;

Prefix = N [0];

For (I = 0; I <s; I ++ ){

For (j = 1; j <Lenn-I; j ++ ){

If (N [J-1]> N [J]) {

K = J-1;

While (k <lenN-i-1 ){

N [k] = N [k + 1];

K ++;

}

N [lenN-i-1] = 0;

Break;

}

}

If (j = Lenn-I ){

K = I;

While (k <s ){

N [J-1] = 0;

J --;

K ++;

}

Break;

}

}

For (I = 0; I <Lenn-S; I ++)

Printf ("% d", N [I]);

Getchar ();

}

The running result is 333348.

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.