C#解密出生日期
时间: 1ms 内存:128M
描述:
使用C#编写一个静态方法。该方法能够根据出生日期,(1)计算此人的年龄;(2)计算从现在到其60周岁期间,总共多少天。
输入:
一个人的出生日期;
输出:
此人的年龄;此人从现在到其60周岁期间,总共多少天。
示例输入:
2019-12-4
示例输出:
0
21914
提示:
参考答案(内存最优[4464]):
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace Program0
{
class Program
{
static void Main(string[] args)
{
string[] s = Console.ReadLine().Split('-');
int a = int.Parse(s[0]);
int b = int.Parse(s[1]);
int c = int.Parse(s[2]);
DateTime dt1 = new DateTime(a, b, c);
int age = DateTime.Now.Year - dt1.Year;
Console.WriteLine("{0}", age);
a = a + 60;
DateTime dt2 = new DateTime(a, b, c);
TimeSpan ts = dt2 - DateTime.Now;
Console.WriteLine("{0,5:F0}", ts.TotalDays-0 + 3);
Console.ReadKey();
}
}
}
参考答案(时间最优[28]):
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
string[] s = Console.ReadLine().Split('-');
int a = int.Parse(s[0]);
int b = int.Parse(s[1]);
int c = int.Parse(s[2]);
DateTime dt1 = new DateTime(a, b, c);
int age = DateTime.Now.Year - dt1.Year;
Console.WriteLine("{0}", age);
a = a + 60;
DateTime dt2 = new DateTime(a, b, c);
TimeSpan ts = dt2 - DateTime.Now;
Console.WriteLine("{0,5:F0}", ts.TotalDays-0 + 4);
Console.ReadKey();
}
}
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。