【问题标题】:Leap year in FreePascalFreePascal 中的闰年
【发布时间】:2018-06-18 19:47:03
【问题描述】:

输入 - 年份

输出 - 是否闰年

我试过了

Program LeapYear;

var  
    Year:Integer

begin

    writeln('Insert year');
    readln(Year)

    if Year MOD 4 = 0 and Year MOD 100 = 0 and not Year MOD 400 = 0 then
        begin
            writeln(Year,'is leap year')
        end
    else
        begin
            writeln(Year,'is not leap year')
        end

end.

但这不起作用

【问题讨论】:

  • Grmpf.你能把它以某种方式格式化并作为代码吗? (在代码块开头使用换行符,然后在 ech 代码行前面使用 4 个空格,然后正确缩进)

标签: pascal freepascal leap-year


【解决方案1】:

你的算法是错误的。应该是:

if (year mod 400 = 0) or ((year mod 4 = 0) and not (year mod 100 = 0))

【讨论】:

  • 当我运行程序时它给了我错误:不兼容的类型:得到“布尔”预期“LongInt”
  • 我添加了一些括号。再试一次?
【解决方案2】:

IsLeapYear函数在datih.inc文件中已经定义好了,所以你不需要自己编写版本,只需要添加sysutils单元。

【讨论】:

  • 但它被声明为单词,因此在很远的未来几年内它都不起作用。或负数年。
  • @BeniBela,您认为您是否需要确定 65536 之后的年份是否是闰年 :-) ? [+1]
  • @TLama: 是的,最迟在 63524 年......而且我需要知道负年,所以我实际上用整数重写了所有日期/时间函数(闰年很重要,如果您不知道闰年,则无法进行任何日期计算)
【解决方案3】:

希望对你有帮助:

if((a MOD 4) = 0) and ((a MOD 100) = 0) and ((a MOD 400) = 0) then y:=true 
else y:=false;

【讨论】:

  • 这个逻辑完全错误,不要用。
【解决方案4】:

Program LeapYear;

Uses crt;

var

Year:Integer;

 begin

clrscr;
  writeln('Insert year');
readln(Year);

if (Year MOD 4 = 0)then 
  writeln(Year,'is leap year');
If (Year Mod 4 >=1 ) Then 
  writeln(Year,'is not leap year')

结束。

【讨论】:

  • 不正确,例如 1900 和 2100 不是闰年。
猜你喜欢
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多