打印以下程序

问题描述

求个程序,打印出以下数据: 1   2   6   7  15  16  28  29  45  3   5   8  14  17  27  30  44  46  4   9  13  18  26  31  43  47  60  10  12  19  25  32  42  48  59  61  11  20  24  33  41  49  58  62  71  21  23  34  40  50  57  63  70  72  22  35  39  51  56  64  69  73  78  36  38  52  55  65  68  74  77  79  37  53  54  66  67  75  76  80  81

解决方案

跟楼上几位思路差不多~发现我的变量命名真是烂啊public class pbox {public static void main(String[] args) {printBox(9);}public static void printBox(int row) {if (row < 1) {System.out.println("......");return;} else if (row > 80) {System.out.println("......");return;}for (int i = 0; i < row; i++) {for (int j = 0; j < row; j++) {int s = i + j;int sm = s > row - 1 ? s - (row - 1) : 0;int s1 = sumOfAS(1, s, 1);int s2 = sumOfAS(2, sm - 1, 2);int sf = s1 - s2;int x = s % 2 == 0 ? j - sm : i - sm;int oyeah = x + sf + 1;System.out.printf("%3d", oyeah);}System.out.println();}}// 等差数列求和public static int sumOfAS(int o, int n, int j) {return o * n + (n - 1) * n * j / 2;}}
解决方案二:
String.format("%3d", (topOfLast + y))这句很经典,fivestarwy兄写程序太仔细了。
解决方案三:
呵呵,这样就可以适应许多种情况了,多谢fivestarwy的改进版.
解决方案四:
今天下午推了一下,还真有点麻烦右半部的值不是与纵横坐标成线性关系代码就直接在fishbottle兄的程序上改了一下 顺便做了推广public class PrintNumber {public static void main(String[] args) {//a matrix of ROW*ROWfinal int ROW=9;for (int x = 1; x < ROW+1; x++) {for (int y = 1; y < ROW+1; y++) {if ((x + y) <= ROW+1) {int lineNum = x + y - 1;int topOfLast = (lineNum - 1) * (lineNum) / 2;boolean direction = lineNum % 2 != 0 ? true : false;if (direction) {System.out.print(String.format("%3d", (topOfLast + y)));} else {System.out.print(String.format("%3d", (topOfLast + lineNum - y + 1)));}} else {int innerX = x;int innerY = y;int offsetX = 1;int offsetY = ROW-1;innerX -= offsetX;innerY -= offsetY;int lineNum = innerX + innerY - 1;int loopNum = 0;int start = ROW-1;for (int j = 1; j < lineNum; j++) {loopNum += start;start--;}boolean direction = lineNum % 2 == 0 ? true : false;if(ROW%2!=0){if (direction) {System.out.print(String.format("%3d", ROW*ROW-(ROW-(x+y-ROW))*(ROW-(x+y-ROW)+1)/2 +y-ROW));} else {System.out.print(String.format("%3d", (ROW*ROW-(ROW-(x+y-ROW)+1)*(ROW-(x+y-ROW)+2)/2+1 - y+ROW)));}}else{if (direction) {System.out.print(String.format("%3d", (ROW*ROW-(ROW-(x+y-ROW)+1)*(ROW-(x+y-ROW)+2)/2+1 - y+ROW)));} else {System.out.print(String.format("%3d", ROW*ROW-(ROW-(x+y-ROW))*(ROW-(x+y-ROW)+1)/2 +y-ROW));}}}}System.out.println();}}}
解决方案五:
有可能吧,这个没有写得那么有扩展性。里面有些参数是只争对行数为9的时候的。比如:[color=darkred][/color]int offsetX = 1; int offsetY = 8; int start = 8;int topOfLast = loopNum + 45;
解决方案六:
这题不用数组的话,还是有点挑战性的fishbottle兄,你的程序当行数是偶数时,有点问题哦(例如是6行6列)
解决方案七:
这个题目有几个要点:这个矩阵是两个斜的三角形 1 2 3 4 5 61.左半部分和右半部分是有区别的:if ((x + y) <= 10)2.你需要通过x,y判断前一行的最大值topOfLast(从斜的角度看)左边部分:int lineNum = x + y - 1; int topOfLast = (lineNum - 1) * (lineNum) / 2;右边部分(我想这一块还可以改进的):int innerX = x; int innerY = y; int offsetX = 1; int offsetY = 8; innerX -= offsetX; innerY -= offsetY; int lineNum = innerX + innerY - 1; int loopNum = 0; int start = 8; for (int j = 1; j < lineNum; j++) { loopNum += start; start--; } int topOfLast = loopNum + 45; 3.判断从斜的角度来看,这一行的方向是向上的,还是向下的,这个可以通过(x+y)%2是否为0来判断,并且左半部分和右半部分是不一样的。4.找到一个不会变的参考,当求左半部分时,可以用y作为参考,当求右半部分时,用x作为参考这样就可以通过x和y确定这一个cell对应的值了。仅供参考
解决方案八:
package test;public class Test {/** * @param args */public static void main(String[] args) {for (int x = 1; x < 10; x++) {for (int y = 1; y < 10; y++) {if ((x + y) <= 10) {int lineNum = x + y - 1;int topOfLast = (lineNum - 1) * (lineNum) / 2;// if direction is true,up// otherwise, downboolean direction = lineNum % 2 != 0 ? true : false;if (direction) {System.out.print("" + (topOfLast + y) + " ");} else {System.out.print("" + (topOfLast + lineNum - y + 1)+ " ");}} else {int innerX = x;int innerY = y;int offsetX = 1;int offsetY = 8;innerX -= offsetX;innerY -= offsetY;int lineNum = innerX + innerY - 1;int loopNum = 0;int start = 8;for (int j = 1; j < lineNum; j++) {loopNum += start;start--;}int topOfLast = loopNum + 45;// if direction is true,up// otherwise, downboolean direction = lineNum % 2 == 0 ? true : false;if (direction) {System.out.print("" + (topOfLast + 10 - x)+ " ");} else {System.out.print("" + (topOfLast + x - lineNum ) + " ");}}}System.out.println();}}}1 2 6 7 15 16 28 29 45 3 5 8 14 17 27 30 44 46 4 9 13 18 26 31 43 47 60 10 12 19 25 32 42 48 59 61 11 20 24 33 41 49 58 62 71 21 23 34 40 50 57 63 70 72 22 35 39 51 56 64 69 73 78 36 38 52 55 65 68 74 77 79 37 53 54 66 67 75 76 80 81
解决方案九:
package cn.iwoo;public class PrintTest {/** * 需要打印的数组的长度 */public static int SIZE = 9;public static void main(String[] args) {int[][] all = new int[SIZE][SIZE];// 初始位置int x = 0;int y = 0;all[x][y] = 1;Direction lastDirection = Direction.H; // 上一次走的方向 for (int i = 1; i < SIZE * SIZE; i++) {Direction position = getNextDirection(lastDirection, x, y); // 得到要走的方向lastDirection = position;int[] nextLocation = getNextLocation(position, x, y); // 得到要走的坐标x = nextLocation[0];y = nextLocation[1];all[x][y] = i + 1; // 给坐标赋值}// 打印printArray(all);}/** * <p> * Description: 打印数组 * </p> */private static void printArray(int[][] array) {for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {if (j == 0) {System.out.println("");}System.out.print(" " + array[j][i]);}}}/** * <p> * Description: 得到下一个坐标 * </p> * @param position 要走的方向 * @param x * @param y * @return */private static int[] getNextLocation(Direction position, int x, int y) {int[] nextLocation = new int[2];if (Direction.H.equals(position)) {nextLocation[0] = x + 1; // 水平走, x + 1, y不变nextLocation[1] = y;} else if (Direction.V.equals(position)) {nextLocation[0] = x; // 垂直走, x, y + 1nextLocation[1] = y + 1;} else if (Direction.D.equals(position)) {nextLocation[0] = x - 1; // 向下走, x - 1, y + 1nextLocation[1] = y + 1;} else if (Direction.T.equals(position)) {nextLocation[0] = x + 1; // 向上走, x + 1, y - 1nextLocation[1] = y - 1;}return nextLocation;}/** * <p> * Description: 得到下一步的方向 * </p> * @param lastDirection * @param x * @param y */private static Direction getNextDirection(Direction lastDirection, int x, int y) {if (x == 2 && y == 2) {System.out.println();}if (!(x == 0 || y == 0 || x == SIZE - 1 || y == SIZE - 1)) {return lastDirection;}if (x == 0 && y == 0) {return Direction.H;}if (x + y < SIZE - 1) { // 前半程的方向判断if (x == 0 && y % 2 == 0) {return Direction.T;} else if (x == 0 && y % 2 != 0) {return Direction.V;} else if (y == 0 && x % 2 != 0) {return Direction.D;} else if (y == 0 && x % 2 == 0) {return Direction.H;}} else { // 后半程的方向判断if (SIZE % 2 == 0) { // 注意此处. 由于SIZE的%2不同, 带来了对后半程的方向的逻辑不一样.if (x == SIZE - 1 && y % 2 == 0) {return Direction.D;} else if (x == SIZE - 1 && y % 2 != 0) {return Direction.V;} else if (y == SIZE - 1 && x % 2 != 0) {return Direction.T;} else if (y == SIZE - 1 && x % 2 == 0) {return Direction.H;}} else {if (x == SIZE - 1 && y % 2 != 0) {return Direction.D;} else if (x == SIZE - 1 && y % 2 == 0) {return Direction.V;} else if (y == SIZE - 1 && x % 2 == 0) {return Direction.T;} else if (y == SIZE - 1 && x % 2 != 0) {return Direction.H;}}}return null; // 理论上不会有null返回的}enum Direction {H, // 水平走V, // 垂直走D, // 向下走 [斜向下的走]T, // 向上走 [斜向上的走]}}
解决方案十:
已经做出来了.. 其实前半程的很简单, 难的是后半程的, 而且, SIZE不一样, 后半程的判断逻辑也有不一样. [一直被卡在这个地方. 耗了很多时间.] 1 2 6 7 15 16 28 29 45 3 5 8 14 17 27 30 44 46 4 9 13 18 26 31 43 47 60 10 12 19 25 32 42 48 59 61 11 20 24 33 41 49 58 62 71 21 23 34 40 50 57 63 70 72 22 35 39 51 56 64 69 73 78 36 38 52 55 65 68 74 77 79 37 53 54 66 67 75 76 80 81
解决方案十一:
package test;public class Test {/** * @param args */public static void main(String[] args) {for (int x = 1; x < 10; x++) {for (int y = 1; y < 10; y++) {if ((x + y) <= 10) {int lineNum = x + y - 1;int topOfLast = (lineNum - 1) * (lineNum) / 2;// if direction is true,up// otherwise, downboolean direction = lineNum % 2 != 0 ? true : false;if (direction) {System.out.print("" + (topOfLast + y) + " ");} else {System.out.print("" + (topOfLast + lineNum - y + 1)+ " ");}} else {/*int innerX = x;int innerY = y;int offsetX = 8;int offsetY = 1;innerX -= offsetX;innerY -= offsetY;int lineNum = innerX + innerY - 1;int loopNum = 0;int start = 8;for (int j = 1; j < lineNum; j++) {loopNum += start;start--;}System.out.println("loopNum:" + loopNum);int topOfLast = loopNum + 45;;// if direction is true,up// otherwise, downboolean direction = lineNum % 2 != 0 ? false : true;if (direction) {System.out.println("dddd" + (topOfLast + innerY - 1) );System.out.print("" + (topOfLast + innerY - 1) + " ");} else {System.out.print("" + (topOfLast + lineNum - innerY + 1)+ " ");}*/}}System.out.println();}}}明天再继续,暂时只能打一边,右边应该一样的

时间: 2024-09-02 23:12:46

打印以下程序的相关文章

打印后台程序服务无法启用的处理办法

  高手支招:打印后台程序服务,当添加打印机或是使用打印机时,系统报错"打印后台程序服务没有运行",出现此现象多是由于系统不稳定导致系统支持打印机的服务无法启用.该"打印后台程序服务"是 Windows 2000.XP.2003 系统支持打印机的服务,所有在系统内进行的与打印机相关操作均需要它. XP系统:添加打印机提示打印后台程序服务没有运行 解决方案: 出现这个问题时,首先请依据如下步骤手动启用该服务: 在桌面选中"我的电脑",鼠标右键选择&

如何解决打印后台程序服务没有运行的问题

  打印后台程序服务没有运行的解决方法: 第一步:点击"开始"菜单-->选择"运行"-->在运行中输入"services.msc"(不含引号),点击确定打开本地服务; 第二步:在"服务(本地)"右边窗口中找到并双击打开"Print Spooler"服务; 第三步:将"启动类型"设置为"自动"-->再点击"服务状态"下方的"

c语言打印素数程序求大神

问题描述 c语言打印素数程序求大神 #include int main() { int a[101],i,j; for(i=0;i<101;i++) a[i]=i; for(i=0;i<101;i++) { for(j=0;j<i-1;j++) { if(a[i]%j!=0)break; } a[i]=0; } for(i=0;i<101;i++) if(a[i]=0) printf("%d is a sushu.",i); return 0; } 初学者啊啊啊

vb net 打印 端口-程序打印不了小票,端口被占用

问题描述 程序打印不了小票,端口被占用 现在有一个VB.net写的程序要用post机打印小票,打印机被设为默认,然后用程序向LTP端口发送打印命令却打印不了,说是LTP端口被占.而把这个打印机删掉或者不设为默认却可以打印, 现在我想知道怎么才能在把打印机设为默认的情况下程序可以打印?

请问那位大侠可以实现文本缩放打印的程序?

问题描述 请问那位大侠可以实现文本缩放打印的java程序?本人已尝试java的drastirng图片打印,但缩放后字体和格式都有问题,请各位高手借鉴借鉴,小弟感激不尽. 解决方案 解决方案二:什么叫文本缩放打印"?把你的程序贴出来看一下吧?解决方案三:就是说文本想按原来的10%.70%或140%打印都可以解决方案四:drastirng图片打印有什么问题啊

WinXP打印后台程序服务没有运行怎么办?

  1.桌面我的电脑右键选择管理. 2.在打开的管理窗口,选择服务和应用程序,然后选择服务,在右边就可以显示出来所有的服务. 3.然后我们在右边的服务中,找到print spooler,点击启动此服务即可.如果启动了就点击重启这个服务. 4.或者我们可以进入控制面板点击管理工具,然后再选择里面的服务.

跪求在.NET2003里怎样实现DataGrid水晶报表打印 WEB程序的?在线等!望高手速度解决!谢谢!急!!!!!

问题描述 希望大家发展点同情心好么! 解决方案 解决方案二:if(mytable.Rows.Count==0)//mytable是一个DataTable,也可用DataSet{this.Button1.Enabled=false;return;}System.Web.UI.WebControls.DataGriddgExport=null;//IO用于导出并返回excel文件System.IO.StringWriterstrWriter=null;System.Web.UI.HtmlTextWr

在.NET2003里怎样实现DataGrid水晶报表打印 WEB程序的?在线等!望高手速度解决!谢谢!急!急!急!!!

问题描述 速度啊!!! 解决方案 解决方案二:n年前做过,其实还是调用ie的打印功能,一般是导出成word,在word中打印web中只有activex比较好的解决了打印的问题

asp.net中C# 自定义打印类程序

 代码如下 复制代码   public class PrintInfo     {         public string PortName { get; set; }         public string DepartureTime { get; set; }         public string AMPM { get; set; }         PrintDocument document = new PrintDocument();         private in