/**
* Implement algorithm like:
f(2,3)=2+22+222;
f(3,4)=3+33+333+3333
......
*/
public class Test{
// get each one value
public static int recur(int a ,int b){
if (b == 1) {
return a;
}else{
return a * (int) Math.pow(10, b - 1) + recur(a, b - 1);
}
}
// get the summary of each one value
public static int getValue(int a, int b) {
int sum = 0;
for (int i = 1; i <= b; i++) {
System.out.print(recur(a, i) + "+");
sum = sum + recur(a, i);
}
return sum;
}
public static void main(String[] args) {
System.out.println("recursive result: " + recur(2, 3));
Test.getValue(2, 3);
}
}
递归实现一小算法
最新推荐文章于 2024-11-02 13:05:10 发布