Location>code7788 >text

Computer 2 C in 2 Months - Question (6) Explanation

Popularity:299 ℃/2024-10-30 10:11:39

1. Preamble

In this post we explain2 months to get computer level 2 C language-Facts 6

真题6-程序评分

2. Procedural fill-in-the-blank questions

2.1 Title requirements

真题6-程序填空

2.2 Code provided

#include <>
unsigned long fun(unsigned long n) {
    unsigned long x = 0;
    int           t;
    while (n) {
        t = n % 10;
        /**********found**********/
        if (t % 2 == __1__)
        /**********found**********/
            x = __2__ + t;
        /**********found**********/
        n = __3__;
    }
    return x;
}
main() {
    unsigned long n = -1;
    while (n > 99999999 || n < 0) {
        printf("Please input(0<n<100000000): ");
        scanf("%ld", &n);
    }
    printf("\nThe result is: %ld\n", fun(n));
    getchar();
}

2.3 Problem solving ideas

The question asks to take out the even number on each side, you need to take out the number on each side and then determine whether the number is even or not, the even number can be determined by the result of dividing the number by 2 and taking the remainder, if the result is 0, then it is even, and if the result is 1, then it is odd.

Fill in the blank at (1):

This is used to determine whether the number of digits taken out is even or not, we have talked about how to determine whether it is even or not, so here we need to let it determine whether it is equal to 0 or not.

if (t % 2 == 0)

Fill in the blank at (2):

The function we want to implement here is to reorganize a new number, and the question asks for the opposite order, so we need to let thex * 10, demonstrated in the code of the following program:

// if t = 6 the condition is satisfied, x = 0 * 10 + 6, then x is 6
// if t = 9, n = n / 10; // if t = 4, x = 6 * 10 + 4, then x is 64
// if t = 4, x = 6 * 10 + 4, then x is 64
// if t = 8, x = 64 * 10 + 8, then x is 648
// Same as below
x = x * 10 + t.

Fill in the blank at (3):

What we're trying to accomplish here is to remove the single digit taken out earlier, and change the original tens digit to a single digit, so that the next time the loop goes int = n % 10;A new value is taken out.

n = n / 10;

2.4 Code Implementation

Fill in the complete code:

#include <>
unsigned long fun(unsigned long n) {
    unsigned long x = 0;
    int t;
    while (n) {
        t = n % 10;
        /**********found**********/
        if (t % 2 == 0) // get rid of 2 The remainder is equal to 0,is an integer
        /**********found**********/
            x = x * 10 + t; // commander-in-chief (military) t The value of the x the units place (or column) in the decimal system
        /**********found**********/
        n = n / 10; // 逐次get rid of去 n the units place (or column) in the decimal system
    }
    return x;
}
main() {
    unsigned long n = -1;
    while (n > 99999999 || n < 0) {
        printf("Please input(0<n<100000000): ");
        scanf("%ld", &n);
    }
    printf("\nThe result is: %ld\n", fun(n));
    getchar();
    getchar();
}

Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.

3. Program modification questions

3.1 Title requirements

真题6-程序修改

3.2 Code provided

#include <>
void fun(char* p) {
    char max, *q;
    int  i = 0;
    max    = p[i];
    q      = p;
    while (p[i] != 0) {
        if (max < p[i]) {
            max = p[i];
    /**********found**********/
            q = p + i
        }
        i++;
    }
    /**********found**********/
    wihle(q > p) {
        *q = *(q - 1);
        q--;
    }
    p[0] = max;
}
main() {
    char str[80];
    printf("Enter a string:  ");
    gets(str);
    printf("\nThe original string:      ");
    puts(str);
    fun(str);
    printf("\nThe string after moving:  ");
    puts(str);
    printf("\n\n");
    getchar();
}

3.3 Problem solving ideas

There are only two errors in this question, a syntax error and a misspelled keyword, both of which are common mistakes made by beginners and require a bit of attention when programming.

Revise (1):

