CF#259 (Div. 2) A.

来源:互联网 发布:性知识软件哪些好 编辑:程序博客网 时间:2024/05/18 02:06
Little Pony and Crystal Mine
time limit per test
1 second
memory limit per test
256 megabytes
题目链接:http://codeforces.com/contest/454/problem/A

Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n × n matrix with a diamond inscribed into it.

You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character "D". All other cells of the matrix should be represented by character "*". Look at the examples to understand what you need to draw.
Input

The only line contains an integer n (3 ≤ n ≤ 101; n is odd).
Output

Output a crystal of size n.
Sample test(s)
Input


3

Output

*D*
DDD
*D*

Input

5

Output

**D**
*DDD*
DDDDD
*DDD*
**D**

Input

7

Output

***D***
**DDD**
*DDDDD*
DDDDDDD
*DDDDD*
**DDD**

***D***




解题思路:

        题意没细看,直接看样例了。样例给的很清楚,输入一个n,然后照那样输出就行。自从回了预处理之后,就欲罢不能,

这题也是用了下预处理。

         先把二维矩阵g的元素都初始化为D,然后n行遍历。遍历分两个阶段,即 * 的数量增长阶段和 * 数量下降阶段。以减少

为例,经观察它最多可以减少到n / 2行。对于每行来说,分别对头和尾两次遍历。注意当k减少到0,开始增加回n / 2的阶段做

下标记,如果最后k增加到tmp + 1,则跳出循环······很好想,看代码吧





完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#define REP(i, n) for (int i=0;i<n;++i)#define FOR(i, a, b) for (int i=a;i<b;++i)#define DWN(i, b, a) for (int i=b-1;i>=a;--i)#define REP_1(i, n) for (LL i=1;i<=n;++i)#define FOR_1(i, a, b) for (int i=a;i<=b;++i)#define DWN_1(i, b, a) for (LL i=b;i>=a;--i)#define REP_C(i, n) for (int n____=n,i=0;i<n____;++i)#define FOR_C(i, a, b) for (int b____=b,i=a;i<b____;++i)#define DWN_C(i, b, a) for (int a____=a,i=b-1;i>=a____;--i)#define REP_N(i, n) for (i=0;i<n;++i)#define FOR_N(i, a, b) for (i=a;i<b;++i)#define DWN_N(i, b, a) for (i=b-1;i>=a;--i)#define REP_1_C(i, n) for (int n____=n,i=1;i<=n____;++i)#define FOR_1_C(i, a, b) for (int b____=b,i=a;i<=b____;++i)#define DWN_1_C(i, b, a) for (int a____=a,i=b;i>=a____;--i)#define REP_1_N(i, n) for (i=1;i<=n;++i)#define FOR_1_N(i, a, b) for (i=a;i<=b;++i)#define DWN_1_N(i, b, a) for (i=b;i>=a;--i)#define REP_C_N(i, n) for (int n____=(i=0,n);i<n____;++i)#define FOR_C_N(i, a, b) for (int b____=(i=0,b);i<b____;++i)#define DWN_C_N(i, b, a) for (int a____=(i=b-1,a);i>=a____;--i)#define REP_1_C_N(i, n) for (int n____=(i=1,n);i<=n____;++i)#define FOR_1_C_N(i, a, b) for (int b____=(i=a,b);i<=b____;++i)#define DWN_1_C_N(i, b, a) for (int a____=(i=b,a);i>=a____;--i)#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)#define REP_S(i, str) for (char*i=str;*i;++i)#define REP_L(i, hd, suc) for (int i=hd;i;i=suc[i])#define REP_G(i, u) REP_L(i,hd[u],suc)#define REP_SS(x, s) for (int x=s;x;x=(x-1)&s)#define DO(n) for ( int ____n = n; ____n-->0; )#define REP_2(i, j, n, m) REP(i, n) REP(j, m)#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)#define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)#define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)#define ALL(A) A.begin(), A.end()#define LLA(A) A.rbegin(), A.rend()#define CPY(A, B) memcpy(A, B, sizeof(A))#define INS(A, P, B) A.insert(A.begin() + P, B)#define ERS(A, P) A.erase(A.begin() + P)#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())#define UBD(A, x) (lower_bound(ALL(A), x) - A.begin())#define CTN(T, x) (T.find(x) != T.end())#define SZ(A) int((A).size())typedef long long LL;//typedef long double DB;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;char g[1111][1111];int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    int n;    while(~scanf("%d",&n))    {        for(int i = 1 ; i <= n ; i ++)        {            for(int j = 1 ; j <= n ; j ++)            {                g[i][j] = 'D';            }        }        int k = n / 2 ;        int tmp = k;        int flag = 0;        int next = 0;        for(int i = 1 ; i <= n ; i ++ )        {            if(next && k == tmp + 1)                break;            if(k == 0)            {                flag = 1;                next = 1;                k++;                continue;            }            if(flag == 1)            {                for(int j = 1 ; j <= k ; j ++)                    g[i][j] = '*';                for(int j = n ; j > n - k ; j --)                    g[i][j] = '*';                k ++;            }            else if(flag == 0)            {                for(int j = 1 ; j <= k ; j ++)                    g[i][j] = '*';                for(int j = n ; j > n - k ; j --)                    g[i][j] = '*';                k -- ;            }        }        for(int i = 1 ; i <= n ; i ++)        {            for(int j = 1 ; j <= n ; j ++)            {                cout << g[i][j];            }            cout << endl;        }    }}


0 0
原创粉丝点击