[timus] 1313. Some Words about Sport / zigzag matrix

1313. Some Words about Sport

Time Limit: 0.5 second
Memory Limit: 16 MB
Ural doctors worry about the health of their youth very much. Special investigations showed that a lot of clever students instead of playing football, skating or bicycling had participated in something like Programming Olympiads. 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 would 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 invented a fundamentally new monitor with diagonal trace of a beam in its electron-beam tube. Soon the winners of Ural Programming Championship would be awarded with such monitors. In the specially designed square monitor the electronic beam would 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 would 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 should transform the usual “rectangle” signal to the signal necessary for this kind of monitors. Program should be fast and reliable. That’s why the development of this program is entrusted to the participants of the Ural Championship for Sports Programming.

Input

The 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).

Output

You 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

inputoutput
4
1 3 6 10
2 5 9 13
4 8 12 15
7 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, 2004

Solution:
输入:需要输入N×N的数组,第一行输入N,接下来输入数组。
输出:沿左下到右上的斜对角线输出数组
思路:首先输出上三角矩阵,
                起始行变量n:0~N-1,
                        行变量i:n~0
                        列变量j:0~n
然后输出下三角矩阵,
               起始列变量n:1~N-1
                      行变量i:N-1~n
                      列变量j:n~N-1

具体代码如下:
// problem 1313

#include <iostream>
using namespace std;


int main()
{
	
	int N;
	cin>>N;

	int** a=new int*[N];  //创建动态二维数组
	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)       //输出算法
	{
		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;
}

收获:建立动态二维数组。

参考:


-----------------------------------------------------------------
扩展:输入N,输出N*N的zigzag矩阵。例如5*5的矩阵为
0       1       5       6       14
2       4       7       13      15
3       8       12      16      21
9       11      17      20      22
10      18      19      23      24
方法一:
对左上角和右下角分别处理
#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 half
		if(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 half
		if(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;

}

方法二:
观察矩阵特性:
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;

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值