2325 - 格子问题

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 125 MB

输入三个自然数N,i,j (1<=i<=N,1<=j<=N),输出在一个N*N格的棋盘中,与格子(i,j)同行、同列、同一对角线的所有格子的位置。


<br />

如:<span>n=4</span>,<span>i=2</span>,<span>j=3</span>表示了棋盘中的第二行第三列的格子,如下图:

<span style="line-height:1.5;"><img src="http://tk.hustoj.com:80/attached/image/20140108/20140108154504_62166.jpg" alt="" />&nbsp; &nbsp;&nbsp;</span> 

当<span>n=4</span>,<span>i=2</span>,<span>j=3</span>时,输出的结果是:<span></span> 

<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (2,1) (2,2) (2,3) (2,4) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span>同一行上格子的位置<span>}</span> 

<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (1,3) (2,3) (3,3) (4,3) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</span>同列列上格子的位置<span>}</span> 

<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (1,2) (2,3) (3,4) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</span>左上到右下对角线上的格子的位置<span>}</span> 

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(4,1) (3,2) (2,3) (1,4) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;{左下到右上对角线上的格子的位置}

<br />

<br />

题目输入

每个测试文件只包含一组测试数据,每组输入数据包含三个自然数N,i,j。


题目输出

对于每组输入数据,输出四行数据:

第一行:同一行上格子的位置;

第二行:同列列上格子的位置;

第三行:左上到右下对角线上的格子的位置;

第四行:左下到右上对角线上的格子的位置。

具体格式见样例输出。


输入/输出样例

输入格式

4 2 3

输出格式

(2,1)(2,2)(2,3)(2,4)
(1,3)(2,3)(3,3)(4,3)
(1,2)(2,3)(3,4)
(4,1)(3,2)(2,3)(1,4)

C语言解答

#include<stdio.h>
int main()
{
	int a,b,i,j,n;
	scanf("%d%d%d",&n,&a,&b);
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(i==a)
				printf("(%d,%d)",i,j);
		}
	
	}
	printf("\n");
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(j==b)
				printf("(%d,%d)",i,j);
		}
	}
	printf("\n");
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(j-i==b-a)
				printf("(%d,%d)",i,j);
		}
	}
	printf("\n");
		for(i=n;i>0;i--)
	{
		for(j=n;j>0;j--)
		{
			if(j+i==b+a)
				printf("(%d,%d)",i,j);
		}
	}
	printf("\n");
	return 0;

}

C++解答

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int N,i,j;
	cin >> N >> i >> j;
	for(int k=1; k<=N; ++k)
		cout << "(" << i << "," << k << ")";
	cout << endl;
	for(int k=1; k<=N; ++k)
		cout << "(" << k << "," << j << ")";
	cout << endl;
	if(j-i>=0)
	{
		for(int k=1; k<=N-(int)fabs(j-i); ++k)
			cout << "(" << k << "," << (int)fabs(j-i)+k << ")";
	}
	else
	{
		for(int k=1; k<=N-(int)fabs(j-i); ++k)
			cout << "(" << (int)fabs(j-i)+k << "," << k << ")";
	}		 	 
	cout << endl;
	if((i+j)-(N+1)>=0)	
	{
		for(int k=N-(int)fabs((i+j)-(N+1)); k>=1; --k)
			cout << "(" << k+(int)fabs((i+j)-(N+1)) << "," << N+1-k << ")";
	}
	else
	{
		for(int k=N-(int)fabs((i+j)-(N+1)); k>=1; --k)
			cout << "(" << k << "," << N-(int)fabs((i+j)-(N+1))-k+1 << ")";
	}
	cout << endl;
	return 0;
}/*
 0  1  2  3
-1  0  1  2
-2 -1  0  1
-3 -2 -1  0 */

Java解答

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt(); // N*N格
		int i = scanner.nextInt(); // 第i行,格子(i,j)
		int j = scanner.nextInt(); // 第j列,格子(i,j)

		// (i,j)同一行上格子的位置
		for (int a = 1; a <= N; a++) {
			System.out.print("(" + i + "," + a + ")");
		}
		System.out.println();

		// (i,j)同一列上格子的位置
		for (int a = 1; a <= N; a++) {
			System.out.print("(" + a + "," + j + ")");
		}
		System.out.println();

		// 左上到右下对角线上的格子的位置
		// 从上往下,所以小到大,++
		for (int a = 1; a <= N; a++) {
			for (int b = 1; b <= N; b++) {
				if ((a - b) == (i - j)) {
					System.out.print("(" + a + "," + b + ")");
				}
			}
		}
		System.out.println();

		// 左下到右上对角线上的格子的位置
		// 从下往上,所以大到小,--
		for (int a = N; a >= 1; a--) {
			for (int b = N; b >= 1; b--) {
				if ((a + b) == (i + j)) {
					System.out.print("(" + a + "," + b + ")");
				}
			}
		}
	}
}