----------------------------------递归问题求解!谢谢
写一个递归方法writeBlock,用他输出M行N列,如,writeBlock( "* ",5,3)产生的输出为:
*****
*****
*****
谢谢
------解决方案--------------------建议楼主自己写,写错了不怕,怕就怕一点都不写.
------解决方案-------------------- public class AAA {
public void writeBlock(String srt, int m , int n){
if(n <1)
return;
else{
for(int i = 0 ; i <m; i++){
System.out.print(srt);
}
System.out.println();
writeBlock(srt, m , n-1);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AAA ex = new AAA();
ex.writeBlock( "* ", 5, 3);
}
}
------解决方案--------------------public class AAA {
int a=0;
int b=0;
public void writeBlock(String srt, int m , int n){
if(n> 0){
if(m> 0){
System.out.print(srt);
a++;
writeBlock(srt,m-1,n);
}
else {
System.out.println();
b++;
m=a/b;
writeBlock(srt,m,n-1);
}
}
}
public static void main(String[] args) {
AAA ex = new AAA();
ex.writeBlock( "* ", 4, 4);
}
}
嗬嗬,随便写了一个。不太会递归,不知道怎么样!!
还望高手指点一下!!!!!!!!!!
------解决方案--------------------出题的人脑袋被门挤了,这种题目用递归
------解决方案--------------------void writeBlock(char ch, int num, int time) {
if (time < 1) return;
for (int i = 0; i < num; i++) {
cout < < '* ';
}
cout < < endl;
writeBlock(ch, num, time-1);
}