首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【优秀题解】蓝桥杯ACM输入输出问题的

【优秀题解】蓝桥杯ACM输入输出问题的

作者头像
编程范 源代码公司
发布于 2018-04-16 11:46:42
发布于 2018-04-16 11:46:42
9610
举报

背景,对于参加蓝桥杯ACM竞赛的同学,输入输出很常见,

咱们网站(www.dotcpp.com)1562这个题对于这道题目 用cin,cout来进行输入输出的操作 会超时 而用scanf printf来输入输出却AC 了

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

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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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;
}

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

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档