C语言实验——某年某月的天数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

输入年和月,判断该月有几天?

Input

输入年和月,格式为年\月。

Output

输出该月的天数。

Sample Input

2009\1

Sample Output

31

Hint

注意判断闰年啊

这个题主要是因为输入的原因,因为是新手,不知道如何格式输入,所以用字符串读入,以后补上输入方式。

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		String s;
		char a;
		int y,m,i,b,d;
		s = cin.next();
		b = d = y = m = 0;
		for(i=0;i<s.length();i++)
		{
			a = s.charAt(i);
			if(a!='\\')
			{
				b *= 10;
				b += a-'0';
			}
			else
			{
				y = b;
				b = 0;
			}
		}
		m = b;
		switch(m)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: d = 31;break;
		case 4:
		case 6:
		case 9:
		case 11: d = 30;break;
		case 2:
		{
			if((y%4==0&&y%100!=0)||y%400==0)
				d = 29;
			else
				d = 28;
			break;
		}
		}
		System.out.println(d);
		cin.close();
	}
}

相关文章:

  • 2022-01-27
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
猜你喜欢
  • 2022-12-23
  • 2022-02-07
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案