洛谷题库—入门1—顺序结构—P5707 【深基2.例12】上学迟到

一、题目要求

二、代码 

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

//分切函数
vector<string> split(string str,char ch)
{
	vector<string> results;
	stringstream ss(str);
	string temp;
	while(getline(ss,temp,ch))
		{
			results.push_back(temp);
		}	
	return results;	
}

//打印结果
void dayin(int a,int b)
{
	string str1;
	string str2;
	
	if (a<10)
	{
		str1 = "0"+to_string(a);
	}
	else
	{
		str1 = to_string(a);
	}
	if (b<10)
	{
		str2 = "0"+to_string(b);
	}
	else
	{
		str2 = to_string(b);
	}
	
	cout << str1 + ":" + str2 << endl;
}

int main()
{
	//键盘输入
	string str;
	getline(cin,str);
		
	//切分
	vector<string> results;	
	results = split(str,' ');
		
	//字符串转浮点数
	int num1 = stoi(results[0]);
	int num2 = stoi(results[1]);
	
	//计算时间
	int temp = num1 / num2;
	//判断是否加1
	if (num1 % num2 != 0)
	{
		temp = temp + 1;
	}
	
	//临界值
	const int  x = 60 * 8 -10;
	
	//判断
	if (temp < x)
	{
		int a = (8*60-temp-10)/60;
		int b = (8*60-temp-10)%60;
		
		dayin(a,b);
	}
	else if (temp > x)
	{
		if((temp-x) >= 24*60)
		{
			int a = (24*60-(temp-x)%(24*60))/60;
			int b = (24*60-(temp-x)%(24*60))%60;
			
			dayin(a,b);
		}
		else
		{
			int a = (24*60-(temp-x))/60;
			int b = (24*60-(temp-x))%60;
						
			dayin(a,b);
		}
	}
	else
	{
		cout << "00:00" << endl;
	}

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值