題目原網址是:https://www.hackerrank.com/challenges/java-loops/problem
這是java的
import java.util.Scanner;
public class question
{
public static void main(String []argh)
{
int f1=0,f2=1,f3=2;
final Scanner in = new Scanner(System.in);
System.out.print("input four numbers before calculate");
final int t=in.nextInt();
final int []rec=new int[t*3];
for(int time=0;time<rec.length;time++)
{
rec[time]=in.nextInt();
}
in.close();
for (int i = 0; i < t; i++)
{
new re(rec[f1], rec[f2], rec[f3]);
f1=f3+1;f2=f1+1;f3=f2+1;
}
}
}
class re{
int all=0;
String block="";
int array[];
public re(int a, int b, int n)
{
for (int i = 0; i <n; i++)
{
all=i==0?a:all;
block=i==n-1?"\n":" ";
all+=add(i)*b;
System.out.print(all+" "+block);//214742147483646 //2147483646
}
}
int add(int a)
{
int n=1;
if(a==0)
{
return n;
}
else
{
for(int i=1;i<=a;i++)
{
n=n*2;
}
return n;/**/
}
}
}
這是python的:
import sys
sys.setrecursionlimit(10000000)
def func2(n,b):
if n<=1 :
return b
else :
return (2**(n-1)*b) + func2(n-1,b)
def func1(a,b,n):
if n <= 1:
return str(a+func2(n,b))
else:
return str(func1(a,b,n-1)) +" "+ str(a+func2(n,b))
while True:
print(func1(int(input("a")),int(input("b")),int(input("n"))))