C语言学习

36 阅读1分钟
int main(){
    int age = 0;
    printf("输入年龄:");
    scanf("%d", &age); 
    
    int time = 0;
    // 明确提示24小时制,避免输入歧义
    printf("输入时间(24小时制,如9=上午9点,19=晚上7点):");
    scanf("%d", &time);
    
    int isVip = 0;
    printf("输入是否会员(1:是,0:否):"); 
    scanf("%d", &isVip); 
    
    // 优化输出格式,让会员状态更易读
    printf("您的年龄是%d岁,看电影的时间是:%d点,是否会员:%s\n",
           age, time, isVip ? "是" : "否");
    
    double price = 0;
    // 时段定价:12点前50元,12点及以后80元
    if(time < 12){
        price = 50.0;
    } else {
        price = 80.0;
    }
    
    // 年龄折扣:6岁及以下5折,60岁及以上7折
    if(age <= 6){
        price *= 0.5;
    } else if(age >= 60){
        price *= 0.7;
    }
    
    // 会员折扣:9折(叠加年龄折扣)
    if(isVip){
        price *= 0.9;
    }
    
    // 修正逻辑优先级:老人/小孩 且 12点后,再减10元(根据实际需求调整)
    if( (age <= 6 || age >= 60) && time >= 12 ){
        price -= 10;
        // 防止折扣后价格为负(极端情况,如6岁以下+会员+12点后:50*0.5*0.9-10=22.5-10=12.5,无需处理)
        if(price < 0) price = 0;
    }
    
    // 用%g输出(自动去掉末尾0,如45.0输出45,22.5输出22.5)
    printf("您需要付费:%g元\n", price);
    return 0;
}

image.png