“输出下一秒”

绝对原创!!!!

写在前面的话:

昨天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;}

 

以下是测试结果。

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

 

JAVA版本代码实现:

package com.example.demo;

import java.text.SimpleDateFormat;
import java.util.Scanner;

public class demo {

	public static long date2Timestamp(String date, String dateFormat) {
		long milliSeconds = 0;
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
			milliSeconds = simpleDateFormat.parse(date).getTime();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return milliSeconds;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);
		String input = scanner.nextLine();

		String dateFormat = "yyyy/MM/dd HH:mm:ss";
		long nextMilliSeconds = date2Timestamp(input, dateFormat) + 1000;
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		System.out.println(sdf.format(nextMilliSeconds));
		
		return;
	}

}

C++使用时间和string类实现:

#include <iostream>
#include <string>
#include <time.h>


int main(int argc, char **argv)
{
	//char szInput[32] = { 0 };
	//std::cin.getline(szInput, 32);
	//std::cout << szInput << std::endl;

	std::string strInput = "";
	getline(std::cin, strInput);
	//std::cout << strInput << std::endl;

	try
	{
		struct tm stDate;
		int iRet = sscanf_s(strInput.c_str(), "%d/%d/%d %d:%d:%d"
			, &(stDate.tm_year)
			, &(stDate.tm_mon)
		    , &(stDate.tm_mday)
		    , &(stDate.tm_hour)
		    , &(stDate.tm_min)
		    , &(stDate.tm_sec));

		stDate.tm_year -= 1900;
		stDate.tm_mon -= 1;

		time_t stTime = mktime(&stDate);
		//printf("time: %lld\n", stTime);
		stTime += 1; //下一秒
		//printf("time: %lld\n", stTime);

		struct tm stNextDate;
		if (0 == localtime_s(&stNextDate, &stTime))
		{
			//输出下一秒
			printf("%04d/%02d/%02d %02d:%02d:%02d\n"
				, stNextDate.tm_year + 1900
				, stNextDate.tm_mon + 1
				, stNextDate.tm_mday
				, stNextDate.tm_hour
				, stNextDate.tm_min
				, stNextDate.tm_sec);
		}

		//当前时间
		/*time_t stNowTime = time(0);
		struct tm stNowDate;
		
		if (0 == localtime_s(&stNowDate, &stNowTime))
		{
			time_t stTmpTime = mktime(&stNowDate);

			printf("time: %lld, %lld, %04d/%02d/%02d %02d:%02d:%02d\n"
				, stNowTime
				, stTmpTime
				, stNowDate.tm_year + 1900
				, stNowDate.tm_mon + 1
			    , stNowDate.tm_mday
			    , stNowDate.tm_hour
			    , stNowDate.tm_min
			    , stNowDate.tm_sec);
		}*/
	}
	catch (const std::exception& e)
	{
		std::cout << e.what() << std::endl;
	}

	getchar();
	return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值