1313. Some words about sporttime limit: 0.5 second
Memory limit: 16 MB
Ural doctors worry about the health of their youth very much. Special Investors showed that a lot of clever students instead of playing football, skating
Or bicycling had particle ipated in something like programming Olympus. Moreover, they call it sports programming! To sit near the monitor and think during 5 hours a day-is it a sport? To do it two times per year during the contests-it is more or less normal,
But during the preparations to the nearest contest they spend several hours a week sitting at their computers! It wocould be possible to understand if they were some blockheads and dunces, but they are ones of the best students all over the world! To save students from the harmful habit to sit at the computer for hours, Ural doctors has specified Ted a fundamentally new monitor with diagonal trace of a beam
In its electron-beam tube. soon the winners of Ural programming championship wocould be awarded with such monitors. in the specially designed square monitor the electronic beam wocould scan the screen not horizontally but diagonally. the difference of the lengths
Of different diagonals causes such effects as non-uniform brightness, flashing and non-linear distortions. the terrible properties of such monitors wocould break of the habit of looking at the monitor for hours. there is a little problem: the majority of Computer
Video cards generates the normal "rectangle" signal for monitor. so it is necessary to develop special adapter-program, which shoshould transform the usual "rectangle" signal to the signal necessary for this kind of monitors. program shoshould be fast and reliable.
That's why the development of this program is entrusted to the participant list of the Ural championship for sports programming. inputthe first input line contains the single integer N (1 ≤ n ≤ 100)-the number of pixels on the side of New Square monitor. it is followed by n lines, each containing
N positive integers not exceeding 100 divided by spaces. it is the image outputting by the usual video card (as you can see the color depth of new monitor is not so large-Anyway usual programmer does not need more than 100 colors ). outputyou are to write the program that outputs the sequence for input into the new monitor. pixels are numbered from the upper-left corner of the screen diagonally
From left ot right and bottom-up. There is no need to explain details-look at the sample and you'll understand everything. Sample
Input |
Output |
41 3 6 102 5 9 134 8 12 157 11 14 16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Problem Author:Idea-Stanislav Vasilyev, prepared by Stanislav Vasilyev and Alexander klepinin
Problem Source:VIII collegiate students Urals programming contest. Yekaterinburg, March 11-16,200 4
Solution:Input: enter an array of n × n. Enter N in the first line, and then enter an array. Output: output an array of diagonal lines from bottom left to top right. Idea: first, output the upper triangle matrix, starting line Variable N: 0 ~ N-1, line variable I: n ~ Column 0 variable J: 0 ~ N and then output the lower Triangle Matrix, starting column Variable N: 1 ~ N-1 line variable I: N-1 ~ N column variable J: n ~ N-1
The Code is as follows:
// Problem 1313 # include <iostream> using namespace STD; int main () {int N; CIN> N; int ** A = new int * [N]; // create a dynamic two-dimensional array for (INT I = 0; I <n; ++ I) A [I] = new int [N]; for (INT I = 0; I <n; ++ I) for (Int J = 0; j <n; ++ J) CIN> A [I] [J]; for (INT n = 0; n <n; ++ N) // output algorithm {for (INT I = N, j = 0; I> = 0, j <= N; -- I, ++ J) cout <A [I] [J] <'';} For (INT n = 1; n <N; + + n) {for (INT I = N-1, j = N; I> = N, j <n; -- I, ++ J) cout <A [I] [J] <'';} system (" pause "); Return 0 ;}
Harvest: create a dynamic two-dimensional array.
Reference: [1]: http://acm.timus.ru/problem.aspx? Space = 1 & num = 1313 [2]: bytes. For example, the matrix of 5*5 is 0, 1, 5, 6, and 14.
2 4 7 13 15
3 8 12 16 21
9 11 17 20 22
10 18 19 23 24 Method 1: process the upper left corner and lower right corner respectively
#include <iostream>using namespace std;int main(){int N;cout<<"input the row number of the matrix:"<<endl;cin>>N;int **a=new int*[N];for(int i=0;i<N;++i)*(a+i)=new int[N]; // a[i]=new int[N];/*for(int i=0;i<N;++i)for(int j=0;j<N;++j)a[i][j]=0;*/bool flag=1;int num=0;for(int n=0;n<N;++n){ //left halfif(flag){for(int i=n,j=0;j<=n;--i,++j){a[i][j]=num;++num;}flag=0;}else{for(int i=0,j=n;i<=n;++i,--j){a[i][j]=num;++num;}flag=1;}}for(int n=1;n<N;++n){ //right halfif(flag){for(int i=N-1,j=n;j<=N-1;--i,++j){a[i][j]=num;++num;}flag=0;}else{for(int i=n,j=N-1;i<=N-1;++i,--j){a[i][j]=num;++num;}flag=1;}}for(int i=0;i<N;++i){for(int j=0;j<N;++j)cout<<a[i][j]<<'\t';cout<<endl;}return 0;}
Method 2: Observe the characteristics of the matrix:
int main(){int N;cout<<"input the row number of the matrix:"<<endl;cin>>N;int **a=new int*[N];for(int i=0;i<N;++i)*(a+i)=new int[N]; // a[i]=new int[N];int s,squa=N*N;for(int i=0;i<N;++i)for(int j=0;j<N;++j){s=i+j;if(s<N)a[i][j]=s*(s+1)/2+(((i+j)%2==0)?i:j);else{s=(N-1-i)+(N-1-j);a[i][j]=squa-(s*(s+1)/2+(((i+j)%2==0)?i:j));}}for(int i=0;i<N;++i){for(int j=0;j<N;++j)cout<<a[i][j]<<'\t';cout<<endl;}return 0;}