Firstly, input an integer � N, which represents a total of � N students. Then enter � N lines, each containing the student's name, class, exam 1 score, and exam 2 score. Count the average and total scores of each exam for each class, and sort the results by class name before outputting them to the console. Output includes class, score1_mean, score1_sum, score2_mean, score2_sum.
时间: 2024-02-05 11:04:54 浏览: 80
Sure, I can help you with that. Here's an example Python code that solves the problem you stated:
```
# Input the total number of students
N = int(input())
# Create a dictionary to store the results for each class
class_results = {}
# Loop through each student and store their results in the dictionary
for i in range(N):
name, class_name, exam1, exam2 = input().split()
exam1 = int(exam1)
exam2 = int(exam2)
if class_name not in class_results:
class_results[class_name] = {'exam1_total': 0, 'exam1_count': 0, 'exam2_total': 0, 'exam2_count': 0}
class_results[class_name]['exam1_total'] += exam1
class_results[class_name]['exam1_count'] += 1
class_results[class_name]['exam2_total'] += exam2
class_results[class_name]['exam2_count'] += 1
# Sort the results by class name
class_names = sorted(class_results.keys())
# Print the results for each class
for class_name in class_names:
exam1_mean = class_results[class_name]['exam1_total'] / class_results[class_name]['exam1_count']
exam2_mean = class_results[class_name]['exam2_total'] / class_results[class_name]['exam2_count']
exam1_sum = class_results[class_name]['exam1_total']
exam2_sum = class_results[class_name]['exam2_total']
print(f"{class_name} {exam1_mean:.2f} {exam1_sum} {exam2_mean:.2f} {exam2_sum}")
```
Here's how you can use this code:
1. Copy and paste the code into a Python file (e.g. `main.py`).
2. Run the Python file using the command `python main.py`.
3. Enter the total number of students when prompted.
4. Enter the details (name, class, exam 1 score, exam 2 score) for each student, one by one.
5. The program will print the results for each class, sorted by class name.
I hope this helps! Let me know if you have any questions.
阅读全文