小新爷爷的电视节目表 - 找出第 N 天的频道

小新爷爷喜欢看电视,他每天只看固定的频道。第一天会看 CCTV1,第二天和第三天会看 CCTV2,接下来三天会看 CCTV3,然后第 7 天又看 CCTV1,接下来两天看 CCTV2……如此循环下去。

现在请你判断小新爷爷在第 n 天会看哪个频道。

输入

一个整数 n 。

输出

输出一个字符串,表示小新爷爷在第 n 天会看的频道。

C++ 代码示例

#include <iostream>
using namespace std;

string getChannel(int n) {
    int cycle = n % 7;
    if (cycle == 1) {
        return "CCTV1";
    } else if (cycle == 2 || cycle == 3) {
        return "CCTV2";
    } else if (cycle == 4 || cycle == 5 || cycle == 6) {
        return "CCTV3";
    } else {
        return "CCTV1";
    }
}

int main() {
    int n;
    cin >> n;
    
    string channel = getChannel(n);
    cout << channel << endl;
    
    return 0;
}

原文地址: https://www.cveoy.top/t/topic/phXn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录