Determine whether a year is a leap year (C language and python)

topic:

  • Whether each year between 2000 and 2500 is a normal year, and output the result:

Test analysis

  • 1. Can not four divisible, but can not be an integer 100 divided by the year is a leap year

  • 2 Leap years are divisible by four hundred

  • This kind of simple question is to examine the most basic algorithm ideas, that is, the idea of ​​solving the problem. It can be achieved with the most basic loop statements, but it is also necessary to know clearly how to write if else statements and where to nest them. Make it clear.

  • The most intuitive way is to use the concept of sets to clarify the ideas.
    Insert picture description here
    In this way, the writing of if else is clear and the process is clear. It will not take two minutes to start writing a few more lines of code.

C language problem solving

#include<stdio.h>

// 算法的步骤
int main()
{
    
    
	// 1.输入,接收 year年份的值
	int year = 2000; 

	while (year <= 2500)
	{
    
    
		// 2.判断是否能被四整除,不能整除,就不是闰年,跳到下次循环
		if(year%4 != 0)
		{
    
    
			printf("%d,非闰年\n",year);
			year ++;
			continue;
		}
		else
		{
    
     
			// 3. 能被四整除,不能被100整除,输出闰年  或  4.能被四百整除,输出闰年
			if(year%100 != 0 || year%400 == 0)
			{
    
    
				printf("%d,是闰年111111111111\n", year);
			}
			else 
			{
    
    
				// 5.确实不是闰年了,输出非闰年
				printf("%d,非闰年\n", year);	
			}
		}
		// 6.year++ 
		year++;
		// 7. 当循环year>2500时,结束循环,算法程序执行结束
	}
	return 0;
}

python problem solving

year = 2000
while year <= 2500:
    if year%4 != 0:
        print("%d,非闰年"% year)
    elif year%100 != 0 or year%400 == 0:
        print("%d, 闰年111111111111111111"% year)
    else:
        print("%d, 非闰年"% year)
    year += 1

Guess you like

Origin blog.csdn.net/pythonstrat/article/details/112539660