3.24题目 3题chessboard goto

题目描述

Draw a rectangle which has a height of H cm and a width of W cm. Draw a 1-cm square by single '#'.

 

输入

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

 

The input ends with two 0 (when both H and W are zero). 

 

输出

For each dataset, print the rectangle made of H × W '#'.

 

Print a blank line after each dataset.

 

样例输入 Copy

3 4
5 6
2 2
0 0

样例输出 Copy

####
####
####

######
######
######
######
######

##
##

提示

  • 1 ≤ H ≤ 300
  • 1 ≤ W ≤ 300

 

#include<iostream>
using namespace std;
int main()
{
    int H, W;
    cin >> H >> W;
    while (H != 0||W!=0)
    {
        int i, j;
        for (i = 0; i < H; i++) {
            for (j = 0; j < W; j++) {
                cout << '#';
            }
            cout << endl;
        }
        cout << endl;
        cin >> H >> W;
    }
    return 0;
}

 

题目描述

Draw a frame which has a height of H cm and a width of W cm. For example, the following figure shows a frame which has a height of 6 cm and a width of 10 cm.

 
##########
#........#
#........#
#........#
#........#
##########
 

输入

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

 

The input ends with two 0 (when both H and W are zero). 

 

输出

For each dataset, print the frame made of '#' and '.'.

 

Print a blank line after each dataset.

 

样例输入 Copy

3 4
5 6
3 3
0 0

样例输出 Copy

####
#..#
####

######
#....#
#....#
#....#
######

###
#.#
###

提示

  • 3 ≤ H ≤ 300
  • 3 ≤ W ≤ 300
#include<iostream>
using namespace std;
int main()
{
    int H, W;
    cin >> H >> W;
    while (H != 0|| W!=0)
    {
        int i, j;
        for (j = 0; j < W; j++) {
            cout << '#';
        }
        cout << endl;

        for (i = 2; i < H; i++) {
            cout << '#';
            for (j = 2; j < W; j++) {
                cout << '.';
            }
            cout << '#'<<endl;
        }

        for (j = 0; j < W; j++) {
            cout << '#';
        }
        cout << endl << endl;

        cin >> H >> W;
    }
    return 0;
}

 

题目描述

Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm.

 
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#



Note that the top left corner should be drawn by '#'.

输入

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

 

The input ends with two 0 (when both H and W are zero). 

 

输出

For each dataset, print the chessboard made of '#' and '.'.

 

Print a blank line after each dataset.

 

样例输入 Copy

3 4
5 6
3 3
2 2
1 1
0 0

样例输出 Copy

#.#.
.#.#
#.#.

#.#.#.
.#.#.#
#.#.#.
.#.#.#
#.#.#.

#.#
.#.
#.#

#.
.#

#

提示

  • 1 ≤ H ≤ 300
  • 1 ≤ W ≤ 300
#include<iostream>
using namespace std;
int main()
{
    int H, W;
    int i, j;
    int m;
    
    while (cin >> H >> W)
    {
        if (H == 0 && W == 0) { break; }
        for(i=0;i<H;i++){
            for (j = 0; j < W; j++) {
                m = i + j;
                switch (m % 2) {
                case 0: cout << '#'; break;
                case 1: cout << '.'; break;
                }
            }
            cout << endl;
        }
        cout << endl;
        
    }
    return 0;
    }

题目描述

In programming languages like C/C++, a goto statement provides an unconditional jump from the "goto" to a labeled statement. For example, a statement "goto CHECK_NUM;" is executed, control of the program jumps to CHECK_NUM. Using these constructs, you can implement, for example, loops.

 

Note that use of goto statement is highly discouraged, because it is difficult to trace the control flow of a program which includes goto.

 

Write a program which does precisely the same thing as the following program (this example is wrtten in C++). Let's try to write the program without goto statements.


void call(int n){
  int i = 1;
 CHECK_NUM:
  int x = i;
  if ( x % 3 == 0 ){
    cout << " " << i;
    goto END_CHECK_NUM;
  }
 INCLUDE3:
  if ( x % 10 == 3 ){
    cout << " " << i;
    goto END_CHECK_NUM;
  }
  x /= 10;
  if ( x ) goto INCLUDE3;
 END_CHECK_NUM:
  if ( ++i <= n ) goto CHECK_NUM;

  cout << endl;
}
 

输入

An integer n is given in a line.

 

输出

Print the output result of the above program for given integer n.

 

样例输入 Copy

30

样例输出 Copy

 3 6 9 12 13 15 18 21 23 24 27 30

提示

  • 3 ≤ n ≤ 10000
#include<iostream>
using namespace std;
int main()
{
    int i, n;
    cin >> n;
    for (i = 1; i <= n; i++) {
        switch (i %10) {
        case 3: cout << " " << i; break;
        default:if (i % 3 == 0) { cout << " " << i; }
                else break;
        }
        
    }
    return 0;
    }

 

posted @ 2020-03-24 16:57  冥古宙  阅读(414)  评论(1)    收藏  举报