编写代码:某公司现需要统计员工出勤次数,具体要求如下: 输入样例: Mark Tom Ivor Mark Ivor Mark Jack end 输入样例解释: 每行表示某天出勤的员工名单,以空格间隔。 end表示输入结束
时间: 2024-03-22 12:38:29 浏览: 73
好的,以下是 Python 代码实现:
```python
attendances = {}
while True:
input_str = input().strip()
if input_str == 'end':
break
employees = input_str.split()
for employee in employees:
if employee not in attendances:
attendances[employee] = 1
else:
attendances[employee] += 1
for employee, count in attendances.items():
print(f'{employee}: {count}次')
```
这段代码使用了一个字典 `attendances` 来存储每个员工的出勤次数,每次输入一行员工名单时,遍历其中的每个员工,如果该员工尚未出现在 `attendances` 中,则将其加入字典并设置出勤次数为 1,否则将其出勤次数加 1。最后遍历字典输出每个员工的出勤次数。
相关问题
某公司现需要统计员工出勤次数,具体要求如下: 输入样例: Mark Tom Ivor Mark Ivor Mark Jack end 输入样例解释: 每行表示某天出勤的员工名单,以空格间隔。 end表示输入结束 输出样例: Mark 3 Ivor 2 Tom 1 Jack 1
好的,这是一个简单的统计问题。我可以为您提供以下Python代码实现:
```
employee_attendance = {}
while True:
attendance_list = input().split()
if attendance_list[0] == 'end':
break
for employee in attendance_list:
if employee in employee_attendance:
employee_attendance[employee] += 1
else:
employee_attendance[employee] = 1
for employee, attendance_count in employee_attendance.items():
print(employee, attendance_count)
```
在这个代码中,我们使用了一个字典employee_attendance来存储每个员工的出勤次数。我们首先读取输入,然后对于每个员工,我们检查是否已经在字典中存在,如果存在,我们增加他们的出勤次数,如果不存在,我们将他们添加到字典中并设置他们的出勤次数为1。最后,我们遍历字典并打印每个员工的出勤次数。
java某公司现需要统计员工出勤次数,具体要求如下: 输入样例: Mark Tom Ivor Mark Ivor Mark Jack end 输入样例解释: 每行表示某天出勤的员工名单,以空格间隔。 end表示输入结束 输出样例: Mark 3 Ivor 2 Tom 1 Jack 1
好的,以下是Java实现此问题的代码:
```java
import java.util.*;
public class EmployeeAttendance {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Map<String, Integer> employeeAttendance = new HashMap<>();
while (true) {
String[] attendanceList = input.nextLine().split(" ");
if (attendanceList[0].equals("end")) {
break;
}
for (String employee : attendanceList) {
if (employeeAttendance.containsKey(employee)) {
employeeAttendance.put(employee, employeeAttendance.get(employee) + 1);
} else {
employeeAttendance.put(employee, 1);
}
}
}
for (Map.Entry<String, Integer> entry : employeeAttendance.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
```
在这个Java代码中,我们使用了一个HashMap对象employeeAttendance来存储每个员工的出勤次数。我们首先读取输入,然后对于每个员工,我们检查是否已经在Map中存在,如果存在,我们增加他们的出勤次数,如果不存在,我们将他们添加到Map中并设置他们的出勤次数为1。最后,我们遍历Map并打印每个员工的出勤次数。