새로새록
[c++]5. 연속되는 숫자, 배열 검사 -기초 본문
자연수 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;
}
'소프트웨어융합 > 경희대 c++ 과제' 카테고리의 다른 글
[c++]6. class :학번 이름 전공 입출력 (0) | 2020.07.16 |
---|---|
[c++]5. 행렬 두개와 2D vector (0) | 2020.07.16 |
[c++] vector에서 push_back 사용 예 (0) | 2020.07.16 |
[c++] 5. vector 역순으로 만들기 (0) | 2020.07.15 |
[c++]4. 최대 길이를 정한 텍스트파일 (0) | 2020.07.15 |