关于JAVA的Recursion的问题
1.有一个Vector,要求用Recursion移除Vector中所有的0(比如Vector是1,0,5,0,6,0,那么print1,5,6),应该怎么写?2.还是一个...
1.有一个Vector,要求用Recursion移除Vector中所有的0(比如Vector是1,0,5,0,6,0,那么print 1,5,6),应该怎么写?
2.还是一个Vector,要求把第三个数变为0(比如Vector是10,9,8,7,6,5,4,3,2,1。那么第一次输出为10,9,0,7,6,0,4,3,0,1)
3.检查n是否能被k整除,不允许用%和loop。可以通过检查n-k来判断
4.已经int n,要求用Recursion计算4*[1 - 1/3 + 1/5 - 1/7 + 1/9 - . . .1/(2*n-1)]
比如n=3,则为4*[1 - 1/3 + 1/5]
上面四道题必须使用recursion,不能使用loop。给出思路或代码均可,感激不尽 展开
2.还是一个Vector,要求把第三个数变为0(比如Vector是10,9,8,7,6,5,4,3,2,1。那么第一次输出为10,9,0,7,6,0,4,3,0,1)
3.检查n是否能被k整除,不允许用%和loop。可以通过检查n-k来判断
4.已经int n,要求用Recursion计算4*[1 - 1/3 + 1/5 - 1/7 + 1/9 - . . .1/(2*n-1)]
比如n=3,则为4*[1 - 1/3 + 1/5]
上面四道题必须使用recursion,不能使用loop。给出思路或代码均可,感激不尽 展开
3个回答
展开全部
1,
import java.util.Vector;
public class Test1 {
private int i;
public void zero(Vector<Integer> vec) {
if (i >= vec.size()) {
return;
}
if (vec.get(i) == 0) {
vec.remove(i);
zero(vec);
}
i++;
zero(vec);
}
public static void main(String[] args) {
Vector<Integer> vec = new Vector<Integer>();
vec.add(1);
vec.add(0);
vec.add(5);
vec.add(0);
vec.add(6);
vec.add(0);
Test1 test = new Test1();
test.zero(vec);
// /*test:
for (int i = 0; i < vec.size(); i++) {
System.out.println(vec.get(i));
}
// */
}
}
==========================================
2,
import java.util.Vector;
public class Test2 {
private int i = -1;
public void zero(Vector<Integer> vec) {
i = i + 3;
if (i > vec.size()) {
return;
}
vec.set(i, 0);
// /* test:
for (int i = 0; i < vec.size(); i++) {
System.out.print(String.valueOf(vec.get(i)).concat(","));
}
// */
System.out.println("");
zero(vec);
}
public static void main(String[] args) {
Vector<Integer> vec = new Vector<Integer>();
vec.add(10);
vec.add(9);
vec.add(8);
vec.add(7);
vec.add(6);
vec.add(5);
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
Test2 test = new Test2();
test.zero(vec);
}
}
===================================
3,
public class Test3 {
public void mul(int n, int k) {
int ret = k - n;
if (ret < 0) {
System.out.println("can not!");
return;
} else if (ret == 0) {
System.out.println("can!");
return;
} else {
mul(n, ret);
}
}
public static void main(String[] args) {
Test3 test3 = new Test3();
// /*test:
test3.mul(7, 14);
test3.mul(3, 14);
// */
}
}
=========================================
4,
public class Test4 {
private double sum = 0;
double count(int n) {
double i = Math.pow(-1, n + 1) * (double) 1 / (double) (2 * n - 1);
sum = sum + i;
if (n > 1) {
count(n - 1);
}
return sum;
}
public static void main(String[] args) {
Test4 test4 = new Test4();
// /*test:
double sum = test4.count(4);
System.out.print(4*sum);
// */
}
}
//全部调试过,不懂问我~
import java.util.Vector;
public class Test1 {
private int i;
public void zero(Vector<Integer> vec) {
if (i >= vec.size()) {
return;
}
if (vec.get(i) == 0) {
vec.remove(i);
zero(vec);
}
i++;
zero(vec);
}
public static void main(String[] args) {
Vector<Integer> vec = new Vector<Integer>();
vec.add(1);
vec.add(0);
vec.add(5);
vec.add(0);
vec.add(6);
vec.add(0);
Test1 test = new Test1();
test.zero(vec);
// /*test:
for (int i = 0; i < vec.size(); i++) {
System.out.println(vec.get(i));
}
// */
}
}
==========================================
2,
import java.util.Vector;
public class Test2 {
private int i = -1;
public void zero(Vector<Integer> vec) {
i = i + 3;
if (i > vec.size()) {
return;
}
vec.set(i, 0);
// /* test:
for (int i = 0; i < vec.size(); i++) {
System.out.print(String.valueOf(vec.get(i)).concat(","));
}
// */
System.out.println("");
zero(vec);
}
public static void main(String[] args) {
Vector<Integer> vec = new Vector<Integer>();
vec.add(10);
vec.add(9);
vec.add(8);
vec.add(7);
vec.add(6);
vec.add(5);
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
Test2 test = new Test2();
test.zero(vec);
}
}
===================================
3,
public class Test3 {
public void mul(int n, int k) {
int ret = k - n;
if (ret < 0) {
System.out.println("can not!");
return;
} else if (ret == 0) {
System.out.println("can!");
return;
} else {
mul(n, ret);
}
}
public static void main(String[] args) {
Test3 test3 = new Test3();
// /*test:
test3.mul(7, 14);
test3.mul(3, 14);
// */
}
}
=========================================
4,
public class Test4 {
private double sum = 0;
double count(int n) {
double i = Math.pow(-1, n + 1) * (double) 1 / (double) (2 * n - 1);
sum = sum + i;
if (n > 1) {
count(n - 1);
}
return sum;
}
public static void main(String[] args) {
Test4 test4 = new Test4();
// /*test:
double sum = test4.count(4);
System.out.print(4*sum);
// */
}
}
//全部调试过,不懂问我~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
answer to question 1:
public void removeZero(Vector<Integer> v, int index) {
if(v == null || index < 0) {
System.out.println("u kidding me mate!");
return;
}
if(v.size() <= 0 || index >= v.size()) { // base condition
return;
}
Integer i = (Integer)v.get(index);
if(i.intValue() == 0) {
v.remove(index);
System.out.println("0 removed at index: " + index);
}
else {
index++;
System.out.println("index incremented to: " + index);
}
removeZero(v, index); // recursion
}
public void removeZero(Vector<Integer> v, int index) {
if(v == null || index < 0) {
System.out.println("u kidding me mate!");
return;
}
if(v.size() <= 0 || index >= v.size()) { // base condition
return;
}
Integer i = (Integer)v.get(index);
if(i.intValue() == 0) {
v.remove(index);
System.out.println("0 removed at index: " + index);
}
else {
index++;
System.out.println("index incremented to: " + index);
}
removeZero(v, index); // recursion
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不用LOOP 怎么迭代遍历 数组 访问里面的值啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询