2013年8月16日 星期五

488 - Triangle Wave

In this problem you are to generate a triangular wave form according to a specified pair of Amplitude and Frequency.

Input and Output

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Each input set will contain two integers, each on a separate line. The first integer is the Amplitude; the second integer is the Frequency.
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
For the output of your program, you will be printing wave forms each separated by a blank line. The total number of wave forms equals the Frequency, and the horizontal ``height'' of each wave equals the Amplitude. The Amplitude will never be greater than nine.
The waveform itself should be filled with integers on each line which indicate the ``height'' of that line.
NOTE: There is a blank line after each separate waveform, excluding the last one.

Sample Input


1

3
2

Sample Output


1
22
333
22
1

1
22
333
22
1
 

出處: UVa Online Judge - Triangle Wave


問題敘述

題目會先輸入共有幾組測試資料,接著輸入每組測試資料的內容。各組測試資料有兩個整數,第一個整數是振幅,第二個整數是頻率,此題要求您依照振幅和頻率印出波形。

解題思路

沒有什麼思路,只要印出和振幅一樣高的波並且重複印出題目給的頻率次即可。

c++ 程式碼

#include <iostream>
using namespace std;

int main() {
    int n;
    bool isFirst = true;

    cin >> n;
    for (int i=0; i<n; i++) {
        int amp, fre;

        cin.get();
        cin >> amp >> fre;
        for (int j=0; j<fre; j++) {
            bool reverse = false;

            if (isFirst)
                isFirst = false;
            else
                cout << endl;
            for (int k=1; k>=1; (reverse)? k--:k++) {
                for (int m=1; m<=k; m++)
                    cout << k;
                cout << endl;
                if (k == amp)
                    reverse = true;
            }
        }
    }

    return 0;
}

沒有留言:

張貼留言