python if统计人数_统计指定数字总数的Python实现

功能说明

统计指定范围[0, n]内所有的奇数数字中,3的数量总和。

实现代码

#!/usr/bin/python

def calcCountOfDigit3InBase(base):

if base == 10: return 1

return 10 * calcCountOfDigit3InBase(base / 10) + (base / 20)

def calcCountOfDigit3(n):

base = 10

while n / base > 0:

base *= 10

base /= 10

count = 0

while base >= 10:

m = n / base

count += m * calcCountOfDigit3InBase(base)

if m > 3:

count += base / 2

if m == 3:

count += ((n + 1) % base) / 2

n = n % base

base /= 10

if n >= 3:

count += 1

return count

def printCountOfDigit3(n):

print("%s = %s" % (n, calcCountOfDigit3(n)))

输出结果

printCountOfDigit3(800000000)

printCountOfDigit3( 60000000)

printCountOfDigit3( 6000000)

printCountOfDigit3( 200000)

printCountOfDigit3( 70000)

printCountOfDigit3( 8000)

printCountOfDigit3( 100)

printCountOfDigit3( 70)

printCountOfDigit3( 1)

printCountOfDigit3(866278171)

seewin@seewin:~$ python StatsCountOfDigit3.py

800000000 = 410000000

60000000 = 29000000

6000000 = 2600000

200000 = 60000

70000 = 22500

8000 = 2100

100 = 15

70 = 12

1 = 0

866278171 = 441684627

不知结论正确与否,欢迎批评指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果想用 Python 实现统计员工工资,可以按照下面的步骤进行: 1. 创建一个存储员工信息的数据结构,例如字典,其包含员工姓名和工资信息。 2. 对于每个员工,读取其姓名和工资,并将这些信息存储在数据结构。 3. 遍历数据结构,统计所有员工的工资和。 4. 输出员工工资和。 代码示例: ``` employees = {} while True: name = input("请输入员工姓名(输入q退出):") if name == 'q': break salary = int(input("请输入员工工资:")) employees[name] = salary total_salary = 0 for salary in employees.values(): total_salary += salary print("员工工资和为:", total_salary) ``` 希望这个代码示例对你有所帮助! ### 回答2: 使用Python编程语言来统计员工工资可以通过以下几个步骤来实现: 1. 首先,我们需要定义一个类来表示员工。该类可以包含以下属性:姓名、员工ID、职位、工资等级和基本工资。可以使用构造函数来初始化这些属性。 2. 接下来,我们可以定义一个函数来计算员工的实际工资。这个函数可以根据不同的工资等级来计算不同的津贴和奖金。通过将基本工资与津贴和奖金相加,我们可以得到员工的实际工资。 3. 接着,我们可以定义一个列表来存储所有的员工对象。可以使用循环和用户输入来动态地创建和添加员工对象到列表。 4. 然后,我们可以定义一个函数来统计员工工资。这个函数可以遍历员工列表,调用计算工资的函数来计算每个员工的实际工资,并将结果累加到工资。 5. 最后,我们可以输出统计结果,包括员工总数工资。 整体代码的实现如下: ``` class Employee: def __init__(self, name, employee_id, position, salary_grade, basic_salary): self.name = name self.employee_id = employee_id self.position = position self.salary_grade = salary_grade self.basic_salary = basic_salary def calculate_salary(employee): allowances = { 1: 1000, 2: 1500, 3: 2000 } bonuses = { 1: 500, 2: 1000, 3: 1500 } allowance = allowances.get(employee.salary_grade, 0) bonus = bonuses.get(employee.salary_grade, 0) return employee.basic_salary + allowance + bonus def calculate_total_salary(employees): total_salary = 0 for employee in employees: total_salary += calculate_salary(employee) return total_salary employees = [] while True: name = input("请输入员工姓名:") if name == "": break employee_id = input("请输入员工ID:") position = input("请输入员工职位:") salary_grade = int(input("请输入员工工资等级(1-3):")) basic_salary = float(input("请输入员工基本工资:")) employee = Employee(name, employee_id, position, salary_grade, basic_salary) employees.append(employee) total_salary = calculate_total_salary(employees) total_count = len(employees) print("员工总数:", total_count) print("员工工资:", total_salary) ``` 这是一个简单的统计员工工资的实现,根据不同的工资等级给予不同的津贴和奖金,并计算最终的工资和。最后输出员工总数工资的统计结果。 ### 回答3: 要用Python实现统计员工工资,首先需要定义一个员工类。可以使用类的属性来表示员工的姓名、工号和工资,使用成员方法来实现对员工工资的统计。 以下是一个示例的实现代码: ```python class Employee: def __init__(self, name, emp_id, salary): self.name = name self.emp_id = emp_id self.salary = salary def get_salary(self): return self.salary # 以示例数据初始化员工对象 employee1 = Employee("张三", "001", 5000) employee2 = Employee("李四", "002", 6000) employee3 = Employee("王五", "003", 7000) # 统计员工工资 total_salary = 0 total_salary += employee1.get_salary() total_salary += employee2.get_salary() total_salary += employee3.get_salary() print("工资:", total_salary) ``` 在上述代码,首先定义了一个名为Employee的类。该类有三个属性:姓名(name)、工号(emp_id)和工资(salary),并有一个成员方法 get_salary()用来返回员工的工资。 接下来使用这个类创建了三个员工对象,并按照其工资分别累加到工资变量total_salary。 最后,打印出工资的值。 这就是用Python实现统计员工工资的简单示例。当然,实际应用可能需要更复杂的逻辑或数据处理,但这个示例可以作为一个基本的框架来实现更复杂的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值