wu


私信TA

用户名:cncfvc

访问量:215693

签 名:

读研狗没有时间刷题了~~

等  级
排  名 2
经  验 36073
参赛次数 8
文章发表 265
年  龄 25
在职情况 学生
学  校 电子科技大学
专  业 通信工程

  自我简介:

写代码 真好玩 ~

对于这道题目 用cin,cout来进行输入输出的操作 会超时  而用scanf printf来输入输出却AC 了


这是因为 很多编译器对scanf printf进行了处理和优化 导致scanf printf处理数据的速度会比cin,cout更快

所以建议大家在参加一些比赛时  尽量用scanf 和printf 来进行输入输出操作 这样可以更快一些

#include<iostream>
using namespace std;
void Print(int n)
{
int h,m,s;
h=n/60/60;
//cout<<"h== "<<h<<endl;
m=((n-h*60*60)/60);
//cout<<"m== "<<m<<endl;
s=(n-h*60*60-m*60);
//cout<<"s== "<<s<<endl;
if(h<10)
cout<<"0"<<h;
else
cout<<h ;
cout<<":";
if(m<10)
cout<<"0"<<m;
else
cout<<m;
cout<<":";
if(s<10)
cout<<"0"<<s;
else
cout<<s<<endl;
}
int main()
{
int T;
int n;
cin>>T;
while(T--)
{
cin>>n;
Print(n);
}
return 0;
}

以上代码会超时

下列代码则可以AC

#include<iostream>
#include<cstdio> 
using namespace std;
inline void Print(int n)
{
	int h,m,s;
	h=n/60/60;
	m=((n-h*60*60)/60);
	s=(n-h*60*60-m*60);
	if(h<10)
	printf("%d%d",0,h);
	else
	printf("%d",h);
	printf(":");
	if(m<10)
	printf("%d%d",0,m);
	else
	printf("%d",m);
	printf(":");
	if(s<10)
	printf("%d%d",0,s);
	else
	printf("%d",s); 
	printf("\n"); 	
}
int main()
{
	freopen("data.in.txt","r",stdin);
	int T;
	int n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		Print(n);	
	} 
	return 0;
}

最后附上一个很简单的做法

#include<stdio.h>
int main()
{
	int n,a;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&a);
		printf("%02d:%02d:%02d\n",a/3600,a%3600/60,a%60);
	}
	return 0;
}


 

0.0分

1 人评分

  评论区