洛谷P1319 压缩技术

题目链接

代码:

#include<bits/stdc++.h>

using namespace std;
#define MAXN 220
int arr[MAXN][MAXN];
int n;
int x,y;
void Fill(int num,int k)
{
    int i,j;//cout<<x<<" "<<y<<" "<<k<<endl;
    j = y;
    for( i = x; i <= n; i++)
    {
        for( ; j <= n; j++)
        {
            k--;
            arr[i][j] = num;

            if(k == 0)
            {
                break;
            }
        }

        if(k==0)
        {
            break;
        }
        j = 1;
    }
    if(j < n)
    {
        x = i;
        y = j + 1;
    }
    else if(j == n)
    {
        y = 1;
        x = i + 1;
    }

    return ;
}
void outPut()
{
    for(int i=1; i<=n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            cout<<arr[i][j];
        }
        cout<<endl;
    }
    return ;
}
int main()
{
    cin>>n;
    int k;
    int cnt = 0;
    x = 1;
    y = 1;
    while(scanf("%d",&k)!=EOF)
    {
        cnt++;
        if(k==0)
            continue;
        if(cnt%2 == 1)
        {
     //       cout<<1<<endl;
            Fill(0,k);
        }else
        {
      //      cout<<0<<endl;
            Fill(1,k);
        }

    }
    outPut();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38851184/article/details/108728800