Analysis of C Language Exercises 4

Analysis of C Language Exercises 4

Analysis of C Language Exercises

01

Sum of Series

Analysis of C Language Exercises 4

Solution:

1. Read integer k;

2. Initialize n’s value, sum=0.0;

3. Start a loop while sum <= k;

4. Accumulate from 1/n and assign to sum;

Simultaneously increment n;

5. When sum > k, exit the loop and output n-1, end the program;

Solution source: Lü Wentao

02

Sum of Sequence

Analysis of C Language Exercises 4Analysis of C Language Exercises 4

Solution:

The problem requires calculating the cumulative sum from 1 to n, and cannot use the arithmetic series sum formula, only through iterative accumulation.

By iterating through all integers from 1 to n, each number is added to an initial total variable set to 0, ultimately obtaining the result.

Solution source: Liu Ruiyang

03

Name of the Student with the Highest Score

Analysis of C Language Exercises 4

Solution:

#include <stdio.h>

#include <string.h> // For string copy function strcpy

int main() {

int N; // Store the number of students

scanf(“%d”, &N); // Read the number of students

int max_score = -1; // Record the highest score, initially set to -1 (score is non-negative, ensuring it can be updated)

char max_name[21]; // Store the name of the student with the highest score (length not exceeding 20, leaving 1 for the null terminator)

// Loop to read information for N students

for (int i = 0; i < N; i++) {

int score; // Temporarily store the current student’s score

char name[21]; // Temporarily store the current student’s name

scanf(“%d %s”, &score, name); // Read score and name

// If the current student’s score is higher than the recorded highest score

if (score > max_score) {

max_score = score; // Update the highest score

strcpy(max_name, name); // Copy the current student’s name to max_name

}

}

printf(“%s\n”, max_name); // Output the name of the student with the highest score

return 0;

}

Solution source: Qiu Xuanyue

04

Distributing Soft Drinks

Analysis of C Language Exercises 4

Solution:

1. The problem states that the final output consists of two results: the total milliliters of drink obtained and the number of cups.

2. First, set two variables, n (integer) and t (float),

(1) t is the amount of soft drink each student receives in milliliters, thus t=t/n;

(2) n is the total number of cups needed, knowing that each student requires 2 cups, thus n=n*2.

3. Note: The problem requires the output to be strictly accurate to three decimal places.

Solution source: Liu Yuyue

05

Finding the Minimum Value

Analysis of C Language Exercises 4Analysis of C Language Exercises 4

Solution:

According to the problem requirements, first, declare the number of digits n, non-negative integer num, and minimum value min;

Then, read n, and read the first number, initializing it as the current minimum value;

Next, loop to read the remaining numbers, each time comparing the current number with the minimum value, updating the minimum value if smaller;

Finally, output the minimum value.

Solution source: Zhang Tianxiao

Analysis of C Language Exercises 4

END

Analysis of C Language Exercises 4

Information A Class Competition Association

Editor: Chen Junheng

Chief Editor: Zou Yicheng

Reviewer: Tian Jianxue

Analysis of C Language Exercises 4

Leave a Comment