SGU

题目链接:https://vjudge.net/contest/239445#problem/H

题目大意:输入n,k,有n*n* n*n的网格,要求每行每列刚好有k个*,每n*n的小方格内也刚好有k个*。

思路:不是自己写出来的,大概思路就是从第一排开始放,放满k个,然后跳到下一行,在这行的基础上加上n的位置开始放,放满k个,····一直下去,当到了第i行,并且i%n==1时,从上一个i%n==1的位置的下一个位置开始放,思路就是这样了

看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=4e2+10;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
int main()
{
    int n,k,ac=1,flag=1;
    int a[maxn][maxn];
    memset(a,0,sizeof(a));
    cin>>n>>k;
    for(int i=1;i<=n*n;i++)
    {
        if(i>1&&i%n==1) {flag++;ac=flag;}//当到了i%n==1时,从上一个i%n==1的位置的下一个开始放,当然i=1除外
            for(int l=ac;l<ac+k;l++)
            {
                int z=l;
                if(z>(n*n)) z=z%(n*n);
                a[i][z]=1;
            }
            ac=ac+n;//下一行跳n个放
            if(ac>n*n) ac=ac%(n*n);

    }
    for(int i=1;i<=n*n;i++)
    {
        for(int j=1;j<=n*n;j++)
        {
            if(a[i][j]) cout<<"*";
            else cout<<".";
        }
        cout<<endl;
    }
    return 0;
}

下面是题目:

The Berland is in trouble again. After the recent elections All-Berland Great Duma has divided into two coalitions: Blue-eyed coalition and Red-eyed coalition. On the independence day the following question was raised: what will the national flag look like? Both coalitions agreed that the flag should be a square of N 2 x N 2 cells, each of them painted blue of red. To make the flag acceptable for both coalitions, the following requirements must be held:

  • every row of the flag must contain exactly K blue cells
  • every column of the flag must contain exactly K blue cells
  • if we split the flag into squares of N x N cells (there will be N x N such squares), then every such square must contain exactly K blue cells. You are the chief programmers in Berland. So, making the flag model is your duty.
    Input
    First line of the input contains two integer numbers N and K (1 ≤ N ≤ 20, 0 ≤ KN2).
    Output
    Output description of the desired flag. Print N2 lines which consist of N2 characters
    '*'
    and
    '.'
    , where
    '*'
    means blue cell and
    '.'
    means red cell. Output
    "NO SOLUTION"
    (without quotes), if there is no flag which satisfies both coalitions. If there are several solutions, output any of them.
    Example(s)
    sample input
    sample output
    2 2
    
    *..*
    .**.
    .**.
    *..*
    

    sample input
    sample output
    3 1
    
    *........
    ...*.....
    ......*..
    .*.......
    ....*....
    .......*.
    ..*......
    .....*...
    ........*
    
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/9394606.html