Here it is because the statement is not followed by;Plus just fine.

q = p + i;

Amend (2):

This sentence is becausewhileWritten in error. For this kind of error, it is more likely that the error ismainIt will be written asmianIt is also important to note that this leads to errors in the program.

while(q > p) {

3.4 Code Implementation

Modified code:

#include <>
void fun(char* p) {
    char max, *q;
    int  i = 0;
    max    = p[i];
    q      = p;
    while (p[i] != 0) {
        if (max < p[i]) {
            max = p[i];
    /**********found**********/
            q = p + i;
        }
        i++;
    }
    /**********found**********/
    while(q > p) {
        *q = *(q - 1);
        q--;
    }
    p[0] = max;
}
main() {
    char str[80];
    printf("Enter a string:  ");
    gets(str);
    printf("\nThe original string:      ");
    puts(str);
    fun(str);
    printf("\nThe string after moving:  ");
    puts(str);
    printf("\n\n");
    getchar();
}

Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.

4. Programming questions

4.1 Title requirements

真题6-程序设计

4.2 Code provided

#include <>
#pragma warning(disable : 4996)
#define N 10
int fun(int x[], int e, int* sum) {
}
main() {
    void NONO();
    int x[N] = {1, 7, 8, 6, 10, 15, 11, 13, 29, 31}, e = 3, n, sum;
    n = fun(x, e, &sum);
    printf("n=%d,sum=%d\n", n, sum);
    NONO();
}

void NONO() {
    /* Please open the file within this function,Input test data,call (programming) fun function (math.),
       output data,Close file。 */
    int i, j, x[10], n, e, sum;
    FILE *rf, *wf;

    rf = fopen("", "r");
    wf = fopen("", "w");
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 10; j++)
            fscanf(rf, "%d ", &x[j]);
        fscanf(rf, "%d", &e);
        n = fun(x, e, &sum);
        fprintf(wf, "%d, %d\n", n, sum);
    }
    fclose(rf);
    fclose(wf);
}

4.3 Problem Solving Ideas

Based on the array provided in the titlex, integereand the output can be seen to require an array that is divisible byethat is, dividingeof multiples, so the title of this question is misdescribed.

Then we just need to iterate through the arrayx, to determine whether an element is divisible by an integereIf yes, the count is increased by 1, otherwise the sum is increased by the number stored in the element.

4.4 Code Implementation

Fill in the complete code:

#include <>
#pragma warning(disable : 4996)
#define N 10
int fun(int x[], int e, int* sum) {
    int count = 0, i = 0;

    *sum = 0; // Empty the sum store to prevent garbage values.

    for (i = 0; i < N; i++) { // Iterate through the array X in the range 0 ~ (N-1)
        if (x[i] % e == 0) { // determine if e is divisible
            count++; // count
        } else {
            *sum += x[i]; // sum the numbers
        }
    }

    return count; }
}
main() {
    void NONO(); int x[N] = {1, 7, 8, 6, 10, 15, 11, 13, 29, 31}
    int x[N] = {1, 7, 8, 6, 10, 15, 11, 13, 29, 31}, e = 3, n, sum; n = fun(x, e, &sum);; return count; } main() { void NONO(); }
    n = fun(x, e, &sum);
    printf("n=%d,sum=%d\n", n, sum);
    NONO();
    getchar();
    getchar();
}

void NONO() {
    /* Please open the file, input the test data, call the fun function, output the data, and close the file in this function.
       Output the data and close the file. */
    int i, j, x[10], n, e, sum;
    FILE *rf, *wf.

    rf = fopen("", "r");
    wf = fopen("", "w");
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 10; j++)
            fscanf(rf, "%d ", &x[j]);
        fscanf(rf, "%d ", &e).
        n = fun(x, e, &sum);
        fprintf(wf, "%d, %d\n", n, sum);
    }
    fclose(rf);
    fclose(wf);
}

Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.

5. Postscript

This is the end of this blog, if you have questions or suggestions you are welcome to leave them in the comments section.