就是写一个X by X 的正方形,然后里面写上一句信息,每个字母填上去每个空格,但是是要按照那个Magic Square的算法,这个正方形要想象着上下左右各有一个一样的正方形,对比这和中间的位置,把第一个字母放在中间行的最顶那个空格,然后开始写,写法规则是第2个字母是在第1个字幕的左上格,然后第3个字母是第2个字母的左上格,如果遇到已经被填上的格子,就在原来的字母往下写下一个字母,在继续往左上格写。例如,第一个字母在第一排的中间格开始,第2个字母要在第一个字母的左上格填上,但上面是没有格子的,但想象它是有一个同样的正方形在那里,然后对比那个第2个字母是最后一排的中间往左边一列那个格子,然后继续往上写我想写上的信息是“I love coffee”,请大家帮帮忙了!!谢谢!!如果不明白我写的地方请提出来
解决方案 »
- java团队开发项目的问题
- 求字符串转日期的方法,请各位进来看
- 为什么executeBatch执行出来显示No row count was produced from executeBatch
- 学过C++的来解答一下
- 想用java写一个p2p实现文件共享的小软件,求助!!!!
- 国庆散个分
- JBuilder书下载给个连结
- 有关session中的getAttribute(java.lang.String name)的问题???
- 我用ODBC连接SQL-SERVER,为什么写到数据库里的汉字变乱码?
- 程序挂了
- JAVA打印指定TXT出现乱码!!!!
- 来个高手给我摆摆Static Nested Class 、Inner Class和Anonymous Inner Class
int[][] nums = magicSquare(3);
printMagicSquare(nums);
} public static int[][] magicSquare(int n) {
int[][] nums = new int[n][n];
int i = 0, j = n / 2, num = 1, t = n * n;
nums[i][j] = num;
while(++num <= t) {
i = (i - 1 + n) % n;
j = (j - 1 + n) % n;
if(nums[i][j] > 0) {
i = (i - 2 + n) % n;
j = (j - 1 + n) % n;
}
nums[i][j] = num;
}
return nums;
} public static void printMagicSquare(int[][] nums) {
for(int i = 0; i < nums.length; i++) {
for(int j = 0; j < nums[i].length; j++) {
if(j > 0) {
System.out.print(" ");
}
System.out.printf("%2d", nums[i][j]);
}
System.out.println();
}
}
} 9 1 5
4 8 3
2 6 7
char[][] ms = magicSquare("I love coffee");
printMagicSquare(ms);
} /**
* 计算字符串需要多少阶幻方
* @param chs
* @return
*/
private static int guessSquare(char[] chs) {
int s = 1, b = 1;
while(s < chs.length) {
s += ++b * 2 - 1;
}
return b;
} public static char[][] magicSquare(String str) {
char[] chs = str.toCharArray();
int n = guessSquare(chs);
char[][] ms = new char[n][n];
int i = 0, j = n / 2, k = 0;
ms[i][j] = chs[k];
while(++k < chs.length) {
i = (i - 1 + n) % n;
j = (j - 1 + n) % n;
if(ms[i][j] > 0) {
i = (i - 2 + n) % n;
j = (j - 1 + n) % n;
}
ms[i][j] = chs[k];
}
return ms;
} public static void printMagicSquare(char[][] ms) {
for(int i = 0; i < ms.length; i++) {
for(int j = 0; j < ms[i].length; j++) {
if(j > 0) {
System.out.print(" ");
}
System.out.printf("%c", ms[i][j] == 0 ? '.' : ms[i][j]);
}
System.out.println();
}
}
}输出结果(未填充部分用“.”表示):o . I
e e . o
l v f e
. c f