我需要做这个公式:
=(A1 / B1)+(A2 / B2)+(A3 / B3)等到第356行,我怎么能以更短的方式做到?
答案 0 :(得分:4)
使用:
import java.math.*;
import java.io.*;
public class FastFibonacci
{
private static BigInteger[] answers;
private static BigInteger one;
private static BigInteger zero;
private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
public static BigInteger fastFibonacci(int n)
{
if((n == 1) || (n == 2))
return answers[0];
if(answers[n-1].compareTo(zero) != 0)
return answers[n-1];
if(answers[n-2].compareTo(zero) == 0)
answers[n-2] = fastFibonacci(n-1);
if(answers[n-3].compareTo(zero) == 0)
answers[n-3] = fastFibonacci(n-2);
return answers[n-2].add(answers[n-3]);
}
public static void main(String[] args)
{
int n;
long time, newTime;
BigInteger answer;
System.out.println("Type a positive integer." );
try{
String input = stdin.readLine();
n = Integer.parseInt( input );
zero = new BigInteger("0");
one = new BigInteger("1");
answers = new BigInteger[n];
answers[0] = new BigInteger("1");
answers[1] = new BigInteger("1");
for(int i = 2; i < n; i++)
answers[i] = new BigInteger("0");
time = System.currentTimeMillis();
answer = fastFibonacci(n);
newTime = System.currentTimeMillis();
System.out.println("The "+n+"th Fibonacci number is "+ answer);
System.out.println("It took " + (newTime-time) + " milliseconds to compute it.");
}
catch(java.io.IOException e)
{
System.out.println(e);
}
}
}
这是一个数组公式。使用 Ctrl 确认 - Shift - 输入。
编辑:添加另一列:
=SUM(A1:A356/B1:B356)
同样是一个数组公式所以 Ctrl - Shift - Enter
答案 1 :(得分:2)
作为非数组公式
=SUMPRODUCT(A1:A356,1/B1:B356)