public class Test{ static int KK = 10; static int p = 0; public static void main(String[] args) { long count = fun(KK); System.out.println(); System.out.println("Count:" + count); }
比如要画N = 225,令N = 000000000000000000 000000000011100001(int64 N) (此处假设N是32位整数)void draw(int64 i){ 取高32位->i1 取低32位->i2 int64 r; if (i2 == 0) return; if (i1 = 0) //是开始 { //此处画0个星星 r = i1 << 64 + i2; } if (i1 < i2) //是递增 { //此处画i1个星星 r = i1 << 64 + i2; } if (i1 = i2) //是到了第N层 { //此处画i2个星星 r = i1 << 64 + i2 -1; } if (i1 > i2) //是递减 { //此处画i2个星星 r = i1 << 64 + i2 -1; } draw(r); return; }用了伪代码,懒...
应该是: 比如要画N = 225,令N = 000000000000000000 000000000011100001(int64 N) void draw(int64 i){ 取高32位->i1 取低32位->i2 int64 r; if (i2 == 0) return; if (i1 == 0) //是开始 { //此处画0个星星 r = (i1+1) << 32; r = r + i2; } if (i1 < i2) //是递增 { //此处画i1个星星 r = (i1+1) << 32; r = r + i2; } if (i1 == i2) //是到了第N层 { //此处画i2个星星 r = i1 << 32; r = r + i2 -1; } if (i1 > i2) //是递减 { //此处画i2个星星 r = i1 << 32 ; r = r + i2 -1; } draw(r); return; }
这个可以吗? 8-Phehe, 不好意思,还是得用一个单独的常量来表示边界条件。不知道这样符不符合楼主本来的题意?public class RecursiveStars { public static void main(String[] args) { RecursiveStars r = new RecursiveStars(); r.recursive( 1 ); } void recursive( int n ) { final int MAX_NUM = 5; if ( n < MAX_NUM * 2 ) recursive( n + 1 ); if ( n <= MAX_NUM ) print( n ); else print( MAX_NUM * 2 + 1 - n ); } void print( int n ) { for ( int i=0; i<n; i++ ) System.out.print( "*" ); System.out.println(); } }
public class Test { static void go(P p) { if(p.number != 1) { p.number--; go(p); p.number++; } StringBuffer stars = new StringBuffer(); for(int i = 0; i < p.number; i++) { stars.append("*"); } p.stars[p.number - 1] = stars; p.stars[p.stars.length - p.number] = stars; } public static void main(String[] args) { P p = new P(); p.number = 10; p.stars = new StringBuffer[p.number * 2 -1]; go(p);
// output for(int i = 0; i < p.stars.length; i++) { System.out.println(p.stars[i]); } } } class P { int number; StringBuffer[] stars; }
空间换时间,一定有人不服气,再来一段 public class Test { static void go(P p) { if(p.cur < 1) return; output(p.cur); if(p.cur == p.max) { p.asc = false; } if(p.asc) p.cur ++; else p.cur--; go(p); } static boolean output(int count) { if(count == 0) return true; System.out.print("*"); if(output(count - 1)) { System.out.println(); } return false; } public static void main(String[] args) { P p = new P(10); go(p); } }class P { boolean asc = true; int max, cur = 1; P(int n) { max = n; } }
public static void main(String[] args) { int t=10; new T().display(t); }
public void display(int i) { upplay(i);// downplay(i); } public void downplay(int i) { while(i!=0){print(i--);System.out.println();} } public void upplay(int i) { int temp=0; while(i!=temp+1){print(++temp );System.out.println();} } public void print(int i) { while(i--!=0)System.out.print("*"); }看看这个是否符合你的标准
没想到有56楼了呵呵. 答案那么多真辛苦大家了.但还没发现让我觉得很漂亮的答案.可能是我一开始想太完美了.看到大家的讨论才知道递归有很多限制啊.我觉得完美的就一个方法带一个参数,然后除了输出外不调用其他方法.不过现在看来这样是不行的了. 总结一下大概有3种,不知对不对: 多带个参数(被我给限制了,因为觉得带多个参数很不美观,原题没这个限制,呵呵); 用成员变量(很多人都这样); 封装一个类作为参数; 10楼写的挺诡异,也算一种吧; 其他用多个方法的就不算了. 其实这道题是《Data Structures and Other Objects Using Java》这本书了的习题来的.ch8,第八题 www.aw.com 这个网站上有答案.不过我弄不到.注册很麻烦.有没有高手能搞到呢.虽然现在我对作者的答案没有期待了. 本来想结贴,不过现在看来还有很大的发展空间.期待高手答案.特别是cnapc和getter. by the way 关于头像...那个...嗯..其实是我的梦中情人来的.
public class Test{
static int KK = 10;
static int p = 0;
public static void main(String[] args) {
long count = fun(KK);
System.out.println();
System.out.println("Count:" + count);
}
private static long fun(int n){
if(n==2){
printStar(p+1);
printStar(p+2);
printStar(p+2);
printStar(p+1);
return 6;
}
long count = 0;
if(n>1){
p++;
printStar(p);
count = fun(n-1) + 2*n;
printStar(p);
p--;
}
return count;
}
private static void printStar(int k){
for(int i=0; i<k; i++)
System.out.print('*');
System.out.println();
}
}
public void paint(int n){
prnt(n);
System.out.println();
}
public void prnt(int n){
if(n<=0)
return ;
for(int i=0;i<n;i++){
System.out.print("*");
}
System.out.println();
n--;
prnt(n);
}
public static void main(String args[]){
new test().paint(5);
}}
import java.util.ArrayList;
import java.util.List;
public class Test
{ /**
* @param args
*/
public static void main( String[] args )
{
List<String> starList = getPrintStarList( 10 );
for( String stars : starList )
{
System.out.println( stars );
}
}
public static List<String> getPrintStarList( int num )
{
List<String> starList = new ArrayList<String>();
if( num == 1 )
{
starList.add( "*" );
return starList;
}
else if( num == 2 )
{
starList.add( "*" );
starList.add( "**" );
starList.add( "**" );
starList.add( "*" );
return starList;
}
else
{
starList = getPrintStarList( num - 1 );
starList.add( num - 1, getStar(num) );
starList.add( num, getStar(num) );
return starList;
}
}
private static String getStar( int num )
{
StringBuffer sb = new StringBuffer();
for( int i = 0; i < num; i++ )
{
sb.append( '*' );
}
return sb.toString();
}
}
int p = 5;
test(p);
}
public static void test(int n) {
p(5);
p1(5);
}
public static void p(int n) {
if(n<=0) {
return;
}else {
n = n - 1;
p(n);
print(n);
System.out.println();
}
}
public static void p1(int n) {
if(n<=0) {
return;
}else {
n = n - 1;
print(n);
System.out.println();
p1(n);
}
}
public static void print(int n) {
if(n<0) {
return;
}else {
print(--n);
System.out.print("*");
}
}
}
有点问题,应该是这样public class Test2 { public static void main(String[] args) {
int p = 5;
test(p);
}
public static void test(int n) {
p(n);
p1(n);
}
public static void p(int n) {
if(n<=0) {
return;
}else {
n = n - 1;
p(n);
print(n);
System.out.println();
}
}
public static void p1(int n) {
if(n<=0) {
return;
}else {
n = n - 1;
print(n);
System.out.println();
p1(n);
}
}
public static void print(int n) {
if(n<0) {
return;
}else {
print(--n);
System.out.print("*");
}
}
}
printUp(5);
printDown(5);
}
private static void printUp(int n) {
if(n < 1) {
return;
}
printUp(n - 1);
printline(n);
}
private static void printDown(int n) {
if(n < 1) {
return;
}
printline(n);
printDown(n - 1);
}
private static void printline(int n) {
if(n < 1) {
System.out.println();
return;
}
System.out.print('*');
printline(n - 1);
}
}
* 写一个能产生n行星号的图案的递归程序.第一行包含一个星号,下一行包含两个,如此增加下去直到第n行包含n个星号. <BR>
* 第n+1行包含n个星号,下一行包含n-1个,这样下去直到第2n行只有一个星号. 用递归完成,只允许有一个参数. <BR>
* <BR>
* 该类创建于 2008-10-8 下午02:38:04
*
* @version 1.0.0
* @author 侯磊
*/
public class I { public static void main(String[] args) {
print(5);
} public static void print(int n) {
printUp(n);
printDown(n);
} private static void printUp(int n) {
if (n <= 0) return;
printUp(n - 1);
printLine(n);
} private static void printDown(int n) {
if (n <= 0) return;
printLine(n);
printDown(n - 1);
} private static void printLine(int n) {
for (int i = 0; i < n; i++) {
System.out.print("*");
}
System.out.println();
}
}
* 写一个能产生n行星号的图案的递归程序.第一行包含一个星号,下一行包含两个,如此增加下去直到第n行包含n个星号. <BR>
* 第n+1行包含n个星号,下一行包含n-1个,这样下去直到第2n行只有一个星号. 用递归完成,只允许有一个参数. <BR>
* <BR>
* 该类创建于 2008-10-8 下午02:38:04
*
* @version 1.0.1
* @author 侯磊
*/
public class I { public static void main(String[] args) {
print(5);
} public static void print(int n) {
printUp(n);
printDown(n);
} private static void printUp(int n) {
if (n <= 0) return;
printUp(n - 1);
printLine(n);
} private static void printDown(int n) {
if (n <= 0) return;
printLine(n);
printDown(n - 1);
} private static void printLine(int n) {
if (n == 0) System.out.println();
if (n <=0) return;
System.out.print("*");
printLine(n - 1);
}
}
public static void main(String[] args) {
fun(10);
} private static void fun(int x) {
fun(x,x);
}
private static void fun(int x,int y) {
if(x>0){
for(int i=x; i<=y; i++){
System.out.print("*");
}
System.out.println("");
fun(x-1,y);
for(int i=x; i<=y; i++){
System.out.print("*");
}
System.out.println("");
}
}
}
public class Star {
public static void printstar(int n)
{
for(int i=0;i<n;i++)
System.out.print("*");
System.out.println();
} public static void digui1(int n)
{
if(n<=0)
return;
else
{
printstar(n);
digui1(n-1);
}
}
public static void digui2(int n)
{
if(n<=0)
return;
else
{
digui2(n-1);
printstar(n);
}
}
public static void digui(int n)
{
digui2(n);
digui1(n-1);
}
public static void main(String[] args)
{
digui(5);
}
}
输出:
*
**
***
****
*****
****
***
**
*
public static void gorilla(int n) {
if (n == 0) return;
if (n > 0) {
gorilla(n - 1);
for (int i = 0; i < n ; ++ i) System.out.print('*');
System.out.println();
}else {
for (int i = 0 ; i < 0 - n ; ++i) System.out.print('*');
System.out.println();
gorilla(n + 1);
}
}
...
如果分成兩次遞歸也不用兩個不同的函數
gorilla(n);
gorilla(-n);
btw, 你的頭像很恐怖
public class StarTest
{
public static void main(String[] args)
{
int num=6;
StarTest st=new StarTest();
st.star(num);
st.star(1-num);
}
public void star(int n)
{
if(n>0)
{
star(n-1);
for(int i=0;i<n;i++)
{
System.out.print(" * ");
}
System.out.println();
}
else if(n<0)
{
for(int i=0;i<-n;i++)
{
System.out.print(" * ");
}
System.out.println();
star(n+1);
}
else
{
return ;
}
}
}
应该符合LZ要求了吧,
依此...赫赫
(此处假设N是32位整数)void draw(int64 i){ 取高32位->i1
取低32位->i2 int64 r;
if (i2 == 0) return;
if (i1 = 0) //是开始
{
//此处画0个星星
r = i1 << 64 + i2;
}
if (i1 < i2) //是递增
{
//此处画i1个星星
r = i1 << 64 + i2;
}
if (i1 = i2) //是到了第N层
{
//此处画i2个星星
r = i1 << 64 + i2 -1;
}
if (i1 > i2) //是递减
{
//此处画i2个星星
r = i1 << 64 + i2 -1;
} draw(r);
return;
}用了伪代码,懒...
比如要画N = 225,令N = 000000000000000000 000000000011100001(int64 N)
void draw(int64 i){ 取高32位->i1
取低32位->i2 int64 r;
if (i2 == 0) return;
if (i1 == 0) //是开始
{
//此处画0个星星
r = (i1+1) << 32;
r = r + i2;
}
if (i1 < i2) //是递增
{
//此处画i1个星星
r = (i1+1) << 32;
r = r + i2;
}
if (i1 == i2) //是到了第N层
{
//此处画i2个星星
r = i1 << 32;
r = r + i2 -1;
}
if (i1 > i2) //是递减
{
//此处画i2个星星
r = i1 << 32 ;
r = r + i2 -1;
} draw(r);
return;
}
RecursiveStars r = new RecursiveStars();
r.recursive( 1 );
} void recursive( int n ) {
final int MAX_NUM = 5; if ( n < MAX_NUM * 2 )
recursive( n + 1 ); if ( n <= MAX_NUM )
print( n );
else
print( MAX_NUM * 2 + 1 - n );
} void print( int n ) {
for ( int i=0; i<n; i++ )
System.out.print( "*" );
System.out.println();
}
}
public class Test
{
static void go(P p)
{
if(p.number != 1)
{
p.number--;
go(p);
p.number++;
}
StringBuffer stars = new StringBuffer();
for(int i = 0; i < p.number; i++)
{
stars.append("*");
}
p.stars[p.number - 1] = stars;
p.stars[p.stars.length - p.number] = stars;
}
public static void main(String[] args)
{
P p = new P();
p.number = 10;
p.stars = new StringBuffer[p.number * 2 -1];
go(p);
// output
for(int i = 0; i < p.stars.length; i++)
{
System.out.println(p.stars[i]);
}
}
}
class P
{
int number;
StringBuffer[] stars;
}
public class Test
{
static void go(P p)
{
if(p.cur < 1)
return; output(p.cur);
if(p.cur == p.max)
{
p.asc = false;
}
if(p.asc)
p.cur ++;
else
p.cur--;
go(p);
}
static boolean output(int count)
{
if(count == 0)
return true;
System.out.print("*");
if(output(count - 1))
{
System.out.println();
}
return false;
}
public static void main(String[] args)
{
P p = new P(10);
go(p);
}
}class P
{
boolean asc = true;
int max, cur = 1;
P(int n)
{
max = n;
}
}
如此增加下去直到第n行包含n个星号.第n+1行包含n个星号,要打印两行星星,哈哈...当然咯,再多打印一行也就是加行代码而已^-^
难道LZ就是考验偶们的观察力?
要求贴出答案...
public void go(int n,int count,boolean dec){
if(dec==true&&count==0){
return;
}
if(dec==false&&count>n){
go(n,count-1,true);
return;
}
for(int i=0;i<count;i++){
System.out.print("*");
}
System.out.println();
count=(dec==true)?count-1:count+1;
go(n,count,dec);
}
public void start(int n){
go(n,1,false);
}
public static void main(String[] args){
Test test=new Test();
test.start(5);
}
}
唉,你大概没注意到 35 楼我给的程序,那个可是符合“第n+1行包含n个星号”滴……其实玩弄这些文字都没有意义,我看楼主题目中“只允许有一个参数”才是预设的难点吧?不过这个很难说的,就像前面有位兄弟说用一个自定义的对象作为参数,那还不是想传多少就传多少?就算是只能使用 primary type,那我用一个 long,也差不多相当于两个 int 了。
我觉得只要做好了封装就行了,这个递归如果只允许有一个int参数恐怕是很难,我是想不出来。
public static void main(String[] args)
{
int t=10;
new T().display(t);
}
public void display(int i)
{ upplay(i);//
downplay(i);
}
public void downplay(int i)
{
while(i!=0){print(i--);System.out.println();}
}
public void upplay(int i)
{
int temp=0;
while(i!=temp+1){print(++temp );System.out.println();}
}
public void print(int i)
{
while(i--!=0)System.out.print("*");
}看看这个是否符合你的标准
答案那么多真辛苦大家了.但还没发现让我觉得很漂亮的答案.可能是我一开始想太完美了.看到大家的讨论才知道递归有很多限制啊.我觉得完美的就一个方法带一个参数,然后除了输出外不调用其他方法.不过现在看来这样是不行的了.
总结一下大概有3种,不知对不对:
多带个参数(被我给限制了,因为觉得带多个参数很不美观,原题没这个限制,呵呵);
用成员变量(很多人都这样);
封装一个类作为参数;
10楼写的挺诡异,也算一种吧;
其他用多个方法的就不算了.
其实这道题是《Data Structures and Other Objects Using Java》这本书了的习题来的.ch8,第八题
www.aw.com 这个网站上有答案.不过我弄不到.注册很麻烦.有没有高手能搞到呢.虽然现在我对作者的答案没有期待了.
本来想结贴,不过现在看来还有很大的发展空间.期待高手答案.特别是cnapc和getter.
by the way 关于头像...那个...嗯..其实是我的梦中情人来的.
private static String gorilla2(int n, String str) {
if (n == 1) return str + "*" + "\n";
String tmpStr = "", spaceStr = "";
for (int i = 0 ; i < n ; ++i) {
tmpStr += "*";
spaceStr += " ";
}
tmpStr =str + tmpStr + "\n";
return gorilla2(n / 2, str) + tmpStr + gorilla2(n / 2, str + spaceStr);
}
private static String gorilla3(int n) {
if (n <= 1) return "*\n*";
String p = gorilla3(n - 1);
String str = "";
for (int i = 0 ; i < n ; ++i)
str += "*";
int mid = p.length() / 2 + 1;
return p.substring(0, mid) + str + "\n" + str + "\n" + p.substring(mid , p.length());
}
做了你第二個題有了點啟示