새로새록

[c++]5. 연속되는 숫자, 배열 검사 -기초 본문

소프트웨어융합/경희대 c++ 과제

[c++]5. 연속되는 숫자, 배열 검사 -기초

류지나 2020. 7. 16. 19:25

자연수 n을 입력받고, 길이가 n인 배열에 숫자가 1부터 n까지 중복 없이 들어 있는지 확인하는 프로그램을 작성하세요.

1) 입력받은 숫자의 크기만큼 new를 활용하여 배열 크기를 할당합니다.
2) 프로그램은 반복되며, 1보다 작은 숫자를 입력 할 경우에 프로그램을 종료합니다.

 

#include <iostream>
#include <string>
using namespace std;

int main() {
	int n;
	while (1) {
		cout << "Please enter number of values to process : ";
		cin >> n;
		if (n < 1)
			exit(100);
		int *lst;
		lst = new int[n];
		cout << "Please enter numbers : ";

		for (int i = 0; i < n; i++)
			cin >> lst[i];

		int sw = 1;

		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < n; j++) {
				if (lst[j] == i)
					break;
				if (j == n - 1)
					sw = 0;
			}
			if (sw == 0)
				break;
		}

		delete[] lst;

		
		if (!sw)
			cout << "False";
		else
			cout << "True";
		cout << endl << endl;
	}

	return 0;
}