求平均分的程序可以根据不同的编程语言和需求有多种实现方法。以下是几种常见编程语言的实现示例:
1. Python
```python
def calculate_average(scores):
return sum(scores) / len(scores)
示例输入
scores = [100, 90, 80, 70]
计算平均分
average_score = calculate_average(scores)
print(f"平均分: {average_score:.2f}")
```
2. Java
```java
public class CalculateAverage {
public static void main(String[] args) {
int[] scores = {100, 90, 80, 70};
double averageScore = calculateAverage(scores);
System.out.printf("平均分: %.2f%n", averageScore);
}
public static double calculateAverage(int[] scores) {
int sum = 0;
for (int score : scores) {
sum += score;
}
return (double) sum / scores.length;
}
}
```
3. C++
```cpp
include include double calculateAverage(const std::vector int sum = 0; for (int score : scores) { sum += score; } return static_cast } int main() { std::vector double averageScore = calculateAverage(scores); std::cout << "平均分: " << averageScore << std::endl; return 0; } ``` 4. JavaScript ```javascript function calculateAverage(scores) { let sum = 0; for (let score of scores) { sum += score; } return sum / scores.length; } // 示例输入 const scores = [100, 90, 80, 70]; // 计算平均分 const averageScore = calculateAverage(scores); console.log(`平均分: ${averageScore.toFixed(2)}`); ``` 5. Excel 在Excel中,你可以使用以下公式来计算平均分: ```excel =AVERAGE(A1:A10) ``` 其中,A1:A10是包含成绩数据的单元格范围。你也可以使用自动求和功能或数据透视表来计算平均值。 总结 以上示例展示了如何在不同编程语言中计算平均分。你可以根据具体需求和使用的工具选择合适的方法。