求助JAVA编程两道题
答案:4 悬赏:60 手机版
解决时间 2021-05-01 08:39
- 提问者网友:临风不自傲
- 2021-04-30 17:01
求助JAVA编程两道题
最佳答案
- 五星知识达人网友:有你哪都是故乡
- 2021-04-30 17:43
第一个用递归来算。给你一个我以前做的例子(顺时针和逆时针旋转都有,小心问问会吃掉缩进,最好用编程工具格式化一下,Eclipse里面的快捷键是Ctrl+Shift+F)。
第二个除了塞二维数组这种笨办法,我暂时没想到更好的处理办法。
public class RollingTest { public static String getLeftLine(int length, int lineNum, int startNum){ String result = ""; if(1 == lineNum){ result += startNum + "\t"; for(int i = 0; i < length -1 ; i++){ result += startNum + length * 4 - 5 - i; result += "\t"; } }else if(length == lineNum){ result += startNum + lineNum -1 + "\t"; for(int i = 1; i < length ; i++){ result += startNum + lineNum -1 + i; result += "\t"; } }else{ result += startNum + lineNum -1 + "\t"; result += getLeftLine(length-2, lineNum-1, startNum + length * 4 - 4 ); result += startNum + length * 2 - 2 + (length - lineNum); result += "\t"; } return result; } public static String getRightLine(int length, int lineNum, int startNum){ String result = ""; if(1 == lineNum){ result += startNum + "\t"; for(int i = 1; i < length ; i++){ result += startNum + i; result += "\t"; } }else if(length == lineNum){ result += startNum + 3*(length-1) + "\t"; for(int i = 1; i < length ; i++){ result += startNum + 3*(length-1) - i; result += "\t"; } }else{ result += startNum + 3*(length-1) + (length - lineNum) + "\t"; result += getRightLine(length-2, lineNum-1, startNum + (length -1) * 4 ); result += startNum + (length -1 ) + (lineNum - 1); result += "\t"; } return result; } public static String getLeftRoll(int length){ String result = ""; for(int i = 1; i <= length; i++){ result += getLeftLine(length, i, 1); result += "\n"; } return result; } public static String getRightRoll(int length){ String result = ""; for(int i = 1; i <= length; i++){ result += getRightLine(length, i, 1); result += "\n"; } return result; } public static void main(String[] args) { System.out.println("左旋:"); System.out.println(getLeftRoll(4)); System.out.println("右旋:"); System.out.println(getRightRoll(4)); }}
第二个除了塞二维数组这种笨办法,我暂时没想到更好的处理办法。
public class RollingTest { public static String getLeftLine(int length, int lineNum, int startNum){ String result = ""; if(1 == lineNum){ result += startNum + "\t"; for(int i = 0; i < length -1 ; i++){ result += startNum + length * 4 - 5 - i; result += "\t"; } }else if(length == lineNum){ result += startNum + lineNum -1 + "\t"; for(int i = 1; i < length ; i++){ result += startNum + lineNum -1 + i; result += "\t"; } }else{ result += startNum + lineNum -1 + "\t"; result += getLeftLine(length-2, lineNum-1, startNum + length * 4 - 4 ); result += startNum + length * 2 - 2 + (length - lineNum); result += "\t"; } return result; } public static String getRightLine(int length, int lineNum, int startNum){ String result = ""; if(1 == lineNum){ result += startNum + "\t"; for(int i = 1; i < length ; i++){ result += startNum + i; result += "\t"; } }else if(length == lineNum){ result += startNum + 3*(length-1) + "\t"; for(int i = 1; i < length ; i++){ result += startNum + 3*(length-1) - i; result += "\t"; } }else{ result += startNum + 3*(length-1) + (length - lineNum) + "\t"; result += getRightLine(length-2, lineNum-1, startNum + (length -1) * 4 ); result += startNum + (length -1 ) + (lineNum - 1); result += "\t"; } return result; } public static String getLeftRoll(int length){ String result = ""; for(int i = 1; i <= length; i++){ result += getLeftLine(length, i, 1); result += "\n"; } return result; } public static String getRightRoll(int length){ String result = ""; for(int i = 1; i <= length; i++){ result += getRightLine(length, i, 1); result += "\n"; } return result; } public static void main(String[] args) { System.out.println("左旋:"); System.out.println(getLeftRoll(4)); System.out.println("右旋:"); System.out.println(getRightRoll(4)); }}
全部回答
- 1楼网友:你哪知我潦倒为你
- 2021-04-30 20:28
定义二维数组,根据规律来编。如果java不会,那就用C语言来编!
- 2楼网友:野慌
- 2021-04-30 19:04
看样子像排序的问题。难点在于如何描述数据。都可以用链式表搞。第一个螺旋状,第二个打折线、
- 3楼网友:廢物販賣機
- 2021-04-30 17:58
可以用这些数字来打印这种图形也算吧。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