Problem description
Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.
A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on.
Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#').
Consider sample tests in order to understand the snake pattern.
Input
The only line contains two integers: n and m (3 ≤ n, m ≤ 50).
n is an odd number.
Output
Output n lines. Each line should contain a string consisting of m characters. Do not output spaces.
Examples
Input
3 3
Output
###
..#
###
Input
3 4
Output
####
...#
####
Input
5 3
Output
###
..#
###
#..
###
Input
9 9
Output
#########
........#
#########
#........
#########
........#
#########
#........
#########
解题思路:按规律输出,当i为奇数时,输出的一行全部为'#';当i为偶数时,用flag做开关,交换输出两种格式,水过!
AC代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define FOR(a,b,c) for(int i=a;i<=b;++i)printf("%c",c)
4 int n,m;bool flag;
5 int main(){
6 cin>>n>>m;flag=false;
7 for(int i=1;i<=n;++i){
8 if(i%2)FOR(1,m,'#');
9 else{
10 if(!flag){FOR(1,m-1,'.');cout<<'#';flag=true;}
11 else{cout<<'#';FOR(2,m,'.');flag=false;}
12 }
13 cout<<endl;
14 }
15 return 0;
16 }
来源:oschina
链接:https://my.oschina.net/u/4356358/blog/3935185