You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写从指定格式字符串计算手机用户话费的代码技术问询

Phone Bill Calculation Solution

Alright, let's work through this phone bill problem together. First, since the third rule about the longest call per number is incomplete, I'll go with the common real-world variant where the longest call for a given phone number is free—if your actual rule is different, you can tweak the code easily later.

Clarified Billing Rules

Let's restate the rules clearly to avoid confusion:

  • Calls shorter than 5 minutes (300 seconds): Charge by the number of seconds elapsed, at 5 cents per second.
  • Calls 5 minutes or longer: Charge by the number of started minutes (e.g., a 6 minute 3 second call counts as 7 minutes), at 150 cents per minute.
  • Longest call per number: The longest call for each phone number is not charged (adjust this logic if your rule differs).

Step-by-Step Implementation (Python)

Here's a practical code implementation that handles the input string, processes each call, and calculates the total bill:

def calculate_phone_bill(call_records_str):
    # Split the input string into individual call records, cleaning up whitespace
    records = [record.strip() for record in call_records_str.split(', ') if record.strip()]
    
    # Group call durations by phone number
    call_data = {}
    
    for record in records:
        # Split each record into duration string and phone number
        duration_str, phone_num = record.split(',')
        hh, mm, ss = map(int, duration_str.split(':'))
        
        # Convert duration to total seconds for easier calculations
        total_seconds = hh * 3600 + mm * 60 + ss
        
        # Add duration to the corresponding phone number's list
        if phone_num not in call_data:
            call_data[phone_num] = []
        call_data[phone_num].append(total_seconds)
    
    total_bill = 0
    
    for num, durations in call_data.items():
        if not durations:
            continue
        
        # Identify the longest call for this number to exclude from charges
        longest_duration = max(durations)
        
        # Calculate cost for all calls except the longest one
        for duration in durations:
            if duration == longest_duration:
                continue  # Skip charging the longest call
            
            if duration < 300:
                # Charge by the second for short calls
                total_bill += duration * 5
            else:
                # Calculate started minutes (round up to the next full minute)
                started_minutes = (duration + 59) // 60
                total_bill += started_minutes * 150
    
    return total_bill

# Example usage with your sample input
input_str = "00:05:01,444-555-701, 00:06:03,111-222-333, 00:01:20,444-555-701"
total_cents = calculate_phone_bill(input_str)
print(f"Total bill: ${total_cents / 100:.2f}")  # Output: Total bill: $14.50

How This Works (Using the Sample Input)

Let's break down the sample calculation to verify:

  1. For 444-555-701: Two calls (301 seconds and 80 seconds). The longest call (301 seconds) is free, so we only charge the 80-second call: 80 * 5 = 400 cents ($4.00).
  2. For 111-222-333: One call of 363 seconds (over 5 minutes). We calculate started minutes as (363 + 59) // 60 = 7, so charge is 7 * 150 = 1050 cents ($10.50).
  3. Total bill: 400 + 1050 = 1450 cents ($14.50).

If your third rule isn't "longest call free", just modify the section where we skip the longest duration—for example, if it's a 50% discount on the longest call, replace the continue with a discounted charge calculation instead.

内容的提问来源于stack exchange,提问作者George Messiha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:04:03