打印一年的季节名称

原文:

给定月号 m,任务是根据月号打印一年的季节名称。 例:

input: m = 5
output: spring
input: m = 1
output: winter

进场:

  • 一年有四个主要季节,即夏、秋、冬、春。
  • 冬天的月份是在十二月,一月和二月。
  • 三月、四月和五月是春天。
  • 六月、七月和八月的夏季。
  • 九月、十月和十一月是秋天。
  • 所以把月份分别映射到特定的季节并打印出来。

以下是上述方法的实现:

c

// c   program to print the season
// name based on the month number
#include 
using namespace std;
void findseason(int m)
{
    // checks out the season according
    // to the month number entered by the user
    switch (m)
    {
        case 12:
        case 1:
        case 2:
            cout << ("\nwinter");
            break;
        case 3:
        case 4:
        case 5:
            cout << ("\nspring");
            break;
        case 6:
        case 7:
        case 8:
            cout << ("\nsummer");
            break;
        case 9:
        case 10:
        case 11:
            cout << ("\nautumn");
            break;
        default:
            // handles the condition if number entered
            // is not among the valid 12 months
            cout << ("\ninvalid month number");
            break;
    }
}
// driver code
int main()
{
    int m = 5;
    cout << "for month number: " << m;
    findseason(m);
    m = 10;
    cout << "\nfor month number: " << m;
    findseason(m);
    return 0;
}
// this code is contributed by rajput-ji

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the season
// name based on the month number
import java.util.*;
public class seasons {
    public static void findseason(int m)
    {
        // checks out the season according
        // to the month number entered by the user
        switch (m) {
        case 12:
        case 1:
        case 2:
            system.out.println("winter");
            break;
        case 3:
        case 4:
        case 5:
            system.out.println("spring");
            break;
        case 6:
        case 7:
        case 8:
            system.out.println("summer");
            break;
        case 9:
        case 10:
        case 11:
            system.out.println("autumn");
            break;
        default:
            // handles the condition if number entered
            // is not among the valid 12 months
            system.out.println("invalid month number");
            break;
        }
    }
    // driver code
    public static void main(string abc[])
    {
        int m = 5;
        system.out.println("for month number: "
                             m);
        findseason(m);
        m = 10;
        system.out.println("for month number: "
                             m);
        findseason(m);
    }
}

python 3

# python3 program to print the season
# name based on the month number
def findseason (m) :
    # taken all the possible
    # month numbers in the list.
    list1 = [[12 , 1 , 2], [3 , 4 , 5],
             [6 , 7 , 8], [9 , 10 , 11]]
    # matching the month number
    # with the above list entries
    if m in list1[0] :
        print ( "winter" )
    elif m in list1[1] :
        print ( "spring" )
    elif m in list1[2] :
        print ( "summer" )
    elif m in list1[3] :
        print ( "autumn" )
    else :
        print ( "invalid month number" )
# driver code
m = 5
print("for month number:", m);
findseason ( m )
m = 10
print("for month number:", m);
findseason ( m )
# this code is contributed by abhishek

c

// c# program to print the season
// name based on the month number
using system;
class gfg
{
public static void findseason(int m)
{
    // checks out the season according
    // to the month number entered by the user
    switch (m)
    {
        case 12:
        case 1:
        case 2:
            console.writeline("winter");
            break;
        case 3:
        case 4:
        case 5:
            console.writeline("spring");
            break;
        case 6:
        case 7:
        case 8:
            console.writeline("summer");
            break;
        case 9:
        case 10:
        case 11:
            console.writeline("autumn");
            break;
        default:
            // handles the condition if number entered
            // is not among the valid 12 months
            console.writeline("invalid month number");
            break;
    }
}
// driver code
public static void main()
{
    int m = 5;
    console.writeline("for month number: "   m);
    findseason(m);
    m = 10;
    console.writeline("for month number: "   m);
    findseason(m);
}
}
// this code is contributed by ankitrai01

java 描述语言


output: 

for month number: 5
spring
for month number: 10
autumn

时间复杂度: o(1)

辅助空间: o(1)