打印日期

题目描述:

给出年分m和一年中的第n天,算出第n天是几月几号。

输入:

输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出:

可能有多组测试数据,对于每组数据,
按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

样例输入:
2000 3
2000 31
2000 40
2000 60
2000 61
2001 60
样例输出:
2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01
 1 #include <stdio.h>
 2 bool isRun(int x)
 3 {
 4     return (x%4==0&&x%100!=0)||x%400==0;
 5 }
 6 int main()
 7 {
 8     int i,y,n;
 9     int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
10 //    freopen("in.txt","r",stdin);
11     while(scanf("%d %d",&y,&n)!=EOF)
12     {
13         i=1;
14         month[2]=28;
15         if (isRun(y))
16             month[2]++;
17         while (n>month[i])
18         {
19             n-=month[i];
20             i++;
21         }
22         printf("%04d-%02d-%02d\n",y,i,n);
23     }
24     return 0;
25 }

注意格式 输出格式yyyy-mm-dd 四位数-二位数-二位数

/* printf example */
#include <stdio.h>

int main()
{
   printf ("Characters: %c %c \n", 'a', 65);
   printf ("Decimals: %d %ld\n", 1977, 650000L);
   printf ("Preceding with blanks: %10d \n", 1977);
   printf ("Preceding with zeros: %010d \n", 1977);
   printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
   printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
   printf ("Width trick: %*d \n", 5, 10);
   printf ("%s \n", "A string");
   return 0;
}

Output:

Characters: a A
Decimals: 1977 650000
Preceding with blanks:       1977
Preceding with zeros: 0000001977
Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick:    10
A string

  

posted @ 2013-02-21 18:53  丶Blank  阅读(200)  评论(0编辑  收藏  举报