3. data comparison size

Source: Internet
Author: User

Here we mainly discuss the implementation of algorithms, which seem simple. In fact, different algorithms have different implementation ideas. Assume that a common example is to input three integers, a, B, and c, in ascending order. Method 1: Comparison between a, B, and c, there are 6 combinations, using if... else if implementation, as follows: # include <stdio. h> int main () {int a, B, c; scanf ("% d", & a, & B, & c ); if (a <= B & B <= c) printf ("% d \ n", a, B, c ); else if (a <= c & c <= B) printf ("% d \ n", a, c, B ); else if (B <= a & a <= c) printf ("% d \ n", B, a, c ); else if (B <= c & c <= a) printf ("% d \ n", B, c, ); else if (c <= a & a <= B) printf ("% d \ n", c, a, B); else if (c <= B & B <= a) printf ("% d \ n", c, B, a); return 0 ;}method 2: I think there are too many branches above. a and B are compared first. If a> B, they are exchanged. Then a and c are compared. If a> c, they are exchanged. Finally, B and c are compared, if B> c, switch. # Include <stdio. h> int main () {int a, B, c, t; scanf ("% d", & a, & B, & c ); if (a> B) {t = a; a = B; B = t;} if (a> c) {t = a; a = c; c = t ;} if (B> c) {t = B; B = c; c = t;} printf ("% d \ n", a, B, c ); return 0;} method 3: set the current minimum value x and the maximum value z. The initial value is a. With the comparison, the minimum value and the maximum value are updated slowly. To the final maximum and minimum values. If you know the minimum and maximum values, you can calculate the Middle Value y. # Include <stdio. h> int main () {int a, B, c, x, y, z; scanf ("% d", & a, & B, & c); // min x; max zx = a; if (B <x) x = B; if (c <x) x = c; z =; if (B> z) z = B; if (c> z) z = c; y = a + B + c-x-z; printf ("% d \ n", x, y, z); return 0;} additional question: enter a positive integer n <= 30, output an inverted triangle of n layers. For example, when n = 5, the output is as follows: *************************. My implementation is as follows: # include <stdio. h> int main () {int I, t, j, k; scanf ("% d", & I); for (t = I; t> = 1; t --) {// output two space characters first for (k = I-t; k> 0; k --) {printf ("");} // output the "*" character for (j = 2 * t-1; j> = 1; j --) {printf ("*");} // line feed printf ("\ n");} return 0 ;}

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.