Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileWriter;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- static int nodForTwo(int m, int f) {
- while (m != f) {
- if (m > f) {
- m = m - f;
- } else {
- f = f - m;
- }
- }
- return m;
- }
- private static int currentIndex = -1;
- private static Integer next(String numbers[]) {
- ++currentIndex;
- while (currentIndex < numbers.length
- && numbers[currentIndex].equals(""))
- ++currentIndex;
- return currentIndex < numbers.length ?
- Integer.parseInt(numbers[currentIndex]) :
- null;
- }
- static int nod (int[] a){
- int k , n = 0;
- k = nodForTwo(a[1], a[2]);
- for (int i = 3 ; i < n; i++) {
- k = nodForTwo(k, a[i]);
- }
- return k ;
- }
- static void saveFile( int k ) throws IOException {
- Scanner in = new Scanner(System.in);
- String outputFileName;
- System.out.println("Введите имя файла в который хотите вывести данные:");
- outputFileName = in.nextLine();
- FileWriter out = new FileWriter(outputFileName);
- out.write("\n Наибольший общий делитель : " + k );
- out.close();
- }
- public static void main (String[] args) throws IOException {
- String NameOfFile;
- Scanner in = new Scanner(System.in);
- FileInputStream inFile = new FileInputStream("D:\\lab rab\\2_5(j).txt");
- byte[] str = new byte[inFile.available()];
- inFile.read(str);
- String text = new String(str);
- String[] numbers = text.split("\\D");
- int n = next(numbers);
- int k ;
- int[] a = new int[n];
- for (int i = 0; i <= n;i++){
- a[i]=next(numbers);
- }
- inFile.close();
- k = nod(a);
- saveFile(k);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement