326 - Extrapolation Using a Difference Table

UVa網站題目連結
My Solved Problems Performance

A very old technique for extrapolating a sequence of values is based on the use of a difference table. The difference table used in the extrapolation of a sequence of 4 values, say 3, 6, 10, and 15, might be displayed as follows:

The original sequence of values appears in the first column of the table. Each entry in the second column of the table is formed by computing the difference between the adjacent entries in the first column. These values (in the second column) are called first differences. Each entry in the third column is similarly the difference between the adjacent entries in the second column; the third column entries are naturally called second differences. Computation of the last column in this example should now be obvious (but beware that this value is not always zero). Note that the last column will always contain only a single value. If we begin with a sequence of n values, the completed difference table will have n columns, with the single value in column n representing the single n-1st difference.

To extrapolate using a difference table we assume the n-1st differences are constant (since we have no additional information to refute that assumption). Given that assumption, we can compute the next entry in the n-2nd difference column, the n-3rd difference column, and so forth until we compute the next entry in the first column which, of course, is the next value in the sequence. The table below shows the four additional entries (in boxes) added to the table to compute the next entry in the example sequence, which in this case is 21. We could obviously continue this extraolation process as far as desired by adding additional entries to the columns using the assumption that the n-1st differences are constant.

Input and Output

The input for this problem will be a set of extraolation requests. For each request the input will contain first an integer n, which specifies the number of values in the sequence to be extended. When n is zero your program should terminate. If n is non-zero (it will never be larger than 10), it will be followed by n integers representing the given elements in the sequence. The last item in the input for each extrapolation request is k, the number of extrapolation operations to perform; it will always be at least 1. In effect, you are to add k entries to each column of the difference table, finally reporting the last value of the sequence computed by such extrapolation. More precisely, assume the first n entries (the given values) in the sequence are numbered 1 through n.

Your program is to determine the n+kth value in the sequence by extrapolation of the original sequence k times.

Hint: no upper limit is given for k, and you might not be able to acquire enough memory to construct a complete difference table.

Sample Input

4 3 6 10 15 1
4 3 6 10 15 2
3 2 4 6 20
6 3 9 12 5 18 -4 10
0

Sample Output

Term 5 of the sequence is 21
Term 6 of the sequence is 28
Term 23 of the sequence is 46
Term 16 of the sequence is -319268

Solution

  1. import java.io.BufferedInputStream;
  2. import java.util.Scanner;
  3. import java.util.Vector;
  4.  
  5. public class Main {
  6.  
  7. static Scanner in = new Scanner(new BufferedInputStream(System.in));
  8. static int n, k;
  9. static Vector<Vector<Integer>> int2DArray = new Vector<Vector<Integer>>();
  10.  
  11. public static void main(String[] args) {
  12.  
  13. while ((n=in.nextInt())!=0) {
  14.  
  15. int2DArray = new Vector<Vector<Integer>>();
  16. for (int i=0; i<n; i++) {
  17. int2DArray.add(new Vector<Integer>());
  18. }
  19. for (int i=0; i<n; i++) {
  20. int2DArray.elementAt(0).add(i, in.nextInt());
  21. }
  22. k = in.nextInt();
  23. for (int i=1; i<n; i++) {
  24. for (int j=0; j<n-i; j++) {
  25. int2DArray.elementAt(i).add(j, int2DArray.elementAt(i-1).
  26. elementAt(j+1)-int2DArray.elementAt(i-1)
  27. .elementAt(j));
  28. }
  29. }
  30.  
  31. for (int i=0; i<k; i++) {
  32. for (int j=n-1; j>=0; j--) {
  33. if (j!=n-1) {
  34. int2DArray.elementAt(j).add(int2DArray.elementAt(j).
  35. lastElement()+int2DArray.elementAt(
  36. j+1).lastElement());
  37. } else {
  38. int2DArray.elementAt(j).add(int2DArray.elementAt(j).
  39. lastElement());
  40. }
  41. }
  42. }
  43. System.out.println("Term " + (n+k) + " of the sequence is " +
  44. int2DArray.elementAt(0).lastElement());
  45. }
  46. }
  47. }
關鍵字:UVa Online Judge, ACM. Java


留言

相關文章

這個網誌中的熱門文章

【汽機車燃料費攻略】2019/108年度信用卡回饋/分期整理!

【永豐大戶DAWHO】現金回饋信用卡國內2%國外3%最高8%!

【證件遺失補辦】身分證/健保卡/駕照/行照/信用卡總整理

【永豐DAWHO攻略】申辦大戶帳戶/信用卡最佳申辦順序!

【博客來折價券】博客來免費序號e-coupon分享(持續更新)

【永豐大戶DAWHO】活存100萬內1.1%+16次免費跨轉跨提!

秒級偵測網頁更新變動通知(PageMonitor,Check4Change)

【超商懶人包】消費享10%回饋攻略+繳費取貨也能賺回饋?

【永豐信用卡推薦方案】首刷800+指定卡再200或機場接送!

【銀行代碼查詢】3碼銀行代碼列表、7碼分行代碼查詢