62,614
社区成员
发帖
与我相关
我的任务
分享
public class ScrewSquare {
public static void main(String[] args) {
ScrewSquare o = new ScrewSquare();
o.fill();
o.print();
}
int w, h;
int[][] array;
int left, top, right, bottom;
int start;
public ScrewSquare() {
w = 5;
h = 5;
array = new int[w][h];
left = 0;
top = 0;
right = w - 1;
bottom = h - 1;
start = 1;
}
public void fill() {
while (left <= right && top <= bottom) {
for (int x=left; x<=right; x++)
array[x][top] = start++;
for (int y=top+1; y<=bottom; y++)
array[right][y] = start++;
for (int x=right-1; x>=left; x--)
array[x][bottom] = start++;
for (int y=bottom-1; y>=top+1; y--)
array[left][y] = start++;
left ++; top ++; right --; bottom --;
}
}
public void print() {
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
System.out.printf("%2d ", array[x][y]);
}
System.out.println();
}
}
}
public class ScrewSquare
{
int n = 0;
public ScrewSquare(int n){
this.n = n;
}
private int getElement(int i,int j){
int mid = n/2;
int k = Math.min( Math.min(i,j) ,n -1 - Math.max(i,j));
int sum = 4 * ( k * (n + 1) - k * (k+1));
if( j >= i) sum += (i+j) - 2*k + 1;
else sum += ( n - 2*k - 1) * 4 - (i+j) + 2*k + 1;
return sum;
}
public void print(){
for(int i = 0 ; i < n;i++){
for(int j = 0; j < n;j++)
System.out.print(" " + getElement(i,j));
System.out.println();
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("usage: \"java screwSquare integer.\"");
System.exit(0);
}
int n = Integer.parseInt(args[0]);
new ScrewSquare(n).print();
}
}
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int getElement(int i,int j,int n){
int mid = n/2;
int k = min( min(i,j) ,n -1 - max(i,j));
int sum = 4 * ( k * (n + 1) - k * (k+1));
if( j >= i) sum += (i+j) - 2*k + 1;
else sum += ( n - 2*k - 1) * 4 - (i+j) + 2*k + 1;
return sum;
}
int main(){
int n,i,j;
printf(" n = ");
scanf("%d",&n);
for(i = 0 ; i < n;i++){
for(j = 0 ; j < n;j++)
printf("%5d ",getElement(i,j,n));
printf("\n");
}
return 0;
}
public class ScrewSquare {
public static void main(String[] args) {
int w = 5;
int h = 5;
int[][] array = new int[w][h];
fill(array, 0, 0, w-1, h-1, 1);
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
System.out.printf("%2d ", array[x][y]);
}
System.out.println();
}
}
public static void fill(int[][] array, int left, int top, int right, int bottom, int start) {
if (left > right || top > bottom) return;
for (int x=left; x<=right; x++)
array[x][top] = start++;
for (int y=top+1; y<=bottom; y++)
array[right][y] = start++;
for (int x=right-1; x>=left; x--)
array[x][bottom] = start++;
for (int y=bottom-1; y>=top+1; y--)
array[left][y] = start++;
fill(array, left+1, top+1, right-1, bottom-1, start);
}
}
import java.text.DecimalFormat;
public class HelixMatrix {
private int[][] array;
private int m,n;
public HelixMatrix(int m, int n) {
this.m = m;
this.n = n;
array = new int[m][];
for(int i = 0; i < m; i++)
array[i] = new int[n];
}
private void fill() {
int count = m * n;
int direct = 0;
int round = 1;
for(int index = 1, x = 0, y = 0; index <= count; index++) {
array[x][y] = index;
switch(direct) {
case 0: //向右
if(y < n - round)
y++;
else {
direct = 1;
x++;
}
break;
case 1:
if(x < m - round)
x++;
else {
direct = 2;
y--;
}
break;
case 2:
if(y >= round)
y--;
else {
direct = 3;
x--;
}
break;
case 3:
if(x > round)
x--;
else {
direct = 0;
round++;
y++;
}
break;
}
}
}
private void printMatrix() {
DecimalFormat df = new DecimalFormat(" 000");
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++)
System.out.print(df.format(array[i][j]));
System.out.println();
}
}
public static void main(String[] args) {
HelixMatrix matrix = new HelixMatrix(8,6);
matrix.fill();
matrix.printMatrix();
}
}
/*
轮回矩阵
阿长最近迷上了一种矩阵,他认为通过分析这种图形可以参悟人的生死轮回(狂汗。。。)。
这个图形由1到n*n这些数字组成。n表示一个人的年龄。比如,当一个人的年龄为4的时候,
那么对于他的轮回矩阵就是如下的一个图形:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
从左上角的1开始,以顺时针的方向进行旋涡式的伸展。这样的一个图形我们称它为
4岁的轮回矩阵。为了更好的研究这些矩阵,阿长不得不再次求助于你,希望你能编
写一个程序,当我们输入一个人的年龄的时候,你的程序能生成一个对于该年龄轮回矩阵。
输入:
一个数字n,表示年龄。对于30%的数据,n<=50,对于100%的数据,n<=500(汗,谁活了
这么久。。。。)。
输出:
轮回矩阵的结构图。每行的数字之间用一个空格分开,注意每行最后一个数字后面不要留有空格。
无须考虑数字的对齐问题。
样例输入1:
4
样例输出1:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
样例输入2:
10
样例输出2:
1 2 3 4 5 6 7 8 9 10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63 84 85 86 87 88 71 46 13
33 62 83 96 97 98 89 72 47 14
32 61 82 95 100 99 90 73 48 15
31 60 81 94 93 92 91 74 49 16
30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19
【华中科技大学计算机学院第三届程序设计大赛试题】
*/
import java.util.*;
import java.io.*;
public class arrayTest
{
public static void main(String[] arg) throws NumberFormatException, IOException
{
System.out.println("请输入N:");
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int x = Integer.parseInt(read.readLine());
int count = (x / 2) + 1;
int k = x;
int[][] a = new int[k][k];
for( int i = 1; i <= count; i++ )
{
for( int n = i - 1; n <= k - i; n++ )
{
if( i == 1 && n == 0 )
a[i-1][n] = 1;
else
a[i-1][n] = a[i-1][n-1] + 1;
}
for( int n = i; n <= k - i; n++ )
{
a[n][k-i] = a[n-1][k-i] + 1;
}
for( int n = k - i - 1; n >= i - 1; n-- )
{
a[k-i][n] = a[k-i][n+1] + 1;
}
for( int n = k - i - 1; n >= i; n-- )
{
a[n][i-1] = a[n+1][i-1] + 1;
}
}
for( int i = 0; i < k; i++ )
{
for( int j = 0; j < k; j++ )
{
if( a[i][j] <= 9 )
{
System.out.print(" "+a[i][j]);
}
else
System.out.print(" "+a[i][j]);
}
System.out.println();
}
}
}
public class LuoXuan{
class zuoBiao{
private int x;
private int y;
private zuoBiao next;
public zuoBiao(int m,int n){
this(m,n,null);
}
public zuoBiao(int m,int n,zuoBiao s){
x=m;
y=n;
next=s;
}
}
private zuoBiao head;
public LuoXuan(){
head=new zuoBiao(0,0);
}
public int OneCircle(int n){
zuoBiao temp=head;
int i=0,mark=n;
for(;n>=1;n=n-2,i=i+1){
temp.next=new zuoBiao(i,i);
temp=temp.next;
while(temp.y<n+i-1){
temp.next=new zuoBiao(temp.x,temp.y+1);
temp=temp.next;
}
while(temp.x<n+i-1){
temp.next=new zuoBiao(temp.x+1,temp.y);
temp=temp.next;
}
while(temp.y>i){
temp.next=new zuoBiao(temp.x,temp.y-1);
temp=temp.next;
}
while(temp.x>i+1){
temp.next=new zuoBiao(temp.x-1,temp.y);
temp=temp.next;
}
}
return mark;
}
public void Print(int n){
int[][] Diagram=new int[n][n];
zuoBiao temp=head.next;
int i=1;
while(temp!=null){
int x=temp.x;
int y=temp.y;
Diagram[x][y]=i;
i++;
temp=temp.next;
}
for(int k=0;k<Diagram.length;k++){
for(int j=0;j<Diagram[k].length;j++)
System.out.print(Diagram[k][j]+"\t");
System.out.println();
}
}
public static void main(String[] args){
LuoXuan luoXuan1=new LuoXuan();
luoXuan1.Print(luoXuan1.OneCircle(6));
}
}