Практикум по программированию. Основы. Массивы. Числа входящие во все массивы

From AsIsWiki
Jump to: navigation, search

Назад · Задачи · Дальше


Contents

Java

public class Task18 {

    private static final int n = 16;

    public static void main(String[] args) {
        
	System.out.println();
        System.out.println(" Поиск чисел встречающихся во всех массивах ");
        System.out.println("--------------------------------------------");

	int a[] = new int[n];
	int b[] = new int[n];
	int c[] = new int[n];
	int d[] = new int[n];

	System.out.print(" Массив A:");

	for (int i = 0; i < n; i++) {
	    a[i] = (int) (10 * Math.random());
	    System.out.print(" " + a[i]);
	}

	System.out.print("\n Массив B:");

	for (int i = 0; i < n; i++) {
	    b[i] = (int) (10 * Math.random());
	    System.out.print(" " + b[i]);
	}

	System.out.print("\n Массив C:");

	for (int i = 0; i < n; i++) {
	    c[i] = (int) (10 * Math.random());
	    System.out.print(" " + c[i]);
	}

        System.out.println("\n--------------------------------------------");
	System.out.print(" Во всех массивах встречаются:");

	int m = 0, w;

	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
		if (a[i] == b[j]) {
		    for (int k = 0; k < n; k++) {
			if (b[j] == c[k]) {
			    w = 0;
			    for (int z = 0; z < m; z++) {
				if (d[z] == a[i]) {
				    w++;
				}
			    }
	   		    if (w == 0) {
				d[m++] = a[i];
				System.out.print(" " + a[i]);
			    }
			}
		    }
		}
	    }
	}

	System.out.println();
    }
}


C++

// g++ 4.2

#include <iostream>

using namespace std;

int main() {

    cout << "\n Поиск чисел встречающихся во всех массивах\n";
    cout << "--------------------------------------------\n";
    
    srand((int)time(0));
    
    const int n = 16;
    
    int a[n], b[n], c[n], d[n];
    
    cout << " Массив A:";
    
    for (int i = 0; i < n; i++) {
        a[i] = rand() % 10;
        cout << " " << a[i];
    }
    
    cout << "\n Массив B:";
    
    for (int i = 0; i < n; i++) {
        b[i] = rand() % 10;
        cout << " " << b[i];
    }
    
    cout << "\n Массив C:";
    
    for (int i = 0; i < n; i++) {
        c[i] = rand() % 10;
        cout << " " << c[i];
    }
    
    cout << "\n--------------------------------------------\n";
    cout << " Во всех массивах встречаются:";
    
    int m = 0, w;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (a[i] == b[j]) {
                for (int k = 0; k < n; k++) {
                    if (b[j] == c[k]) {
                        w = 0;
                        for (int z = 0; z < m; z++) {
                            if (d[z] == a[i]) {
                                w++;
                            }
                        }
                        if (w == 0) {
                            d[m++] = a[i];
                            cout << " " << a[i];
                        }
                    }
                }
            }
        }
    }
    
    cout << "\n\n";
    return 0;
}


Pascal




Назад · Задачи · Дальше

Personal tools
Namespaces

Variants
Actions
Navigation
Tools