Languages/C++

[C++] 중복 가능 로또, 중복 불가능 로또 실습

정보보안🌐 2021. 5. 4. 16:18
반응형

코드

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

int big (int a, int b){
	if(a>b) return a;
	else return b;
}

int big(int a[], int size){
	int res = a[0];
	for(int i = 1; i<size; i++)
		if(res < a[i]) res = a[i];
	return res;
}
int small (int a, int b){
	if(a>b) return b;
	else return a;
}

int small(int a[], int size){
	int res = a[0];
	for(int i = 1; i<size; i++)
		if(res > a[i]) res = a[i];
	return res;
}

void lotto(int y[], string s,int size=6){
/*	for(int i=0; i< size; i++){
		if(s == "중복 불가능 로또"){
			중복 제거 알고리즘
			int res = y[0];
			for(int i = 1; i<size; i++){
				if(res == y[i]) --i;
			}
		}
		
		int r = rand()%45+1;
		cout<<r<<endl;
	}
	cout <<size<<endl;
*/	
	if(s =="로또 중복 가능"){
		cout<<"중복 가능 로또 : ";
		for(int i =0; i<size; i++){
			y[i]=rand()%45+1;
			cout<<y[i]<<", ";
		}	
		cout<<endl; 
	}else {
	//	y[0] = rand()%45+1;
		for(int i =1; i<size; i++){
			y[i]=rand()%45+1;
			for(int j = 0;j<i;j++){
				if(y[i]==y[j]){
					--i;
					break;
				}
			}
		}
		cout<<endl<<"중복 불가 로또 : ";
		for (int i = 0;i<size;i++){
			cout<<y[i]<<", " ;
		}	
	}

}
int main()
{
	
	//int ar[] = {1,8,-3,4,6,7,8,6,4,56,4};
	//cout<<big(2,3)<<endl;
	//cout<<"최대 값 : "<<big(ar,sizeof(ar)/sizeof(ar[0]))<<endl;
	//cout<<small(2,3)<<endl;
	//cout<<"최소 값 : "<<small(ar,sizeof(ar)/sizeof(ar[0]))<<endl;
	
	srand(time(NULL));
//	int r = rand()%100+1;//1~100 사이의 임의의 수 
//	cout<<r<<endl;
	int x[6];
	lotto(x,"로또 중복 가능" );
	lotto(x,"중복 불가능 로또", sizeof(x)/sizeof(x[0]));
	 
	return 0;
}

 

결과

반응형