The Java Program: D_knitting/knitting.java

  1 import java.io.*;
  2 import java.util.*;
  3 import java.awt.*;
  4 import java.text.*;
  5 import java.math.*;
  6 
  7 /**
  8  * Solution to Knitting
  9  */
 10 class knitting
 11 {
 12     public Scanner sc;
 13     public PrintStream ps;
 14 
 15     public String toString()
 16     {
 17         return "knitting";
 18     }
 19         
 20     /**
 21      * The main method
 22      */
 23     public void doit() throws Exception
 24     {
 25         sc = new Scanner( System.in ); // new Scanner( new File( "knitting.judge" ) );
 26         ps = System.out; // new PrintStream( new FileOutputStream( "knitting.solution" ) ); 
 27         
 28         for(;;)
 29         {
 30                 // Read n, m, k
 31                 int n = sc.nextInt();
 32                 int m = sc.nextInt();
 33                 int k = sc.nextInt();
 34                 
 35                 // Exit if at the end
 36                 if( n==0 && m==0 && k==0 ) break;
 37                 
 38                 // Read in an array of the differences
 39                 int diffs[] = new int[k];
 40                 for( int i=0; i<k; i++ )
 41                 {
 42                         diffs[i] = sc.nextInt();
 43                 }
 44                 
 45                 // The numbers are small enough that we don't need any fancy math.
 46                 // Just iterate through all of the rows of the project.
 47                 int total = 0;
 48                 int row = n;
 49                 for( int i=0; i<m; i++ )
 50                 {
 51                         // Add in this row
 52                         total += row;
 53                         
 54                         // Size of next row
 55                         row += diffs[i%k];
 56                 }
 57                 
 58                 // And, print the result.
 59                 ps.println( total );
 60         }
 61     }
 62 
 63         public static void main(String[] args) throws Exception
 64         {
 65                 long starttime = System.currentTimeMillis();
 66                 new knitting().doit();
 67                 System.out.println( System.currentTimeMillis() - starttime );
 68         }
 69 }