Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package src;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7.  
  8. public class Carry {
  9.    
  10.     public static void main(String[] args) throws IOException {
  11.         Carry carry = new Carry();
  12.        
  13.        
  14.         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  15.        
  16.         // Loop through every input line.
  17.         String line = input.readLine();
  18.         while (line != null && line.length() != 0) {
  19.             String[] inputNumbers = line.split(" ");
  20.            
  21.             int carryCount = carry.computeCarry(inputNumbers[0], inputNumbers[1]);
  22.            
  23.             // Grammar correction.
  24.             if (carryCount == 0) {
  25.                 System.out.println("No carry operation.");
  26.             }
  27.             else {
  28.                 String s = (carryCount == 1) ? "" : "s";
  29.                 System.out.println(carryCount + " carry operation" + s + ".");
  30.             }
  31.             line = input.readLine();
  32.         }
  33.         input.close();
  34.     }
  35.    
  36.     /*
  37.      * Computes and prints the number of 'carry' operations when adding two
  38.      * positive integers of <10 digits.
  39.      */
  40.     int computeCarry(String first, String second) {    
  41.         // ensure first is > second
  42.         if (second.length() > first.length()) {
  43.             String temp = second;
  44.             second = first;
  45.             first = temp;
  46.         }
  47.        
  48.  
  49.         // reverse the strings, to make indexing possible.
  50.         first = new StringBuffer(first).reverse().toString();
  51.         second = new StringBuffer(second).reverse().toString();
  52.        
  53.         // The total number of cary operations for this sum.
  54.         int carryCount = 0;
  55.         // The number we 'carry' over to the next column.  Only ever zero or 1.
  56.         int carryOver = 0;
  57.        
  58.         // Loop through the number sequence, adding each digit and
  59.         // the carry '1' where applicable, incrementing carryCount for every carry
  60.         for (int i = 0; i < second.length() ; i ++) {
  61.             int digitOne = Integer.parseInt(first.substring(i, i+1));      
  62.             int digitTwo = Integer.parseInt(second.substring(i, i+1));
  63.  
  64.             int sum = digitOne + digitTwo + carryOver;
  65.             System.out.println(digitOne + " + " + digitTwo + " + " + carryOver + " (cC: " + carryCount + ")");
  66.  
  67.            
  68.             // If the sum of this column is 10 or more, we need to carry.
  69.             if (sum >= 10) {
  70.                 carryCount++;
  71.                 carryOver = 1;
  72.             }
  73.             else {
  74.                 carryOver = 0;
  75.             }
  76.  
  77.         }
  78.         return carryCount;
  79.     }
  80.    
  81.    
  82.  
  83. }
Add Comment
Please, Sign In to add comment