您的位置:首页 > 编程语言 > C语言/C++

“输出下一秒”

2015-09-12 01:02 441 查看
绝对原创!!!!

写在前面的话:

昨天2015/09/11 16:30到xxx去机试,那个oj平台有些问题,我提交的代码,在VS2005中运行所有测试用例都能通过,可是上传到oj上就是错误的,

一开始是上传受限,说我的程序大于10MB,太坑了,换了一台电脑,工程师帮我解决了。满是欢喜。

可惜,更惨的还在后面,我的代码在oj上,一直不通过,我看检测报告是“ERROR in Line 1......”,可是我第一行是#include<iostream>呀,

最后找那边工程师过来看,还是没有解决,但是他在VS2005上,帮我测试,所有测试用例都能通过,可惜就是在OJ上不通过,一分都没有拿到...

悲剧呀。后来他们又给我换了一个账号,但是还是那个问题,好悲剧...直接放弃啦...

现在我把代码以及我自己测试的几个结果图片贴出来,望大家指正!!!谢谢,不胜感激!

感觉xxxOJ还是看运气吧!!!好运。

题目:

输出下一秒

输入如:2015/02/09  15:58:59,或者2015/2/9 5:6:12 

输出入:2015/02/09 15:59:00或者2015/02/09 05:06:13

#include<iostream>
#include<string>
using namespace std;

int Format(char *str,char *result,int &year,int &month,int &days,int &hour,int &minute,int &seconds);  //日期与时间格式转换
void NextDateAndTime(char *result,int year,int month,int days,int hour,int minute,int seconds);   //计算下一秒
int StrToInt(char *temp);  //字符转整型
int isrun;
int main()
{
char str[80];
int i,year,month,days;
int hour,minute,seconds;
cin.getline(str,80);
char result[20];
i=Format(str,result,year,month,days,hour,minute,seconds);
if(i>0)
NextDateAndTime(result,year,month,days,hour,minute,seconds);
else
return 0;   //格式非法,直接返回
return 0;
}
int StrToInt(char *temp)
{
int a;
a=0;
while(*temp!='\0')
{
a*=10;
a+=(*temp++) - '0';
}
return a;
}
int Format(char *str,char *result,int &year,int &month,int &days,int &hour,int &minute,int &seconds)
{
char *p;
char t[6];
int i,j,k,m,n,x;
i=0;
j=0;
k=0;
m=0;
p=str;
isrun=0;

if(str[4]!='/')
return 0;

for(i=0;i<5;i++)
result[j++]=str[i];
for(x=0;x<4;x++)
t[x]=result[x];
t[x]='\0';
year=StrToInt(t);
if((year%4==0 && year%100!=0) || (year%400==0))
isrun=1;
p+=5;
while(*p!='/')
{
m++;
p++;
}

if(m<2)
{
result[j++]='0';
for(n=0;n<2;n++)
result[j++]=str[i++];
}
else
{
for(n=0;n<3;n++)
result[j++]=str[i++];
}

for(x=0;x<2;x++)
t[x]=result[x+5];
t[x]='\0';
month=StrToInt(t);
if(month<1||month>12)
return 0;

p++;
m=0;
while(*p!=' ')
{
m++;
p++;
}
if(m<2)
{
result[j++]='0';
for(n=0;n<2;n++)
result[j++]=str[i++];
}
else
{
for(n=0;n<3;n++)
result[j++]=str[i++];
}

for(x=0;x<2;x++)
t[x]=result[x+8];
t[x]='\0';
days=StrToInt(t);
if(month==2)
{
if(isrun==1)
{
if(days<1||days>29)
return 0;
}
else
{
if(days<1||days>28)
return 0;
}
}

if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
{
if(days<1||days>31)
return 0;
}

if((month==4)||(month==6)||(month==9)||(month==11))
{
if(days<1||days>30)
return 0;
}

m=0;
while(*p==' ')
{
m++;
p++;
}
if(m>2)
i=i+m-1;
m=0;
while(*p!=':')
{
m++;
p++;
}

if(m<2)
{
result[j++]='0';
for(n=0;n<2;n++)
result[j++]=str[i++];
}
else
{
for(n=0;n<3;n++)
result[j++]=str[i++];
}

for(x=0;x<2;x++)
t[x]=result[x+11];
t[x]='\0';
hour=StrToInt(t);
if(hour<0||hour>23)
return 0;

m=0;
p++;
while(*p!=':')
{
m++;
p++;
}

if(m<2)
{
result[j++]='0';
for(n=0;n<2;n++)
result[j++]=str[i++];
}
else
{
for(n=0;n<3;n++)
result[j++]=str[i++];
}

for(x=0;x<2;x++)
t[x]=result[x+14];
t[x]='\0';
minute=StrToInt(t);
if(minute<0||minute>59)
return 0;
m=0;
p++;
while(*p!='\0')
{
m++;
p++;
}

if(m<2)
{
result[j++]='0';
for(n=0;n<1;n++)
result[j++]=str[i++];
}
else
{
for(n=0;n<2;n++)
result[j++]=str[i++];
}
result[j]='\0';

for(x=0;x<2;x++)
t[x]=result[x+17];
t[x]='\0';
seconds=StrToInt(t);
if(seconds<0||seconds>59)
return 0;
cout<<result<<endl;
return 1;

}
void NextDateAndTime(char *result,int year,int month,int days,int hour,int minute,int seconds)
{
seconds++;
int t;
t=0;
if(seconds<60)
{
if(seconds>9)
{
t=seconds%10;
result[18]=t+'0';
t=seconds/10;
result[17]=t+'0';
}
else
{
t=seconds%10;
result[18]=t+'0';
}
cout<<result<<endl;
return;
}
else
{
result[18]='0';
result[17]='0';
}
minute++;
if(minute<60)
{
if(minute>9)
{
t=minute%10;
result[15]=t+'0';
t=minute/10;
result[14]=t+'0';
}
else
{
t=minute%10;
result[15]=t+'0';
}
cout<<result<<endl;
return;
}
else
{
result[15]='0';
result[14]='0';
}
hour++;
if(hour<24)
{
if(hour>9)
{
t=hour%10;
result[12]=t+'0';
t=hour/10;
result[11]=t+'0';
}
else
{
t=hour%10;
result[12]=t+'0';
}
cout<<result<<endl;
return;
}
else
{
result[12]='0';
result[11]='0';
}
days++;
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
{
if(days<32)
{
if(days>9)
{
t=days%10;
result[9]=t+'0';
t=days/10;
result[8]=t+'0';
}
else
{
t=days%10;
result[9]=t+'0';
}
cout<<result<<endl;
return;
}
else
{
result[9]='1';
result[8]='0';
}
}

if((month==4)||(month==6)||(month==9)||(month==11))
{
if(days<31)
{
if(days>9)
{
t=days%10;
result[9]=t+'0';
t=days/10;
result[8]=t+'0';
}
else
{
t=days%10;
result[9]=t+'0';
}
cout<<result<<endl;
return;
}
else
{
result[9]='1';
result[8]='0';
}
}
if(month==2)
{
if(isrun)
{
if(days<30)
{
if(days>9)
{
t=days%10;
result[9]=t+'0';
t=days/10;
result[8]=t+'0';
}
else
{
t=days%10;
result[9]=t+'0';
}
cout<<result<<endl;
return;
}
else
{
result[9]='1';
result[8]='0';
}
}
else
{
if(days<29)
{
if(days>9)
{
t=days%10;
result[9]=t+'0';
t=days/10;
result[8]=t+'0';
}
else
{
t=days%10;
result[9]=t+'0';
}
cout<<result<<endl;
return;
}
else
{
result[9]='1';
result[8]='0';
}
}
}

month++;
if(month<13)
{
if(month>9)
{
t=month%10;
result[6]=t+'0';
t=month/10;
result[5]=t+'0';
}
else
{
t=month%10;
result[6]=t+'0';
}
cout<<result<<endl;
return;
}
else
{<pre name="code" class="cpp">            result[6]<span style="font-family: Arial, Helvetica, sans-serif;">='1';</span>
result[5]='0';}year++;for(int i=3;i>=0;i--){t=year%10; result[i]=t+'0'; year=year/10;}cout<<result<<endl;return;}

以下是测试结果。







这是测试闰年是否成功,之前天数判断有误,现在已修正。







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息