C language, how to calculate the maximum value and secondary limit after removing repeated elements in the array

Source: Internet
Author: User

// Original] C language, how does an array calculate the maximum value and secondary limit after removing repeated elements?



# Include <stdio. h>

Int main ()

{

Int a [7];

Printf ("input seven numbers :");

For (int I = 0; I <7; I ++)

{

Scanf ("% d", & a [I]);

}

Int len = 7; // 5 5 5 2 5

// Two for loops are used to traverse

For (int j = 0; j <len; ++ j)

{

For (int m = j + 1; m <len; ++ m)

{

If (a [j] = a [m])

{

// Use the overwrite method to delete the same element as the first for statement.

For (int k = m + 1; k <len; ++ k) // there will be differences in writing-According to k = m + 1, may be a len-1 or other...

A [k-1] = a [k];

Len --; // keep up with the length

-- M; // because an element is deleted, the subscript of the array element to be compared with a [j] does not change but the value has changed)

}

}

}

For (I = 0; I <len; I ++)

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

Printf ("\ n ");

Int temp;

For (int n = 0; n <len; n ++)

For (int p = n + 1; p <len; p ++)

If (a [n] <= a [p])

{

Temp = a [n];

A [n] = a [p];

A [p] = temp;

}

For (I = 0; I <len; I ++)

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

Printf ("MAX = % d, S_MAX = % d \ n", a [0], a [1]);

Return 0;

}


// Running result

// Input seven numbers: 7 5 7 5 8 6 8

// 7 5 8 6

// 8 7 6 5 MAX = 8, S_MAX = 7

// Press any key to continue


This article is from the "Jean_Z learning blog" blog, please be sure to keep this source http://7905490.blog.51cto.com/7895490/1299815

Related Article

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.