hdoj1036简单题、格式化输出

本文介绍了一个使用C++编写的程序,该程序能够读取参赛者的人数和公里数,并处理每支队伍的数据,计算并输出每支队伍的平均分钟数/公里。程序还包含了对无效输入的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include <math.h>
#include<string>
using namespace std;

int round(double a){
	if(a - floor(a) >= 0.5){
		a = (int)(a + 1);
	}
	return a;
}

int main(){
	int n1, n2;//n2是队伍号码
	string n4;//n4是时间
	int total;
	int flag;
	double n3;
	cin>>n1>>n3;//n1是接力人数,n3是公里数
	while(cin>>n2){//处理每一队的数据
		total = 0;
		cout.width(3);
		cout<<n2<<": ";	
		flag = 0;
		for(int i = 0; i < n1; i++){
			cin>>n4;
			if(n4 != "-:--:--"){
				total+=(int)(n4[6] - 48);
				total+=((int)(n4[5] - 48)) * 10;
				total+=((int)(n4[3] - 48)) * 60;
				total+=((int)(n4[2] - 48)) * 600;
				total+=((int)(n4[0] - 48)) * 3600;
			}else{
				flag = 1;
				cout<<"-"<<endl;
				if(i != n1 - 1){
					string s1;
					getline(cin, s1);
					break;
				}
			}
		}
		if(flag == 1){
			continue;
		}
		total = round(total / n3);
		int t1, t2, t3, t4, t5;
		t1 = total / 3600;
		if(t1){
			cout<<t1<<":";
			total %= 3600;
		}
		t2 = total / 600;
		if(t2){
			cout<<t2;
			total %= 600;
		}else if(t1){
			cout<<t2;
			total %= 600;
		}

		t3 = total / 60;
		cout<<t3;
		if(t3){
			total %= 60;
		}
		cout<<":";

		t4 = total /10;
		cout<<t4;
		if(t4){
			total %= 10;
		}

		t5 = total;
		cout<<total;

		cout<<" min/km"<<endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值