"Learn the path of iOS: C" array. Loops. Sum of statements use test questions practice

Source: Internet
Author: User

1). Find the second largest value in an array. The range of values in the array is 10-30, and the storage space size is 10.

The code is as follows:

    int a[10] = {0};    int max = 0, Secmax = 0; Defines the first large value, the second largest value.        for (int i = 0; i<; i++) {        A[i] = arc4random ()% (30-10 + 1) + 10;//random value 10-30        printf ("a[%d] =%d\n", I, a[i ]);    }    for (int i = 0; i < i++) {        if (Max < a[i]) {            Secmax = max;  Give Secmax the second largest value in your hand. Then save the current maximum value.            max = A[i];        } else if (A[i] > Secmax && max! = A[i]) {//In order not to appear max=, Sexmax =30 phenomenon. That is  A[i] < Secmax < Max            Secmax = A[i];  If the current value is larger than Sexmax and smaller than Max, then the second largest value.        }    }    printf ("max =%d,sexmax =%d \ n", Max, Secmax);

2.) randomly generates a positive integer of 20 10~50 into the array, and computes the maximum, minimum, average, and sum of the elements in the array.

The code is as follows:

    int m[20] = {0};    for (int i = 0; i <; i++) {        M[i] = arc4random ()% (50-10 + 1) +;        printf ("%d\n", M[i]);    }    int max = 0, min = 0, sum = 0; float avg = 0;        Maximum for    (int i = 0; i <; i++) {        if (Max < m[i]) {            max = m[i];}    }    printf ("max =%d\n", max);        Minimum for    (int i = 0; i < i++) {        if (i = = 0) {           min = m[i];        } else {            if (min > M[i]) { C19/>min = M[i];    }} printf ("min =%d\n", min);        and for    (int i = 0; i <; i++) {        sum + = M[i];    }    printf ("sum =%d\n", sum);        Average    avg = sum/20;    printf ("avg =%.2f\n", avg);

3). Merges two well-ordered arrays into another array, and the merged array is also ordered (merge sort).

    Defines two arrays of m,n int m[10] = {0};        int n[10] = {0};         Assigns a value to the array, the range of random values [20,40] between for (int i = 0; i <; i++) {M[i] = Arc4random ()% (40-20 + 1) + 20;    N[i] = arc4random ()% (40-20 + 1) + 20; }//array m in ascending order (int i = 0; i < 10-1; i++) {for (int j = 0; J < 10-i-1, j + +) {if (M                [j] > M[j+1]) {int temp = m[j];                M[J] = m[j+1];            M[J+1] = temp;            }}}//array n ascending order (int i = 0; i < 10-1; i++) {for (int j = 0; J < 10-i-1, j + +) {                if (N[j] > n[j+1]) {int temp = n[j];                N[J] = n[j+1];            N[J+1] = temp;  }}}//After sorting the results for (int i = 0; i < i++) {printf ("m[%d] =%d,n[%d] =%d \ n", I, m[i], I,    N[i]);  }  int c[10] ={0};//defines a third array. Two number combinations and. Merge sort     int k = 0, i = 0, j = 0;    while (i < Ten &&J <) {        if (A[i] < b[j]) {             c[k++] = a[i++];       } else {             c[k++] = b[j++];       }    }    while (I < ten) {        c[k++] = a[i++];    }    while (J < Ten) {         c[k++] = b[j++];& nbsp;  }    for (int i = 0; i<; i++) {    printf ("%d\n", C[i]);//Output result}

4). In a known string, look for the longest word and output the longest word.

    Char str[] =  "Name is a Mk FUCKD is Fucshish";    int len = 0;//stores the length of the current word    int max = 0;//stores the length of the longest word    int maxindex = 0;//stores the position of the longest word        int i = 0;//loop variable initial value while    (St R[i] = ' + ') {if        (str[i]! = ') {              //If the character that goes to is not a space            len++;        } else {             //Gets the character is a space.            if (Len > Max) {                max = len;                Maxindex = I-len;            }            Len = 0;//The word length to zero and calculates the new word.        }        i++;    }    If the last word is the longest word and no spaces are encountered, it will not be compared with maxlength. So we just need to compare    the loop outside. if (Len > Max) {        max = len;        Maxindex = I-len;    }        for (int i = Maxindex; i < Maxindex + max; i++) {        printf ("%c", Str[i]);//Longest word    }

5). Jesus has 13 disciples, one of whom is a traitor who betrayed Jesus, please use the rule of exclusion to find the traitor: 13 people sitting around, starting from the first number: 1,2,3,1,2,3 .... Anyone who reports to "3" exits the circle, and the last person who stays in the circle is the traitor who betrayed him. Please find out its original serial number.


ideas:
1. How to describe 13 people? Defining arrays
2. How to count off? Define a variable count log off
3. Once the count is 3, what do you do with it?
4. How do I get rid of a person? Place the element 0
5. Indicates the current or the number of people? Define the variable number;
6. Because of the number of uncertain cycles, cycle conditions with Whlie numbers > 1
7. How to cycle off? If i = 13, go to the first person to continue the count.

The code is as follows:

    int arr[13] ={1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one,    a. int count = 0, number = 13,i = 0;        while (number > 1) {        if (arr[i]! = 0) {            count++;        if (count = = 3) {                arr[i] = 0;                number--;                Count = 0;            }        }         i++;    if (i = = 13) {//subscript            i = 0;//and first person        }           }        //Log group traversal, find the living person for    (int i = 0; i <; i++) {        if (arr[i]! = 0) {            printf ("Currently living Person:%d\n", Arr[i]);}    }


6), there are 1 million numbers, each value range is 0-999999 to find out the number of repetitions, repetitions.

         int n[1000000];    for (int i = 0; i < 1000000; i++) {        //randomly generate 1 million numbers, range 0-999999        n[i] = arc4random ()% 999999;        printf ("%d", N[i]);    }     printf ("\ n");    int count = 0; int temp = 0;    for (int i = 0; i < 1000000; i++) {             count = 0;        for (int j = i; j < 1000000  ; j + +) {            if (n[i] = = N[j] && i! = J && N[i]! =-1) {                count++ ;                temp = N[i];                N[J] =  -1;            }        }        N[i] =-1;        if (Count > 0) {            printf ("Number of repetitions:%d, repetitions:%d\n", Temp,count);        }    }


"Learn the path of iOS: C" array. Loops. Sum of statements use test questions practice

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.