Advertisement
bryanhenao

Untitled

Jul 9th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class coprimes {
  4.     public static void main(String[] args) throws IOException {
  5.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  6.         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
  7.         String linea;
  8.         String num;
  9.         int num1, num2;
  10.         int t = 1;
  11.         while ((linea = in.readLine()) != null) {
  12.             int n = Integer.parseInt(linea);
  13.             for (int i = 0; i < n; i++) {
  14.                 num = in.readLine();
  15.                 boolean xd = false;
  16.                 for (int j = 1; j < num.length() - 1; j++) {
  17.                     num1 = Integer.parseInt(num.substring(0, j));
  18.                     num2 = Integer.parseInt(num.substring(j, num.length()));
  19.                     if (gcd(num2, num1) == 1) {
  20.                         out.write("Ticket #" + t + ":\n");
  21.                         out.write(num1 + " " + num2 + "\n");
  22.                         t++;
  23.                         xd = true;
  24.                         break;
  25.                     }
  26.                 }
  27.                 if (xd == false) {
  28.                     out.write("Ticket #" + t + ":\nNot relative\n");
  29.                     t++;
  30.                 }
  31.             }
  32.         }
  33.         in.close();
  34.         out.flush();
  35.         out.close();
  36.     }
  37.  
  38.     public static int gcd(int a, int b) {
  39.         if (a == 0)
  40.             return b;
  41.  
  42.         while (b != 0) {
  43.             if (a > b)
  44.                 a = a - b;
  45.             else
  46.                 b = b - a;
  47.         }
  48.  
  49.         return a;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement