上学迟到问题

刚看到这题时,我突然就有了思路。说起来挺可笑的当我写完这题后,并成功AC时,看着我那冗长的代码我陷入了深思,难道真的有必要写这么一长串吗?

这是我的代码

#include<stdio.h>
int main(){
	int s,v,t;
	scanf("%d %d",&s,&v);
	if(s%v!=0) t = s/v + 1 + 10;
	else  t = s/v + 10;
	int x = t%60;
	int y = (t-x)/60;
    if(t<=480)
    {if(x>0){
    	if(60-x<10) printf("0%d:0%d",8-y-1,60-x);
    	else printf("0%d:%d",8-y-1,60-x);}
    	else printf("0%d:00",8 - y);
	}
	else{
		t = t - 480;
		int i = t%60;
		int j = t/60;
		if(t<=840){if(i>0){
			if(60-i<10) printf("%d:0%d",24-j-1,60-i);
			else printf("%d:%d",24-j-1,60-i);}
			else printf("%d:00",24-j);
		}else{
			t = t - 840;
			int a = t%60;
			int b = t/60;
			if(a>0){if(60-a<10) printf("0%d:0%d",10-b-1,60-a);
			else printf("0%d:%d",10-b-1,60-a);}
			else printf("0%d:00",10-b);
		}
		
	}
	
	return 0;
}

完全就是高中生分类讨论的思维,好在是细心, 一遍就ac要是哪里粗心一点,就真的无从检查了:思路其实很好理解,分三个时间段,08到00小时,24到10小时,10到08小时,因为这三段的代码我没有想到如何统一所以只能这样了。

                                                          这是题解代码

#include <iostream>
#include <cstdio> 
#include <cmath> 

using namespace std;

const int CLK = 24;  // 24小时制 
const int LMT_H = 8;  // 到校小时上限 
const int LMT_M = 0;  // 到校分钟上限 
const int EXT = 10;  // 额外消耗分钟数

int main() {
	int s, v;
	cin >> s >> v;
	
	int costM = ceil(1.0 * s / v) + EXT;  // 计算消耗分钟
	int costH = ceil(1.0 * costM / 60);  // 计算消耗小时
	costM %= 60;  // 保留60进制下的分钟部分 
	printf("%02d:%02d", (LMT_H - costH + CLK) % CLK, (LMT_M - costM + 60) % 60);  // 输出结果 

	return 0;
}

虽然我还是小白看不懂题解,但我是真的羡慕简洁的语言所带来的优美感!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值