Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import java.io.FileWriter;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     static int nodForTwo(int m, int f) {
  9.         while (m != f) {
  10.             if (m > f) {
  11.                 m = m - f;
  12.             } else {
  13.                 f = f - m;
  14.             }
  15.         }
  16.         return m;
  17.     }
  18.  
  19.     private static int currentIndex = -1;
  20.  
  21.     private static Integer next(String numbers[]) {
  22.         ++currentIndex;
  23.         while (currentIndex < numbers.length
  24.                 && numbers[currentIndex].equals(""))
  25.             ++currentIndex;
  26.         return currentIndex < numbers.length ?
  27.                 Integer.parseInt(numbers[currentIndex]) :
  28.                 null;
  29.     }
  30.  
  31.  
  32.     static int nod (int[] a){
  33.         int k , n = 0;
  34.         k = nodForTwo(a[1], a[2]);
  35.         for (int i = 3 ; i < n; i++) {
  36.             k = nodForTwo(k, a[i]);
  37.         }
  38.         return k ;
  39.     }
  40.     static void saveFile( int k ) throws IOException {
  41.         Scanner in = new Scanner(System.in);
  42.         String outputFileName;
  43.         System.out.println("Введите имя файла в который хотите вывести данные:");
  44.         outputFileName = in.nextLine();
  45.         FileWriter out = new FileWriter(outputFileName);
  46.         out.write("\n Наибольший общий делитель : " + k );
  47.         out.close();
  48.     }
  49.  
  50.  
  51.     public static void main (String[] args) throws IOException {
  52.         String NameOfFile;
  53.         Scanner in = new Scanner(System.in);
  54.         FileInputStream inFile = new FileInputStream("D:\\lab rab\\2_5(j).txt");
  55.         byte[] str = new byte[inFile.available()];
  56.         inFile.read(str);
  57.         String text = new String(str);
  58.         String[] numbers = text.split("\\D");
  59.         int n = next(numbers);
  60.         int k ;
  61.         int[] a = new int[n];
  62.         for (int i = 0; i <= n;i++){
  63.            a[i]=next(numbers);
  64.         }
  65.         inFile.close();
  66.         k = nod(a);
  67.         saveFile(k);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement