#include <iostream>
using namespace std;
int main(void)
{
srand(time(NULL));
#pragma region // 실습1. 3 ~ 7 사이의 랜덤한 숫자를 출력하는 프로그램을 작성하자.(3, 7 포함)
cout << "#####실습1#####" << endl;
cout << rand() % (4 + 1) + 3 << endl; // 3 + 0 ~ 32767 % 5 - 5로 나누면 0 ~ 4까지의 수만 나올 수 있다.
cout << "###############" << endl << endl;
#pragma endregion
#pragma region // 실습2. 가장 작은 값과 큰 값을 입력받아서, 그 사이의 임의의 값을 출력하는 프로그램을 작성하자.
int input_min_num;
int input_max_num;
cout << "#####실습2#####" << endl;
cout << "작은 숫자를 입력해주세요 : ";
cin >> input_min_num;
cout << "큰 숫자를 입력해주세요 : ";
cin >> input_max_num;
cout << "숫자 범위는 " << input_min_num << "부터 " << (input_max_num - input_min_num) + input_min_num << "까지입니다." << endl;
cout << rand() % (input_max_num - input_min_num + 1) + input_min_num << endl;
cout << "###############" << endl << endl;
#pragma endregion
#pragma region // 실습3. 프로그램을 실행하면 내부적으로 1 ~ 10 사이의 숫자가 정답으로 정해진다.
// 유저가 1 ~ 10 사이의 숫자를 입력하면 맞으면 true, 틀리면 false를 출력한다.
int rand_num = rand() % 10 + 1;
int input;
cout << boolalpha;
cout << "#####실습3#####" << endl;
cout << "1 ~ 10 사이의 숫자를 입력해주세요 : ";
cin >> input;
cout << (input == rand_num) << endl;
cout << "###############" << endl << endl;
#pragma endregion
//추가 지식 rand()함수의 데이터 크기는?
cout << "rand() 함수의 데이터크기" << endl;
cout << "rand() 함수의 데이터크기는" << sizeof(rand()) << "Byte 입니다." << endl;
cout << "즉, int와 같으며 0~32767사이의 값을 반환합니다." << endl;
return 0;
}
[C++] 코드 닫기 박스 생성 (0) | 2022.04.12 |
---|---|
[C++] 문법_if 문 (0) | 2022.04.12 |
[C++] C++ 문법(논리연산자, 삼항연산자, 난수) (0) | 2022.04.11 |
[C++] C++ 문법(산술 연산자, 대입 연산자, 복합 대입 연산자, 증감 연산자, 관계 연산자) + 실습 (0) | 2022.04.07 |
[C++] 실습_입출력, 표기법 (0) | 2022.04.06 |
댓글 영역