LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1981|回复: 5

看闰年的C程序

[复制链接]
发表于 2004-4-25 22:38:24 | 显示全部楼层 |阅读模式
源代码:

  1. [linuxer@mydesktop bmp]$ cat year.c
  2. #include "stdio.h"
  3. main()
  4. {int year;
  5. printf("\n1900-2100的闰年有:\n\n");
  6. for(year=1900;year<=2100;year++)
  7. if((year%4==0&&year%100!=0)||(year%400==0))printf("%d,",year);
  8. printf("\n\n1900-2100的平年有:\n\n");
  9. for(year=1900;year<=2100;year++)
  10. if(!((year%4==0&&year%100!=0)||(year%400==0)))printf("%d,",year);
  11. }
  12. [linuxer@mydesktop bmp]$
复制代码

编译与运行:

  1. [linuxer@mydesktop bmp]$ gcc -o year.o year.c
  2. [linuxer@mydesktop bmp]$ ./year.o

  3. 1900-2100的闰年有:

  4. 1904,1908,1912,1916,1920,1924,1928,1932,1936,1940,1944,1948,1952,
  5. 1956,1960,1964,1968,1972,1976,1980,1984,1988,1992,1996,2000,2004,
  6. 2008,2012,2016,2020,2024,2028,2032,2036,2040,2044,2048,2052,2056,
  7. 2060,2064,2068,2072,2076,2080,2084,2088,2092,2096,

  8. 1900-2100的平年有:

  9. 1900,1901,1902,1903,1905,1906,1907,1909,1910,1911,1913,1914,1915,
  10. 1917,1918,1919,1921,1922,1923,1925,1926,1927,1929,1930,1931,1933,
  11. 1934,1935,1937,1938,1939,1941,1942,1943,1945,1946,1947,1949,1950,
  12. 1951,1953,1954,1955,1957,1958,1959,1961,1962,1963,1965,1966,1967,
  13. 1969,1970,1971,1973,1974,1975,1977,1978,1979,1981,1982,1983,1985,
  14. 1986,1987,1989,1990,1991,1993,1994,1995,1997,1998,1999,2001,2002,
  15. 2003,2005,2006,2007,2009,2010,2011,2013,2014,2015,2017,2018,2019,
  16. 2021,2022,2023,2025,2026,2027,2029,2030,2031,2033,2034,2035,2037,
  17. 2038,2039,2041,2042,2043,2045,2046,2047,2049,2050,2051,2053,2054,
  18. 2055,2057,2058,2059,2061,2062,2063,2065,2066,2067,2069,2070,2071,
  19. 2073,2074,2075,2077,2078,2079,2081,2082,2083,2085,2086,2087,2089,
  20. 2090,2091,2093,2094,2095,2097,2098,2099,2100,
  21. [linuxer@mydesktop bmp]$
复制代码
发表于 2004-4-27 12:04:31 | 显示全部楼层
goooood!!
发表于 2004-4-27 13:40:49 | 显示全部楼层
最初由 Linux_Lyb 发表
goooood!!

灌水嫌疑
发表于 2004-4-27 13:54:34 | 显示全部楼层
有谁说说闰年的知识阿。。。。我看了网上说能整除四的一年就时闰年。。。但看大家的概念都不同呢:confused: :confused:
发表于 2004-4-27 14:28:50 | 显示全部楼层
最初由 devel 发表
有谁说说闰年的知识阿。。。。我看了网上说能整除四的一年就时闰年。。。但看大家的概念都不同呢:confused: :confused:

现行公历历法400年中有97个闰年,这样算下去,3000多年会有一天的误差。
97个闰年是这样分布的:

  1. if((year % 4 == 0 && year % 100 != 0)
  2.     || (year % 400 == 0))
  3. {
  4.           /* leap year */
  5. }
  6. else
  7. {
  8.          /*non-leap year*/
  9. }
复制代码

这样的话,每400年中前三个能被100整除的年份,都不是闰年,所以闰年的个数刚好是100-3 = 97。
 楼主| 发表于 2004-5-8 23:12:27 | 显示全部楼层
贴个比较完整的,可以接受输入任何年份:

  1. /* 显示闰年、平年的C代码
  2. // 接受两个变量a,b作为起止年份。
  3. //gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
  4. //    version 0.10
  5. //     2004-05-07
  6. // By seablue at linuxsir.cn
  7. */

  8. #include <stdio.h>
  9. main()
  10. {int a,b,i,k,t;
  11. printf("Please input year of beginning and year of end:\n");
  12. printf("Example: 1900 2000\n");

  13. /*输入起止年份,并让a<b
  14. */
  15. scanf("%d %d",&a,&b);
  16. if(a>b){t=a;a=b;b=t;}

  17. /*输出闰年及其总数
  18. */
  19. printf("\nLeapyear of %d-%d:\n",a,b);
  20. for(i=a,k=0;i<=b;i++)
  21. if((i%4==0&&i%100!=0)||(i%400==0)){k++;printf("%d,",i);}
  22. printf("\b.\nTotall: %d\n\nNon-leapyear of %d-%d:\n",k,a,b);

  23. /*输出平年及其总数
  24. */
  25. for(i=a,k=0;i<=b;i++)
  26. if(!((i%4==0&&i%100!=0)||(i%400==0))){k++;printf("%d,",i);}
  27. printf("\b.\nTotall: %d\n",k);
  28. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表