Guest User

Untitled

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