Saturday, October 30, 2010

poj_3173_Parkside_s_Triangle.cpp

Problem Links:


poj3173,

Problem:


Parkside's Triangle















Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 5634Accepted: 3367


Description
Bessie taught the cows to make Parkside's Triangle. It is generated from two numbers: the size and the seed. The size N (1 <= N <= 20) determines how many rows are in the triangle and the seed S (1 <= S <= 9) determines the first number in the triangle. Here are two examples:
 N=5, S=3                  N=6, S=1  



3 4 6 9 4 1 2 4 7 2 7

5 7 1 5 3 5 8 3 8

8 2 6 6 9 4 9

3 7 1 5 1

8 6 2

3

The first line of any triangle has no blanks at its front.

Analyze the above examples, discover the rule, and write a program that will generate Parkside's Triangle given any size N (1 <= N <= 20) and any seed S (1 <= S <= 9).

Input
Line 1: Two space-separated integers: N and S

Output
Lines 1..N: Parkside's Triangle as above; no trailing blanks on any line.

Sample Input
5 3

Sample Output
3 4 6 9 4
5 7 1 5
8 2 6
3 7
8

Source
USACO 2006 December Bronze

Solution:


Easy Math problem,
Take care of the space between numbers.

Source Code:


[sourcecode language="cpp" collapse="true" padlinenumbers="true"]
//Sat Apr 24 01:39:44 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, const char* argv[])
{
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
int N, S;
while (cin >> N >> S)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int a = ((j + 1) * j / 2 + i + S) % 9;
if (a == 0)
a = 9;
if (i > j)
cout << " ";
else if (j < N - 1)
cout << a << " ";
else
cout << a << endl;
}
}
}
fclose(stdin);
fclose(stdout);
return 0;
}
[/sourcecode]

No comments :