国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT甲級1105

2019-11-11 01:06:38
字體:
來源:轉載
供稿:網友

1105. Spiral Matrix (25)

時間限制150 ms內存限制65536 kB代碼長度限制16000 B判題程序Standard作者CHEN, Yue

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and ncolumns, where m and n satisfy the following: m*n must be equal to N; m>=n; and m-n is the minimum of all the possible values.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:
1237 76 20 98 76 42 53 95 60 81 58 93Sample Output:
98 95 9342 37 8153 20 7658 60 76
#include<iostream>#include<vector>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iomanip>using namespace std;int cmp(int a, int b){	return a > b;}/*public class Helix {static int N = 0;public static int Helix(int x, int y) {if ((x==1) && (y==1)) return 1;if ((x-y>=1)&&(x+y<=N)) return (1 + Helix(x+1,y));if ((x-y>=1)&&(x+y>N)) return (1 + Helix(x, y+1)); if ((x-y<1)&&(x+y<=N+1)) return (1 + Helix(x, y-1));if ((x-y<1)&&(x+y)>N+1) return (1 + Helix(x-1,y));else return 0;} public static void main(String[] args) {N = //輸入int值;for (int i= 1; i <= N; i++){for (int j= 1; j <= N; j++)System.out.PRint(Helix(i,j) + "/t");System.out.println(""); }}}這個就是先把矩陣每個位置二元組轉換成一維時對應的數值求出來,我轉別人的,不是自己想的,實質就是迷宮求解問題*/int main(){	int N;	cin >> N;	int t;	int N1 = N;	vector<int> v;	while (N--)	{		cin >> t;		v.push_back(t);	}	sort(v.begin(), v.end(), cmp);//非遞增	int i;	for (i = sqrt(N1); i <= N1; i++)	{		if (N1%i == 0)			break;	}//m和n肯定一個小于等于N平方根另一個大于等于N平方根	int m = max(i,N1/i), n = N1 / m;	int**a = new int*[m];	for (int i = 0; i < m; i++)	{			a[i] = new int[n];	}	for (int i = 0; i < m; i++)	{		for (int j = 0; j < n; j++)		{			a[i][j] = 0;		}	}//必須初始化為0		i = 0;	int x = 0, y = 0;	while (i < N1)	{		while (y < n&&!a[x][y])			a[x][y++] = v[i++];		y--;		x++;		while (x < m && !a[x][y])			a[x++][y] = v[i++];		x--;		y--;		while (y >= 0 && !a[x][y])			a[x][y--] = v[i++];		y++;		x--;		while (x >= 0 && !a[x][y])			a[x--][y] = v[i++];		x++;		y++;	}//螺旋放置的過程:先聲明橫坐標x和縱坐標y,然后類似走迷宮的方式,若前面能走(即判斷下一個位置是否為0,不為0就不能進行賦值了)	//分為四個過程,右下左上,依次進行即可	for (int i = 0; i < m; i++)	{		for (int j = 0; j < n; j++)		{			cout << a[i][j];			if (j != n - 1)				cout << " ";			else				cout << endl;		}			}	return 0;}
上一篇:繼承訪問權限

下一篇:CMake Overview

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 静海县| 湟源县| 茌平县| 桂阳县| 彭水| 大连市| 宁夏| 玉环县| 略阳县| 醴陵市| 北流市| 阳春市| 英山县| 绥中县| 南郑县| 阳春市| 文成县| 绥中县| 井陉县| 安岳县| 出国| 富裕县| 拜城县| 阜新市| 漯河市| 宜川县| 淄博市| 泰宁县| 蒲江县| 交城县| 呼伦贝尔市| 易门县| 随州市| 崇礼县| 凌云县| 馆陶县| 宿州市| 宿州市| 姜堰市| 鄄城县| 安吉县|