P9177 [COCI2022-2023#5] Kalendar题解

· · 题解

思路

首先,我们可以通过观察样例发现:

如何确保格式

  1. 用变量 d 储存输出到哪一天,依次递增,根据 d 是否满足 d<10 而且 d>0 来判断输出多少个 .

  2. 其次,用变量 w 储存输出到了这一行的第几个,如果 w=7,表示结尾,需换行了,输出边框 |,将 w 归零 。

  3. 如果 w=0,代表开始,输出边框 |

code


#include<bits/stdc++.h>
using namespace std;
int n,x,d=1,w;
int main(){
    cin>>n>>x;
    cout<<"+---------------------+"<<endl;//上边框
    if(x!=1){
        cout<<"|";//对1的特判,不然会输出两个边框
    }
    for(int i=1;i<x;i++){//开头遗失的日子
        cout<<"...";
        w++;
    }
    while(d<=n){//输出n天
        if(w==0){
            cout<<"|";//起始边框
        }
        if(d>0&&d<10) cout<<".."<<d;//判断一位还是两位
        else{
            cout<<"."<<d;
        }
        w++;//星期+1
        if(w==7){
            w=0;//下一周,归零
            cout<<"|"<<endl;//末尾边框
        }
        d++;

    }
    if(w!=0){//如果最后一行还没有结束

        while(w<7){
            cout<<"...";//输出遗失的日子
            w++;
        }
        cout<<"|"<<endl;//末尾边框
    }
    cout<<"+---------------------+";//下边框
    return 0;
}