U E D R , A S I H C RSS

Number Baseball Game/동기


~cpp 
#include <iostream>
#include <ctime>
using namespace std;

int number[3];
int input[3];

int random(int[],int);

void main()
{
	int strike,ball;
	int i = 0;
	while(i==0)
	{
		srand((unsigned)time(NULL));
		number[0] = rand()%10;
		number[1] = rand()%10;
		number[2] = rand()%10;
		
		if (number[0]!=0 && number[0]!=number[1] && number[1]!=number[2] && number[0]!=number[2])
			i = 1;
	}
	cout<<number[0]<<number[1]<<number[2]<<endl;
	

	while(strike<3)
	{
		i=0;
		while(i==0)
		{
			cout <<"숫자를 입력하세요 ex)3 5 2 >> ";
			cin>>input[0];
			cin>>input[1];
			cin>>input[2];
			cin.get();
			if (input[0]!=0 && input[0]!=input[1] && input[1]!=input[2] && input[0]!=input[2])
				i = 1;
		}
		
		strike = 0;
		ball = 0;	
		
		for (int k=0;k<3;k++)
		{
			if (number[k]==input[k])
				strike++;
		}
		for (k=1;k<3;k++)
		{
			if (number[0]==input[k])
				ball++;
		}
		for (k=0;k<3;k+=2)
		{
			if (number[1]==input[k])
				ball++;
		}
		for (k=0;k<2;k++)
		{
			if (number[2]==input[k])
				ball++;
		}

		if (strike==0 && ball==0)
		{
			cout <<"out"<<endl;
		}
		else
		{
			cout <<" strike"<<strike<<"\t"<<" ball"<<ball<<endl;
		}
	}
	cout <<"정답입니다."<<endl;

				
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:51
Processing time 0.0103 sec