任务详情 请用多种方式实现:输入5个整数,输出其 中的最大值。
时间: 2024-11-05 22:24:08 浏览: 17
任务详情是让你编写一个程序,该程序需要接收用户输入的五个整数,然后计算并找出这五个数中的最大值。以下是几种常见的编程语言实现方式:
1. Python:
```python
def find_max(numbers):
return max(numbers)
inputs = list(map(int, input("请输入五个整数,用逗号分隔:").split(",")))
max_value = find_max(inputs)
print("最大值是:", max_value)
```
2. Java:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入五个整数,用空格分隔:");
int[] numbers = new int[5];
for (int i = 0; i < 5; i++) {
numbers[i] = scanner.nextInt();
}
int maxValue = getMax(numbers);
System.out.println("最大值是:" + maxValue);
}
public static int getMax(int[] numbers) {
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
return max;
}
}
```
3. JavaScript:
```javascript
function getMax() {
const numbers = readline().split(',').map(Number);
let max = -Infinity;
for (let num of numbers) {
if (num > max) {
max = num;
}
}
console.log(`最大值是:${max}`);
}
getMax();
```
4. C++:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int findMax(const std::vector<int>& numbers) {
return *std::max_element(numbers.begin(), numbers.end());
}
int main() {
std::vector<int> inputs;
for (int i = 0; i < 5; ++i) {
int number;
std::cin >> number;
inputs.push_back(number);
}
int max_value = findMax(inputs);
std::cout << "最大值是:" << max_value << std::endl;
return 0;
}
```
阅读全文