列表

详情


CPP11. 判断季节

描述

键盘录入一个月份 month,判断属于哪个季节。(3 - 5 为春季、6 - 8 为夏季、9 - 11 月为秋季、12,1,2 月为冬季)

输入描述

输入任意一个月份整数,范围在 1 - 12

输出描述

输出对应月份的季节,3 - 5 月为春季、6 - 8 月为夏季、9 - 11 月为秋季、12,1,2 月为冬季。如果输入的月份不是 1 - 12,则输出“不合法”。

示例1

输入:

3

输出:

春季

示例2

输入:

7

输出:

夏季

示例3

输入:

10

输出:

秋季

示例4

输入:

12

输出:

冬季

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 400KB, 提交时间: 2021-12-05

#include <iostream>
using namespace std;

int main() {
    
    int month;
    cin >> month;

    // write your code here......
    if(month>0&&month<=12){
        if(month>=3&&month<=5){
            cout<<"春季";
        }else if(month>=6&&month<=8){
            cout<<"夏季";
        }else if(month>=9&&month<=11){
            cout<<"秋季";
        }else{
            cout<<"冬季";
        }
    }else{
        cout<<"不合法";
    }

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 400KB, 提交时间: 2021-11-29

#include <iostream>
using namespace std;

int main() {
    
    int month;
    cin >> month;

    // write your code here......
    switch (month) {
        case 12:
        case 1:
        case 2:
            cout << "冬季" << endl;
            break;
        case 3:
        case 4:
        case 5:
            cout << "春季" << endl;
            break;
        case 6:
        case 7:
        case 8:
            cout << "夏季" << endl;
            break;
        case 9:
        case 10:
        case 11:
            cout << "秋季" << endl;
            break;
        default:
            cout << "不合法" << endl;
    }

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 400KB, 提交时间: 2021-11-26

#include <iostream>
using namespace std;

int main() {
    
    int month;
    cin >> month;
    switch (month) {
        case 12:
        case 1:
        case 2:
            cout << "冬季" << endl;
            break;
        case 3:
        case 4:
        case 5:
            cout << "春季" << endl;
            break;
        case 6:
        case 7:
        case 8:
            cout << "夏季" << endl;
            break;
        case 9:
        case 10:
        case 11:
            cout << "秋季" << endl;
            break;
        default:
            cout << "不合法" << endl;
    }

    
    

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 412KB, 提交时间: 2021-10-16

#include <iostream>
using namespace std;

int main() {
    
    int month;
    cin >> month;

    // write your code here......
    if(month<1||month>12) cout<<"不合法";
    else 
    {switch(month)
    {
        case 3 ... 5:cout<<"春季"; break;
        case 6 ... 8:cout<<"夏季"; break;
        case 9 ... 11:cout<<"秋季"; break;
        default:cout<<"冬季";
    }
    }

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 416KB, 提交时间: 2021-11-29

#include <iostream>
using namespace std;

int main() {
    
    int month;
    cin >> month;

    if(month<=5&&month>=3)// write your code here......
    {
        cout<<"春季"<<endl;
    }
     else if(month<=8&&month>=6)
    {
        cout<<"夏季"<<endl;
    }
     else if(month<=11&&month>=9)
    {
        cout<<"秋季"<<endl;
    }    
  
      else if(month==12||month==1||month==2)
     {
        cout<<"冬季"<<endl;
     }
     else
     {
         cout<<"不合法"<<endl;
      }
    return 0;
}

上一题